JPanel重绘不起作用
JPanel repaint doesn't work
我有一个简单的任务。
有一个框架。该框架中有两个面板。在第二个面板中有一个按钮。当用户单击该按钮时,第一个面板必须更改其内容。
代码如下:
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();
}
});
}
}
但这行不通。单击按钮后,我在控制台中看到文本,但第一个面板保持不变。我读到我必须使用 invalidate() validate() 和 repaint() 方法,我做了,但这没有帮助。
如有任何帮助,我们将不胜感激。
在您的 mouseClicked() 方法中,您创建了一个新的 topPanel,但您没有对其进行任何操作。也许您打算从 myFrame 中删除原来的 topPanel,创建一个新的 topPanel,然后将新的 toipPanel 添加到 myFrame。
请注意,这可能不是最佳策略(创建新的 topPanel)。
如果你想 "switch" 面板,那么你应该使用 CardLayout
。 CardLayout
允许 2 个(或更多)组件在容器中共享相同的 space,但一次只能看到一个。
阅读有关 How to Use CardLayout 的 Swing 教程部分,了解更多信息和工作示例。
我有一个简单的任务。
有一个框架。该框架中有两个面板。在第二个面板中有一个按钮。当用户单击该按钮时,第一个面板必须更改其内容。
代码如下:
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();
}
});
}
}
但这行不通。单击按钮后,我在控制台中看到文本,但第一个面板保持不变。我读到我必须使用 invalidate() validate() 和 repaint() 方法,我做了,但这没有帮助。
如有任何帮助,我们将不胜感激。
在您的 mouseClicked() 方法中,您创建了一个新的 topPanel,但您没有对其进行任何操作。也许您打算从 myFrame 中删除原来的 topPanel,创建一个新的 topPanel,然后将新的 toipPanel 添加到 myFrame。
请注意,这可能不是最佳策略(创建新的 topPanel)。
如果你想 "switch" 面板,那么你应该使用 CardLayout
。 CardLayout
允许 2 个(或更多)组件在容器中共享相同的 space,但一次只能看到一个。
阅读有关 How to Use CardLayout 的 Swing 教程部分,了解更多信息和工作示例。