import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class test1 extends JFrame{
JTextField textfield1=new JTextField(10);
JTextField textfield2=new JTextField(10);
JLabel label1=new JLabel("长");
JLabel label2=new JLabel("宽");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
ButtonGroup buttongroup1=new ButtonGroup();
JRadioButton radio1=new JRadioButton("矩形");
JRadioButton radio2=new JRadioButton("园");
JButton bt1=new JButton("画");
int flag=0;
int location1;
int location2;
test1 window1=this;
test1(){
Container contain=this.getContentPane();
this.setSize(150,200);
this.setVisible(true);
contain.add(panel1);
contain.add(panel2);
contain.add(panel3);
contain.add(bt1);
contain.setLayout(new GridLayout(4,1));
panel1.setLayout(new FlowLayout());
panel1.add(label1);
panel1.add(textfield1);
panel2.setLayout(new FlowLayout());
panel2.add(label2);
panel2.add(textfield2);
panel3.add(radio1);
panel3.add(radio2);
buttongroup1.add(radio1);
buttongroup1.add(radio2);
panel3.setLayout(new FlowLayout());
bt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
location1=Integer.parseInt(textfield1.getText());
location2=Integer.parseInt(textfield2.getText());
test2 window2=new test2(window1,flag,location1,location2);
window2.setSize(150,200);
window2.setVisible(true);
setVisible(false);
}
}
);
radio1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
flag=0;
}
});
radio2.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
flag=1;
}
});
}
public static void main(String args[]){
new test1();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test2 extends JFrame{
JButton jb1=new JButton("返回");
test1 window1;
JPanel jp1;
int flag,location1,location2;
test2(test1 window,int flag1,int lo1,int lo2){
window1=window;
flag=flag1;
location1=lo1;
location2=lo2;
Container contain=this.getContentPane();
contain.add(jb1,"North");
if (flag==0)
jp1=new draw_Rec(location1,location2);
else
jp1=new draw_Oval(location1,location2);
contain.add(jp1,"Center");
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
window1.show();
}
});
}
}
class draw_Rec extends JPanel{
int a,b;
public draw_Rec(int a,int b){
this.a=a;this.b=b;
}
public void paint(Graphics g){
super.paintComponents(g);
g.setColor(Color.red);
g.fillRect(a,b,100,100);
}
}
class draw_Oval extends JPanel{
int a,b;
public draw_Oval(int a,int b){
this.a=a;this.b=b;
}
public void paint(Graphics g){
super.paintComponents(g);
g.setColor(Color.red);
g.drawOval(a,b,100,100);
}
}