我创建了一个名为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);
}
}
但这并不奏效。我有一种感觉,这是测试中的太阳法。我该怎么做?我查阅了所有的java书籍,找不到任何有帮助的东西。
请注意,我不想在方法中添加什么参数。
非常感谢。
汤姆。