import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Fonts extends JFrame implements ActionListener{
JButton b1,b2,b3;
Container c;
JPanel p;
int count=0;
public Fonts (){
b1=new JButton("三角形");
b2=new JButton("矩形");
b3=new JButton("椭圆形");
p=new JPanel();
p.add(b1);
p.add(b2);
p.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c=getContentPane();
c.add("North",p);
this.setSize(400,350);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
c.add(new Fonts1());
c.validate();
}else if(e.getSource()==b2){
c.add(new Fonts2());
c.validate();
}else{
c.add(new Fonts3());
c.validate();
}
count++;
if(count>9)
count=1;
}
public static void main(String[] args){
new Fonts();
}
class Fonts1 extends JPanel{
int a,b,c,d,width,heigh;
public void paintComponent(Graphics g){
switch(count){
case 1: a=this.getSize().width/6;
b=0;
c=0;
d=(this.getSize().height)/3;
width=this.getSize().width/3;
heigh=d;
break;
case 2: a=this.getSize().width/2;
b=0;
c=this.getSize().width/3;
d=(this.getSize().height)/3;
width=c*2;
heigh=d;
break;
case 3: a=this.getSize().width/6*5;
b=0;
c=this.getSize().width/3*2;
d=(this.getSize().height)/3;
width=this.getSize().width;
heigh=d;
break;
case 4: a=this.getSize().width/6;
b=(this.getSize().height)/3;
c=0;
d=(this.getSize().height)/3*2;
width=this.getSize().width/3;
heigh=d;
break;
case 5: a=this.getSize().width/2;
b=(this.getSize().height)/3;
c=this.getSize().width/3;
d=(this.getSize().height)/3*2;
width=this.getSize().width/3*2;
heigh=d;
break;
case 6: a=this.getSize().width/6*5;
b=(this.getSize().height)/3;
c=this.getSize().width/3*2;
d=(this.getSize().height)/3*2;
width=this.getSize().width;
heigh=d;
break;
case 7: a=this.getSize().width/6;
b=(this.getSize().height)/3*2;
c=0;
d=(this.getSize().height);
width=this.getSize().width/3;
heigh=d;
break;
case 8: a=this.getSize().width/2;
b=(this.getSize().height)/3*2;
c=this.getSize().width/3;
d=(this.getSize().height);
width=this.getSize().width/3*2;
heigh=d;
break;
default:a=this.getSize().width/6*5;
b=(this.getSize().height)/3*2;
c=this.getSize().width/3*2;
d=(this.getSize().height);
width=this.getSize().width;
heigh=d;
}
Polygon p=new Polygon();
p.addPoint(a,b);
p.addPoint(c,d);
p.addPoint(width,heigh);
g.setColor(Color.yellow);
g.fillPolygon(p);
}
}
class Fonts2 extends JPanel{
int a,b,width,heigh;
public void paintComponent(Graphics g){
switch(count){
case 1: a=0;
b=0;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
case 2: a=this.getSize().width/3;
b=0;
width=a;
heigh=(this.getSize().height)/3;
break;
case 3: a=this.getSize().width/3*2;
b=0;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
case 4: a=0;
b=(this.getSize().height)/3;
width=this.getSize().width/3;
heigh=b;
break;
case 5: a=this.getSize().width/3;
b=(this.getSize().height)/3;
width=this.getSize().width/3;
heigh=b;
break;
case 6: a=this.getSize().width/3*2;
b=(this.getSize().height)/3;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
case 7: a=0;
b=(this.getSize().height)/3*2;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
case 8: a=this.getSize().width/3;
b=(this.getSize().height)/3*2;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
default:a=this.getSize().width/3*2;
b=(this.getSize().height)/3*2;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
}
g.setColor(Color.blue);
g.fillRect(a,b,width,heigh);
System.out.println(width);
}
}
class Fonts3 extends JPanel{
int a,b,width,heigh;
public void paintComponent(Graphics g){
switch(count){
case 1: a=0;
b=0;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
case 2: a=this.getSize().width/3;
b=0;
width=a;
heigh=(this.getSize().height)/3;
break;
case 3: a=this.getSize().width/3*2;
b=0;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
case 4: a=0;
b=(this.getSize().height)/3;
width=this.getSize().width/3;
heigh=b;
break;
case 5: a=this.getSize().width/3;
b=(this.getSize().height)/3;
width=this.getSize().width/3;
heigh=b;
break;
case 6: a=this.getSize().width/3*2;
b=(this.getSize().height)/3;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
case 7: a=0;
b=(this.getSize().height)/3*2;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
case 8: a=this.getSize().width/3;
b=(this.getSize().height)/3*2;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
break;
default:a=this.getSize().width/3*2;
b=(this.getSize().height)/3*2;
width=this.getSize().width/3;
heigh=(this.getSize().height)/3;
}
g.setColor(Color.red);
g.fillOval(a,b,width,heigh);
}
}
}
看看还有什么更好的方法来写