java求三角形的周长和面积

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class JuxingFrame extends JFrame implements ActionListener//3.1界面加载监听器
{
//1。声明苏需要的的组件和容器
JLabel lab = new JLabel ("请输入长和宽");
JButton btnGrith = new JButton ("周长");
JButton btnArea = new JButton ("面积");

JTextField tfa = new JTextField (5);
JTextField tfb = new JTextField (5);


JTextField tfGrith = new JTextField (5);
JTextField tfArea = new JTextField (5);

Container cp = this.getContentPane();
//2,构造函数里初始化界面
JuxingFrame ()
{
//设置窗体的标题栏
super ("矩形测试窗体");
//this.setTitle("三角形窗体")

//设置容器的布局方式
cp.setLayout(new GridLayout(1,2));
// 按照顺序加载组件
cp.add(lab);
cp.add(tfa);
cp.add(tfb);

cp.add(btnGrith);
cp.add(tfGrith);
cp.add(btnArea);
cp.add(tfArea);
//3.2组件绑定监听器
btnGrith.addActionListener(this);
btnArea.addActionListener(this);
//设置窗体的大小,位置
this.pack();
this.setLocation(300,400);
//设置窗体可见
this.setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource()==btnGrith)
{
int a = Integer.parseInt (tfa.getText());
int b = Integer.parseInt (tfb.getText());

//1.1计算周长
double s =(2*(a+b));
tfGrith.setText(String.valueOf(s));


}
if (e.getActionCommand()=="面积")
{
//2.1把长和宽的值取回来
int a = Integer.parseInt (tfa.getText());
int b = Integer.parseInt (tfb.getText());

//tfa.getText()
//2.2计算面积

double area = (a*b);
//2.3把面积的值显示在对应的
tfArea.setText(String.valueOf(area));
}
}

}

class Test
{
public static void main (String args[])
{
new JuxingFrame ();
}
}

相关文档
最新文档