package javademo;
import javax.swing.*;
import java.awt.*;
public class DrawDemo extends JFrame{
MyPanel mp = null;
public static void main(String []args){
DrawDemo drawdemo = new DrawDemo();
}
public DrawDemo(){
mp = new MyPanel();
this.add(mp);
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel {
//重寫JPanel的Paint方法
//Graphics是繪圖的重要類,你可以把它理解成一支畫筆
public void Paint(Graphics g){
//調用父類函數完成初始化
//這句話不能少
super.paint(g);
System.out.println("321");
//先畫一個圓圈
g.drawOval(10, 10, 40, 60);
}
}
補充說明,測試了下,的確是方法名稱寫錯了。修正就好了。
import javax.swing.*;
import java.awt.*;
public class DrawDemo extends JFrame {
MyPanel mp = null;
public static void main(String[] args) {
DrawDemo drawdemo = new DrawDemo();
}
public DrawDemo() {
mp = new MyPanel();
this.add(mp);
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel {
// 重寫JPanel的Paint方法
// Graphics是繪圖的重要類,你可以把它理解成一支畫筆
public void paint(Graphics g) {
// 調用父類函數完成初始化
// 這句話不能少
super.paint(g);
System.out.println("321");
// 先畫一個圓圈
g.drawOval(10, 10, 40, 60);
}
}