考虑到扩展了 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 做过任何事情了。
有人可以帮忙吗?
线程循环工作正常,
但重画似乎没有安排......
You're 实现 the JFrame
在添加组件之前(即Canvas
,以及之前的JPanel
).
Try this
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);
}
EDIT
顺便说一句,我建议使用JPanel
代替Canvas
因为不建议您混合重型和轻型组件。有关更多详细信息,请参阅混合重组分和轻组分 http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)