当WorldPanel显示时,我想得到(图形g)。由于堆栈溢出,我看到了应该在哪里使用getGraphics方法(ComponentListener.componentShown)的答案,但不知何故,我的侦听器无法捕获componentShown。
我做错什么了?在代码片段的底部,不能显示"hello“。
public class MainPanel extends javax.swing.JPanel implements ComponentListener {
private CWorldPanel WorldPanel; // extends JPanel
private void initGUI() {
try {
...
this.setLayout(thisLayout);
{
WorldPanel = new CWorldPanel();
WorldPanel.addComponentListener(this);
...
@Override
public void componentShown(ComponentEvent e) {
System.out.println("hello");
}见我的解释,并在(cscsaba)页面底部感谢
发布于 2011-03-30 12:06:45
如果您想要获取Graphics对象,以便在组件上一看到就进行绘图,就不要这样做。只需重写要显示的对象的paintComponent方法,然后在其中进行绘制。
(我不知道为什么您的ComponentListener不能工作,也无法尝试,因为您的示例是不完整的。)
https://stackoverflow.com/questions/5484455
复制相似问题