java 单击按钮后重新绘制 JPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10363824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Repainting JPanel after button click
提问by Anass
I'm new in Java Swing and I have a strange problem to refresh my JPanel
.
我是 Java Swing 的新手,我有一个奇怪的问题来刷新我的JPanel
.
I create a static JPanel
componant in my frame and I call a static method from a FileListenner
to repaint my JPanel
我JPanel
在我的框架中创建了一个静态组件,并从 a 调用了一个静态方法FileListenner
来重新绘制我的JPanel
public static void repaintPlan(JPanel f) {
f.paint(f.getGraphics());
f.revalidate(); // or validate
}
I mean, when I detect change in file content, I calculate the new coordinates and I repaint the JPanel
(I create a class exends from JPanel to define
paintComponent()` method)
我的意思是,当我检测到文件内容发生变化时,我会计算新坐标并重新绘制JPanel
(我从JPanel to define
paintComponent()`方法创建了一个类扩展)
Everything is working fine when I run the app, and the repaint works when a change data in file; but if I click with my mouse in the Jpanel
, the repaint()
method doesn't work anymore. Can you tell me why after clicking on JPanel
, repainting doesn't work ?
当我运行应用程序时一切正常,当文件中的数据发生变化时重绘工作;但是如果我在 中用鼠标单击Jpanel
,该repaint()
方法将不再起作用。你能告诉我为什么点击后JPanel
,重新绘制不起作用吗?
Sorry for my bad english Thanks in advance:)
对不起,我的英语不好提前致谢:)
Edit: Thanks for your repsonses! But even if I use repaint()
method, it's the same problem. I'm trying to understand what happens when I click on JPanel
. Should I use mouse events in Swing to solve the problem?
编辑:感谢您的回复!但即使我使用repaint()
方法,也是同样的问题。我试图了解当我点击 时会发生什么JPanel
。我应该在 Swing 中使用鼠标事件来解决问题吗?
回答by mKorbel
1) for Swing JComponents
is there method paintComponent()
, method paint()
is for Top-Level Containers (JFrame
, JDialog ...) and for AWT Components
1) 对于 Swing JComponents
is there method paintComponent()
,方法paint()
用于顶级容器 ( JFrame
, JDialog ...) 和用于AWT Components
2) don't use getGraphics()
this method created snapshot that after calling validate
, revalidate
, repaint
expired
2)不要用getGraphics()
这种方法创建的快照在调用后validate
,revalidate
,repaint
过期
3) you have look at tutorial about 2D Graphics, examples here
3) 你看过关于2D Graphics 的教程,这里的例子
4) in the case that you'll have real question, please edit your question with SSCCE
4) 如果您有真正的问题,请使用SSCCE编辑您的问题
回答by trashgod
I'm trying to understand what happens when I click on
JPanel
. Should I use mouse events in Swing to solve the problem?
我试图了解当我点击 时会发生什么
JPanel
。我应该在 Swing 中使用鼠标事件来解决问题吗?
You might get some insight from this examplethat responds to mouse pressed events. In this case, paintComponent()
is called automatically when the color is updated. See also Painting in AWT and Swing.
您可能会从这个响应鼠标按下事件的示例中获得一些见解。在这种情况下,paintComponent()
在颜色更新时自动调用。另请参阅在 AWT 和 Swing 中绘画。
回答by Hovercraft Full Of Eels
No, paintComponent is not called after a mouse press, not unless you've got some code that makes it do this. For example:
不,paintComponent 在鼠标按下后不会被调用,除非你有一些代码让它这样做。例如:
import java.awt.*;
import javax.swing.*;
public class MyPanel extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = 400;
public MyPanel() {
setBorder(BorderFactory.createTitledBorder("My Panel"));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("myPanel's paintComponent method has been called");
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
MyPanel mainPanel = new MyPanel();
JFrame frame = new JFrame("MyPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
As you can plainly see, in a plain JPanel, paintComponent is not called after any mouse action unless you change the size of the GUI.
如您所见,在普通 JPanel 中,除非您更改 GUI 的大小,否则在任何鼠标操作后都不会调用paintComponent。
Something else is wrong with your GUI, and also, it shouldn't matter if paintComponent is called once or several times since your program logic should not depend on whether or not this method gets called.
您的 GUI 还存在其他问题,而且,调用一次或多次paintComponent 应该无关紧要,因为您的程序逻辑不应该依赖于是否调用此方法。
回答by Anass
I resolve my problem by overriding mouse event methods of my JPanel like this
我通过像这样覆盖我的 JPanel 的鼠标事件方法来解决我的问题
myJPanel.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
thanks
谢谢