使用 Java.swing 使用可调整大小的组件编写 GUI [英] Programming a GUI with resizable components using Java.swing

查看:33
本文介绍了使用 Java.swing 使用可调整大小的组件编写 GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该编写一个 GUI.此 GUI 中的每个组件都必须能够动态调整大小.

I'm supposed to program a GUI. Every component in this GUI has to have the possibility to be resized dynamically.

到目前为止,我使用了 GlassPane 和 ContentPane,添加了一个 JPanel 并在其上添加了一个按钮.当单击 GlassPane 时获取事件,分析底层组件,为这个特殊组件创建一个新句柄并将其处理到所述组件.我添加了一个应该自动改变其大小的按钮.像我想要的那样工作.但是:当我现在更改框架的大小并单击按钮时,什么也没有发生.GlassPane 能够识别按钮,但似乎有问题...

So far I worked with GlassPane and ContentPane, added a JPanel and on it a button. When clicking the GlassPane gets the event, analyses the underlying component, creates a new handle for this special component and handles it over to the said component. I added a button that should change its size automatically. Works like I wanted it to work. BUT: When I change the size of the frame and click on the button now, nothing happens. The GlassPane is able to identify the button, but something seems to be wrong...

这里是拦截GlassPane并将事件交给组件的代码:

Here's the code for the interception of GlassPane and giving the event to the component:

private void resendEvent(MouseEvent e) {
    //Point p = SwingUtilities.convertPoint(content.getGlassPane(), e.getPoint(), content.getContentPane());
    Point p = e.getPoint();
    Component component = SwingUtilities.getDeepestComponentAt(content.getContentPane(), p.x, p.y);
    System.out.println(component.toString());
    Point p2 = component.getLocation();
    MouseEvent event = new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), p2.x, p2.y, e.getClickCount(), e.isPopupTrigger());
            //following lines have the same effects
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event); 
    //component.dispatchEvent(event);
}

感谢您的帮助/建议

好的,这里还有一些代码:

Okay, here's some more Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import resize.ResizablePanel;

public class ExampleProblem extends JFrame {

    public ExampleProblem () {
        JPanel glassPane = new JPanel();
        glassPane.setOpaque(false);
        glassPane.addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(MouseEvent e) {
                resendEvent(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                resendEvent(e);
            }
        });
        this.setGlassPane(glassPane);
        glassPane.setVisible(true);         

        JButton b = new JButton("Test");
        b.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Button clicked");
            }});

        JPanel p = new JPanel();
        p.add(b);
        setContentPane(p);
        pack();
        setVisible(true);
    }

    private void resendEvent(MouseEvent e) {//Point p = SwingUtilities.convertPoint(content.getGlassPane(), e.getPoint(), content.getContentPane());
        Point p = e.getPoint();
        Component component = SwingUtilities.getDeepestComponentAt(this.getContentPane(), p.x, p.y);
        System.out.println(component.toString());
        Point p2 = component.getLocation();
        MouseEvent event = new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), p2.x, p2.y, e.getClickCount(), e.isPopupTrigger());
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
    }

    public static void main(String[] args) {
        new ExampleProblem();
    }   
}

希望问题现在更清楚了.有没有可能,我不应该使用 setContentPane()?但是我们必须覆盖 JContentPane 以调整其上的组件大小...

Hope, the problem is a bit clearer now. Is it possible, that I shouldn't have used setContentPane()? But we had to overwrite the JContentPane for resizing the components on it...

推荐答案

您必须根据您的坐标确定组成点.使用它而不是 p2 来创建事件:

You have to determine the component point from your coordinate. Use this instead of p2 to to create the event:

Point p2 = SwingUtilities.convertPoint(this.getGlassPane(), p, component);

这篇关于使用 Java.swing 使用可调整大小的组件编写 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆