/**绘图*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class QQ27 implements ActionListener{
JFrame f = new JFrame("天天");
MyButton b1;
CB P1;
int tag = 1;

public static  void main(String[] args){
QQ27 CD = new QQ27();
CD.go();
}
public void go(){
b1 = new MyButton("光光");
b1.addActionListener(this);
f.getContentPane().add(b1,"Scuth");

P1 = new CB();
f.getContentPane().add(P1,"Center");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300,200);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(tag == 0){
tag = 1;
b1.setText("去去");

}else{
tag = 0;
b1.setText("回回");
}
P1.repaint();// 重绘
}

 public class MyButton extends JButton{
  MyButton(String text){
     super(text);
  }
  
  protected void paintComponent(Graphics g){
     super.paintComponent(g);
     g.setColor(Color.red);
     int width = getWidth();
     int height = getHeight();
     g.drawOval(4,4, width - 8, height - 8);//绘制椭圆
  }
}

  public class CB extends JPanel{
protected void painComponent(Graphics g){

super.paintComponent(g);
if(tag == 0){
g.setColor(Color.blue); //设置色彩
g.drawLine(40,25,30,50);  //绘制直线
g.setColor(Color.green);
g.drawRect(100,50,100,45);
g.setColor(Color.red);
g.drawRoundRect(73,32,56,37,10,16);
g.setColor(Color.yellow);
g.fillOval(180,60,60,45);
g.setColor(Color.pink);
g.fillArc(250,32,90,60,15,30);
}
}
 }
}编译完成
但运行不了

解决方案 »

  1.   

    f.setLayout(new GridLayout());加上;
    为JFrame设置Layout
    f.getContentPane().add(b1,"Scuth");
    不知道是不是应该写成
    f.getContentPane().add(b1,"South"); 
    运行出来是:一个JButton中间画了一个红色的圆圈.
      

  2.   

    package test;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class QQ27 implements ActionListener {
    JFrame f = new JFrame("天天"); MyButton b1; CB P1; int tag = 1; public static void main(String[] args) {
    QQ27 CD = new QQ27();
    CD.go();
    } public void go() {
    b1 = new MyButton("光光");
    b1.addActionListener(this);
    f.getContentPane().add(b1, BorderLayout.SOUTH); P1 = new CB();
    f.getContentPane().add(P1, BorderLayout.CENTER);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 200);
    f.setVisible(true);
    } public void actionPerformed(ActionEvent e) {
    if (tag == 0) {
    tag = 1;
    b1.setText("去去"); } else {
    tag = 0;
    b1.setText("回回");
    }
    P1.repaint();// 重绘
    } public class MyButton extends JButton {
    MyButton(String text) {
    super(text);
    } protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.red);
    int width = getWidth();
    int height = getHeight();
    g.drawOval(4, 4, width - 8, height - 8);// 绘制椭圆
    }
    } public class CB extends JPanel {
    protected void painComponent(Graphics g) { super.paintComponent(g);
    if (tag == 0) {
    g.setColor(Color.blue); // 设置色彩
    g.drawLine(40, 25, 30, 50); // 绘制直线
    g.setColor(Color.green);
    g.drawRect(100, 50, 100, 45);
    g.setColor(Color.red);
    g.drawRoundRect(73, 32, 56, 37, 10, 16);
    g.setColor(Color.yellow);
    g.fillOval(180, 60, 60, 45);
    g.setColor(Color.pink);
    g.fillArc(250, 32, 90, 60, 15, 30);
    }
    }
    }
    }
    还是用BorderLayout.SOUTH和BorderLayout.CENTER吧