package练习;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Font;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjava.awt.GridLayout;importjava.awt.TextArea;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;public class Test implementsActionListener{
String[] str= {"1","2","3","4","5","6","7","8","9","0",".","DEL","圆","计算","球"};privateTextArea tf;private int flag=0;private String str1 ="请输入半径: ";private String str2="";private String str3="请选择计算的种类";publicTest(){
JFrame f= new JFrame("圆的相关计算");
JLabel lb= new JLabel("显示:");
lb.setBounds(90,100,30,15);
JPanel p= newJPanel();
tf= new TextArea(3,50);
tf.setText(str3);
p.add(lb);
p.add(tf,BorderLayout.SOUTH);
GridLayout gl= new GridLayout(5,3,10,10);
JPanel p2= newJPanel();
p2.setLayout(gl);for (int i= 0; i
JButton b= newJButton(str[i]);
p2.add(b);
b.addActionListener(this);
b.setFont(new Font("宋体", Font.BOLD, 25));
b.setBackground(Color.YELLOW);
}
f.add(p,BorderLayout.NORTH);
f.add(p2,BorderLayout.SOUTH );
f.setSize(400, 400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}public static voidmain(String[] args){newTest();
}public voidactionPerformed(ActionEvent e) {
JButton button=(JButton)e.getSource();if(button.getText().equals("圆"))
{
flag= 1;
tf.setText(str1);return;
}if(button.getText().equals("球"))
{
flag= 2;
tf.setText(str1);return;
}if(button.getText().equals("计算"))
{
Double bo= newDouble(str2);switch(flag){case 1:
tf.setText("圆的面积是:"+bo*bo*Math.PI+" "+ "周长是:"+(bo+bo)*Math.PI+"");break;case 2:
tf.setText("球的体积是:"+bo*bo*bo*(4/3)*Math.PI+" "+ "表面积是:"+4*bo*bo*Math.PI+"");break;
}
str2="";return;
}if(button.getText().equals("DEL"))
{
tf.setText(str3);return;
}else{
str2=str2+button.getText();
tf.setText(str2);
}
}
}