[英]Java AWT Window doesn't get repainted
我从 window 派生了一个类,但是每当我调用 setValue() (它调用 repaint)它都不会被重绘(调用了该方法,但屏幕上没有任何变化)。 绘制第一个值,默认为 0。 这是课程:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Window;
@SuppressWarnings("serial")
public class HashtagLikeDisplay extends Window {
protected int count;
HashtagLikeDisplay() throws HeadlessException {
super(null);
this.setAlwaysOnTop(true);
this.setBounds(this.getGraphicsConfiguration().getBounds());
this.setBackground(new Color(0, true));
this.setVisible(true);
}
public void paint(Graphics g) {
super.paint(g); // Doesn't matter if this is here or not
Font font = getFont().deriveFont(48f);
g.setFont(font);
g.setColor(Color.RED);
String message = "Total: " + Integer.toString(count);
g.drawString(message, 10, 58);
}
public void update(Graphics g) {
paint(g);
}
public void setCount(int c) {
this.count = c;
this.revalidate();
this.repaint();
}
}
为什么它没有得到正确的重新粉刷?
来自 oracle 文档
如果重新实现 [the update] 方法,则应调用 super.update(g) 以便正确呈现轻量级组件。 如果子组件被 g 中的当前裁剪设置完全裁剪,则 update() 将不会转发给该子组件。
另外,就像第一个评论者所说的那样,在paint()中调用super.paint(g)。
如果它仍然不起作用,您应该使用 Swing,就像一个 JComponent 而不是 window。
public class HashtagLikeDisplay extends JComponent {
...
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Customize after calling super.paintComponent(g)
Font font = getFont().deriveFont(48f);
g.setFont(font);
g.setColor(Color.RED);
String message = "Total: " + Integer.toString(count);
g.drawString(message, 10, 58);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.