[英]how to update panel with repaint()?
I have a panel-class:我有一个面板类:
public class MyPanel extends JPanel {
Color myColor = Color.BLUE;
String string = "Hello";
public void update(String newString) {
myColor = Color.GREEN;
string = newString;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(myColor);
g2.drawString(string, 200, 100);
}
}
I want the panel to be repainted after I call the update()-method.我希望在调用 update() 方法后重新绘制面板。 But everything in update() works fine but not the repaint()-method which should repaint the panel.
但是 update() 中的所有内容都可以正常工作,但应该重新绘制面板的 repaint() 方法却不行。 The panel is always the same with the old values.
该面板始终与旧值相同。 How can I update the values and show them in panel?
如何更新值并在面板中显示它们? In my frame-class I am calling the update() method.
在我的框架类中,我调用了 update() 方法。 I checked if I get into that method and it works.
我检查了我是否使用了该方法并且它有效。 So to mention: the calling of the update()-method cant be the problem.
值得一提的是:update() 方法的调用不是问题所在。 I also tried executing update() after some time, but repaint() never works.
我也尝试过一段时间后执行 update() ,但是 repaint() 从来没有用过。
public class MyFrame extends JFrame {
public MyFrame() {
setTitle("Task");
setLayout(new BorderLayout());
add(new MyPanel(), BorderLayout.CENTER);
}
public static void main(String [] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
MyPanel myPanel = new MyPanel();
myPanel.update("new Hello");
}
}
Your code is OK.你的代码没问题。 Here's an example of it working with my suggestion.
这是一个根据我的建议工作的例子。 Don't use wildcard imports in real code like I did;)
不要像我一样在真实代码中使用通配符导入;)
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class F extends JFrame {
class MyPanel extends JPanel implements ActionListener {
Color myColor = Color.BLUE;
String string = "Hello";
public MyPanel() {
super();
Timer t = new Timer(2000, this);
t.start();
}
public void update(String newString) {
// myColor = Color.GREEN;
// Jazz it up
// myColor = Color.GREEN;
myColor = new Color((int) (Math.random() * Integer.MAX_VALUE));
string = newString;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(myColor);
g2.drawString(string, 200, 100);
}
@Override
public void actionPerformed(ActionEvent e) {
String s = "" + (long) (Math.random() * Long.MAX_VALUE);
update(s);
}
}
private void setGui() {
try {
setLocation(0, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.add(new MyPanel());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
F f = new F();
f.setGui();
f.setSize(400, 200);
f.setVisible(true);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.