开发者

Programming a GUI with resizable components using Java.swing

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

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...

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);
}

Thanks for your help/suggestions


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(compon开发者_开发知识库ent, 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();
    }   
}

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...


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);


You might be able to use the Component Resizer. It would need to be added to all components in advance.

Or maybe you could add the listener when you select the component and then remove the listener when you deselect the component.

The code you posted is of little help when guessing what your actual program is doing. When you have problems with your code you need to post a SSCCE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜