java 程序设计 三角形求面积
import java.lang.*; import java.awt.*; import javax.swing.*; import java.awt.event.*;
class Trangle_GUI extends Frame { Label fist,second,third,area,result; TextField _fist,_second,_third; Button judge,calc;
Box box1,box2,box3,basebox; Trangle_GUI()
{ super("三角形面积计算"); Toolkit tk=getToolkit();
Dimension dim=tk.getScreenSize();
setBounds(dim.width/3,dim.height/3,300,220); setLayout(new FlowLayout()); setResizable(false);
setBackground(Color.LIGHT_GRAY);
fist=new Label("请输入第一个边:"); second=new Label("请输入第二个边:"); third=new Label("请输入第三个边:"); box1=Box.createVerticalBox();//创建列排列0 box1.add(fist); box1.add(second); box1.add(third);
_fist=new TextField(20); _second=new TextField(20); _third=new TextField(20);
box2=Box.createVerticalBox();//创建列排列1 box2.add(_fist); box2.add(_second); box2.add(_third);
basebox=Box.createVerticalBox();//创建列排列2 Panel mypanel=new Panel();//创建面板 mypanel.add(box1); mypanel.add(box2); basebox.add(mypanel);
area=new Label(" ==========该三角形的面积如下=========="); result=new Label(); basebox.add(area); basebox.add(result);
basebox.add(new Label(" ====================================")); box3=Box.createHorizontalBox();//创建行排列0
judge=new Button("判断是否能构成三角形"); judge.addActionListener(new Event());
calc=new Button(" 求三角形面积 "); calc.addActionListener(new Event()); box3.add(judge);
box3.add(Box.createHorizontalStrut(20)); box3.add(calc); basebox.add(box3); add(basebox); setVisible(true); validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e) { System.exit(0);} } ); }
class Event implements ActionListener
{ public void actionPerformed(ActionEvent e) { float a=Float.parseFloat(_fist.getText()); float b=Float.parseFloat(_second.getText()); float c=Float.parseFloat(_third.getText()); if(e.getSource()==judge)
{ if((a+b>c&&a+c>b&&b+c>a))
{ result.setText(" 能构成三角形!"); calc.setForeground(Color.black); calc.addActionListener(new Event()); } else
{ result.setText(" 不能构成三角形!"); ActionListener[] Ac=calc.getActionListeners(); calc.removeActionListener(Ac[0]);
calc.setForeground(Color.LIGHT_GRAY); } } else
{ double p=(a+b+c)/2;
p=M
ath.sqrt((p)*(p-a)*(p-b)*(p-c));
result.setText(" 所构成的三角形面积为:"+p); } } } }
public class Trangle
{ public static void main(String args[]) { Trangle_GUI my=new Trangle_GUI();} }
相关推荐: