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

别来无恙 提交于 2019-12-30 12:10:10

问题


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 JFrame?

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. */
    }
}

回答1:


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



回答2:


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

AFTER PRESSING THE BUTTON

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.



回答3:


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



来源:https://stackoverflow.com/questions/6547360/how-to-add-components-to-jframe-once-its-visible-without-having-to-resize-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!