单击按钮后重新绘制JPanel [英] Repainting JPanel after button click

查看:362
本文介绍了单击按钮后重新绘制JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java Swing的新手,在刷新JPanel时遇到了一个奇怪的问题.

I'm new in Java Swing and I have a strange problem to refresh my JPanel.

我在框架中创建了一个静态JPanel分量,并从FileListenner调用了一个静态方法来重绘我的JPanel

I create a static JPanel componant in my frame and I call a static method from a FileListenner to repaint my JPanel

 public static void repaintPlan(JPanel f) {
    f.paint(f.getGraphics()); 
    f.revalidate(); // or validate
}

我的意思是,当我检测到文件内容的变化时,我会计算新坐标并重新绘制JPanel(我创建了一个从JPanel to define paintComponent()`方法扩展的类)

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 definepaintComponent()` method)

当我运行该应用程序时,一切正常,并且当文件中的更改数据时,重新绘制工作正常;但是如果我用鼠标单击Jpanel,则repaint()方法将不再起作用.您能告诉我为什么单击JPanel后重新粉刷不起作用吗?

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 ?

对不起,我的英语不好 在此先感谢:)

Sorry for my bad english Thanks in advance:)

感谢您的答复!但是,即使我使用repaint()方法,也是同样的问题.我试图了解单击JPanel时会发生什么.我应该在Swing中使用鼠标事件来解决问题吗?

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?

推荐答案

1)for Swing JComponents是否存在方法paintComponent(),方法paint()用于顶级容器(JFrame,JDialog ... )和AWT Components

1) for Swing JComponents is there method paintComponent(), method paint() is for Top-Level Containers (JFrame, JDialog ...) and for AWT Components

2)不要使用getGraphics(),此方法创建的快照在调用validaterevalidaterepaint后已过期

2) don't use getGraphics() this method created snapshot that after calling validate, revalidate, repaint expired

3)您已经看过有关 2D图形的教程,示例

3) you have look at tutorial about 2D Graphics, examples here

4)如果您有真正的问题,请使用 SSCCE

4) in the case that you'll have real question, please edit your question with SSCCE

这篇关于单击按钮后重新绘制JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆