package 面积;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class BaiduFrame extends JFrame
{
//创建按钮
public BaiduFrame()
{
this.setTitle("求三角形面积");//窗体名称
this.setBounds(300,200,580,600);
this.setSize(300,200);
this.setLocation(300,200);
//创建文本
JLabel la1=new JLabel("边长A");
JLabel lb2=new JLabel("边长B");
JLabel lb3=new JLabel("边长C");
//创建文本框
JTextField txt1=new JTextField(10);
JTextField txt2=new JTextField(10);
JTextField txt3=new JTextField(10);
//创建容器
JPanel p1,p2,p3,p4;
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
JButton but1=new JButton("提交");
JButton but2=new JButton("清空");
//按钮点击事件
but1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null,"面积="+(Integer.parseInt(txt1.getText())+Integer.parseInt(txt2.getText())+Integer.parseInt(txt3.getText()))/2, "消息", JOptionPane.PLAIN_MESSAGE);
}
});
//清空事件
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
txt1.setText("");
txt2.setText("");
txt3.setText("");
}
});
//设置布局
this.setLayout(new GridLayout(4,1));
//把文本框添加到容器中
p1.add(la1);
p2.add(lb2);
p3.add(lb3);
p1.add(txt1);
p2.add(txt2);
p3.add(txt3);
p4.add(but1);
p4.add(but2);
//把容器添加到文本框中
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args)
{
new BaiduFrame();
}
}