import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestThree {
public static void main(String[] args) {
WindonwPanel win= new WindonwPanel();
win.setBounds(500, 100, 400, 400);
win.setTitle("画板");
}
}
class WindonwPanel extends JFrame implements ActionListener{
MyJPanel jp;
JButton button1,button2,button3;
JPanel p1,p2;
Color co;
public WindonwPanel(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init(){
setLayout(new BorderLayout());
jp = new MyJPanel(co);
p2=new JPanel();
button1=new JButton("红色");
button2=new JButton("黄色");
button3=new JButton("绿色");
button1.setBackground(Color.RED);
button2.setBackground(Color.yellow);
button3.setBackground(Color.GREEN);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
p2.add(button1);
p2.add(button2);
p2.add(button3);
add(jp);
add("South",p2);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1){
jp.c=Color.red;
}
else if(e.getSource()==button2){
jp.c=Color.yellow;
}
else if(e.getSource()==button3){
jp.c=Color.GREEN;
}
repaint();
}
class MyJPanel extends JPanel{
Color c;
public MyJPanel(Color c) {
this.c = c ;
}
public void Paint(Graphics g) {
g.setColor(c);
g.fillOval(200, 100, 100, 100);
}
}
}