简体   繁体   English

JPanel重新绘制不起作用

[英]JPanel repaint doesn't work

I have a simple task. 我有一个简单的任务。

There is a frame. 有一个框架。 There are two panel in that frame. 该框架中有两个面板。 In second panel there is a button. 在第二个面板中有一个按钮。 When user click that button first panel must change its content. 当用户单击该按钮时,第一个面板必须更改其内容。

Here is a code: 这是一个代码:

package test;


import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;


class MyJPanel1 extends JPanel {
    MyJPanel1() {
        this.add(new JButton("MyJPanel1"));
    }
}


class MyJPanel2 extends JPanel {
    MyJPanel2() {
        this.add(new JButton("MyJPanel2"));
    }
}


class MyFrame extends JFrame {
    JPanel topPanel = null;

    MyFrame() {        
        super("Test");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridLayout(0, 1, 20, 20));

        topPanel = new MyJPanel1();                
        this.add(topPanel); 

        JPanel bottomPanel = new JPanel();
        this.add(bottomPanel);

        JButton button = new JButton("switch");
        button.addMouseListener(new MouseClickListener());
        bottomPanel.add(button);

        this.pack();
        this.setVisible(true);
    }    

    class MouseClickListener extends MouseAdapter { 
        @Override
        public void mouseClicked(MouseEvent e) {        
            topPanel = new MyJPanel2();
            System.out.println("switch");

            topPanel.invalidate();
            topPanel.validate();
            topPanel.repaint();

            MyFrame.this.invalidate();
            MyFrame.this.validate();
            MyFrame.this.repaint();
        }
    }
}


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

But that don't work. 但这不起作用。 After I click on button I see text in console, but first panel remain the same. 单击按钮后,我在控制台中看到了文本,但第一个面板保持不变。 I read that I must use invalidate() validate() and repaint() methods and I did, but it isn't help. 我读到我必须使用invalidate()validate()和repaint()方法,但是我这样做了,但这没有帮助。

Any help would be appreciated. 任何帮助,将不胜感激。

If you want to "switch" panels then you should be using a CardLayout . 如果要“切换”面板,则应使用CardLayout The CardLayout allows 2 (or more) components to share the same space in a container but only one is ever visible at a time. CardLayout允许2个(或多个)组件共享一个容器中的相同空间,但一次只能看到一个。

Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples. 阅读Swing教程中有关如何使用CardLayout的部分, 获取更多信息和工作示例。

In your mouseClicked() method you create a new topPanel, but you don't do anything with it. 在mouseClicked()方法中,您将创建一个新的topPanel,但是您对此不做任何事情。 Perhaps you meant to remove the original topPanel from myFrame, create a new topPanel, and then add the new toipPanel to myFrame. 也许您打算从myFrame中删除原始的topPanel,创建一个新的topPanel,然后将新的toipPanel添加到myFrame中。

Note that this may not be the best strategy (creating a new topPanel). 请注意,这可能不是最佳策略(创建新的topPanel)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM