为什么我的重绘不起作用?
考虑到扩展了 Canvas 的 Display 类,我遇到了一个问题。 单个线程在同一个类中运行。 在这个线程中,调用了repaint方法。 然而,虽然线程工作正常,但油漆方法从未被调用!
这是我的代码(我省略了所有不相关的内容):
package display;
public final class Display extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private Frame frame;
private GraphicsEnvironment ge;
private GraphicsDevice gd;
public BDisplay() {
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
frame = new Frame();
//frame.setUndecorated(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.add(this);
frame.setLayout(null);
//gd.setFullScreenWindow(frame);
}
@Override
public final void paint(Graphics g) {
//THIS METHOD IS NEVER CALLED
System.out.println("Paint method was called!");
super.paint(g);
//...
}
@Override
public synchronized void run() {
while (isRunning()) {
//THIS LOOP WORKS FINE.
this.repaint();
}
}
}
它与 isRunning() 等缺少的函数无关。 它们存在,我只是把它们排除在外。
我以前从来没有遇到过这样的问题 虽然我已经有一段时间没有使用 SWING 或 AWT 做过任何事情了。
有人可以帮忙吗?
线程循环工作正常, 但重画似乎没有安排......
I'm stuck with a problem considering my Display class, which extends Canvas.
A single thread is running within the very same class.
In this thread, the repaint method is called.
However, while the thread works fine, the paint method is never called!
Here's my code (I left out everything unrelated):
package display;
public final class Display extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private Frame frame;
private GraphicsEnvironment ge;
private GraphicsDevice gd;
public BDisplay() {
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
frame = new Frame();
//frame.setUndecorated(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.add(this);
frame.setLayout(null);
//gd.setFullScreenWindow(frame);
}
@Override
public final void paint(Graphics g) {
//THIS METHOD IS NEVER CALLED
System.out.println("Paint method was called!");
super.paint(g);
//...
}
@Override
public synchronized void run() {
while (isRunning()) {
//THIS LOOP WORKS FINE.
this.repaint();
}
}
}
It has nothing to do with some missing functions like isRunning().
They exist, i just left those out.
I never stumbled across a problem like this before,
although I haven't done anything with SWING or AWT for some time now.
Can anyone help?
The thread loop works fine,
but the repaint just doesn't seem to get scheduled...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在添加组件(即
Canvas
和之前的JPanel
)之前,您实现了JFrame
。尝试这个
编辑
顺便说一句,我建议使用
JPanel
而不是Canvas
因为不建议您混合重型和轻型组件。有关更多详细信息,请参阅混合重组件和轻组件。You're realizing the
JFrame
before you add the component (i.e.Canvas
, and previouslyJPanel
).Try this
EDIT
By the way, I'd recommend using a
JPanel
instead of aCanvas
since it's not recommended that you mix heavy and light components. For more detail, see Mixing heavy and light components.现在???通过使用 javax.swing.Timer
或
and now ??? by using javax.swing.Timer
or