import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class CircleCLT//圆的计算布局第一个。
{
JFrame frame;
JLabel l1,l2,l3;
JTextField tf1,tf2,tf3;
JButton b1,b2,b3,b4;
JPanel p1,p2,p3;
JTextArea ta;
static double h;
public static void main(String[] args)
{
CircleCLT cc=new CircleCLT();
cc.go();
}
public void go()
{
frame=new JFrame("圆的计算");
l1=new JLabel("请输入圆的半径",l1.LEFT);
tf1=new JTextField(10);
b1=new JButton("确定");
b1.setForeground(Color.white);
b1.setBackground(Color.black);
b2=new JButton("重置");
b2.setForeground(Color.black);
b2.setBackground(Color.white);
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf1.setText("");//setText()是修改文本的方法,让它的新参数为空(""),就等于清空了三个文本域。
tf2.setText("");
tf3.setText("");
}
});
p1=new JPanel();
p1.add(l1);
p1.add(tf1);
p1.add(b1);
p1.add(b2);
l2=new JLabel("圆的面积");
tf2=new JTextField(10);
p2=new JPanel();
p2.add(l2);
p2.add(tf2);
l3=new JLabel("圆的周长");
tf3=new JTextField(10);
p3=new JPanel();
p3.add(l3);
p3.add(tf3);
ta=new JTextArea(50,1);
b3=new JButton("清除");//如果逗号用输入汉字时所用的逗号,会出现"非法的标识符"的错误
b3.setForeground(Color.black);
b3.setBackground(Color.white);
b3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ta.setText("");
}
});
JScrollPane p4=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
b1.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
h=Double.parseDouble(tf1.getText());
ta .append("\n圆的半径:"+h);
}
});
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double w;
w=(h*3.14*2);
ta .append("\n圆的面积:"+w);
}
});
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double y;
String g;
y=3.14*h*h;
g=Double.parseDouble(y);
ta .append("\n圆的周长:"+g);
}
});
b4=new JButton("拷贝");
b4.setForeground(Color.black);
b4.setBackground(Color.white);
b4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String h=tf1.getText();
ta .append("\n圆的半径:"+h);
String w=tf2.getText();
ta .append("\n圆的面积:"+w);
String y=tf3.getText();
ta .append("\n圆的周长:"+y);
}
});
JPanel p6=new JPanel();
p6.add(b3);
p6.add(b4);
JPanel p5=new JPanel();
p5.setLayout(new GridLayout(1,2));
p5.add(p4);
p5.add(p6);
Container cp=frame.getContentPane();
cp.setLayout(new GridLayout(4,1));
cp.add(p1);
cp.add(p2);
cp.add(p3);
cp.add(p5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450,300);
frame.setVisible(true);
}
}