package 练习;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test implements ActionListener{
String[] str = {"1","2","3",
"4","5","6",
"7","8","9",
"0",".","DEL",
"圆","计算","球"};
private TextArea tf;
private int flag=0;
private String str1 ="请输入半径: ";
private String str2="";
private String str3="请选择计算的种类";
public Test(){
JFrame f = new JFrame("圆的相关计算");
JLabel lb = new JLabel("显示:");
lb.setBounds(90,100,30,15);
JPanel p = new JPanel();
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 = new JPanel();
p2.setLayout(gl);
for (int i= 0; i<str.length;i++){
JButton b = new JButton(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 void main(String[] args){
new Test();
}
public void actionPerformed(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 = new Double(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);
}
}
}