【发布时间】:2013-03-31 16:06:42
【问题描述】:
我创建了一个名为 Test 的类,很可能是我出错的地方。
import javax.swing.JPanel;
import java.awt.*;
public class Test extends JPanel {
Graphics grap;
public void sun()
{
super.paintComponent(grap);
grap.setColor(Color.YELLOW);
grap.fillOval(0,0,20,20);
}
}
如您所见,我想使用一种方法在面板的左上角绘制一个黄色的“椭圆形”,但我没有使用 PaintComponent 方法。现在我尝试在我的 Paint 组件方法中实现它,该方法位于一个名为 Painting 的类中。
//import...;
public class Painting extends JPanel{
protected void paintComponent(Graphics g)
{
Test test = new Test();
test.sun();
}
现在我创建了一个主窗口,它将创建一个面板并显示黄色椭圆形。
//import...
public class main extends JFrame{
public static main(String [] args){
JFrame window = new JFrame();
window.add(new Painting());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(100,100);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
但这不起作用。我有一种感觉,这是测试中的sun方法。我该如何让它工作?我查看了所有的 Java 书籍,但找不到任何可以提供帮助的内容。
请注意,我并没有给方法添加什么参数。
谢谢 汤姆。
【问题讨论】:
-
什么是
CelestialDisplayPanel? -
在
Test中,grap不会一直是null?
标签: java swing jpanel java-2d paintcomponent