开发者

How to add components to JFrame once it's visible without having to resize it?

I have a program where I have a JFrame with a JButton in it. When the user clicks the JButton, all Components of the JFrame are removed, and a JPanel with red background is added to it.

When I click the JButton, that red JPanel does not become visible unless I resize the JFrame (I am using Windows 7). Is there a way to achieve what I want without having to manually resize the JF开发者_Python百科rame?

Here is a part of the code I am using:

public class Demo implements ActionListener{
    public static void main(String args[]){
           ...............
        button.addActionListener(this); //'button' is an object of Jbutton class.
        frame.setVisible(true); //'frame' is an object of JFrame class.
        ............
    }

    public void actionPerformed(ActionEvent ae){
        frame.removeAllComponents();
        frame.add(panel1); //panel1 is an object of Jpanel class with red  background.

        /* Here is where my problem lies.
           panel1 is not visible to me unless I manually resize the JFrame. */
    }
}


For removing (and then, for example, add new JComponents) JComponents from JPanel or from top-level containers you have to call, only once and on the end of the action:

revalidate();
repaint();

And if you only resize or change JComponents:

validate();
repaint();


For me, this was a bit of an oddity. As it turned out, invoking remove(Component comp), adding the new JPanel, and then invoking pack() worked for me.

public class Demo{

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        final JButton button = new JButton("Press Me");
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                frame.remove(panel);

                final JPanel redPanel = new JPanel(){

                    @Override
                    public Dimension getPreferredSize(){
                        return new Dimension(200, 200);
                    }

                    @Override
                    protected void paintComponent(Graphics g){
                        Graphics g2 = g.create();

                        g2.setColor(Color.RED);
                        g2.fillRect(0, 0, getWidth(), getHeight());

                        g2.dispose();
                    }
                };

                frame.add(redPanel);
                frame.pack();
            }
        });

        panel.add(button);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

BEFORE PRESSING THE BUTTON

How to add components to JFrame once it's visible without having to resize it?

AFTER PRESSING THE BUTTON

How to add components to JFrame once it's visible without having to resize it?

ODDITIES

  1. Invoking removeAll() actually caused the GUI to freeze. It seems that this event has occurred before. This occurred even after I attempted to remove the action listener prior to removal of all components.
  2. I did not need to invoke any of the validate methods, or even repaint the GUI.


you have to force a repaint() in the frame so the frame have to repaint itself.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜