问题描述
我坚持用我的考虑Display类的问题,它扩展画布。
一个线程非常相同的类中运行。
在这个线程,重绘方法被调用。
然而,当线程工作正常,绘制方法永远不会被调用!
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!
下面是我的code(我离开了一切无关):
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();
}
}
}
它没有像isRunning缺少一些功能)做(。
它们的存在,我刚刚离开那些出。
It has nothing to do with some missing functions like isRunning().They exist, i just left those out.
我从未遇到过一个问题跌跌撞撞像在此之前,
虽然我还没有对Swing或AWT有一段时间了什么。
I never stumbled across a problem like this before,although I haven't done anything with SWING or AWT for some time now.
谁能帮助?
螺纹环工作正常,
但重绘只是似乎没有获得计划...
The thread loop works fine,but the repaint just doesn't seem to get scheduled...
推荐答案
你的实现的的的JFrame
添加组件之前(即画布
和previously 的JPanel
)。
You're realizing the JFrame
before you add the component (i.e. Canvas
, and previously JPanel
).
试试这个
public BDisplay() {
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
frame = new Frame();
frame.setResizable(false);
frame.add(this);
//frame.setLayout(null); // Why are you using a null layout?
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
修改
顺便说一句,我推荐使用,而不是一个的JPanel
A 画布
,因为它不建议您混合重和轻组分。更多详细信息,请参见的。
EDIT
By the way, I'd recommend using a JPanel
instead of a Canvas
since it's not recommended that you mix heavy and light components. For more detail, see Mixing heavy and light components.
这篇关于为什么我的重绘工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!