zoukankan      html  css  js  c++  java
  • 梯形计算

    
    

    Example9_15.java


    1
    public class Example9_15 { 2 public static void main(String[] args) { 3 WindowTriangle win=new WindowTriangle(); 4 win.setTitle("使用MVC结构"); 5 win.setBounds(100,100,420,260); 6 } 7 } 8
    
    

    WindowTriangle.java


    1
    import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JPanel; 8 import javax.swing.JTextArea; 9 public class WindowTriangle extends JFrame implements ActionListener{ 10 Triangle triangle; 11 JTextField textA,textB,textC; 12 JTextArea showArea; 13 JButton controlButton; 14 WindowTriangle(){ 15 init(); 16 setVisible(true); 17 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 } 19 void init() { 20 triangle=new Triangle(); 21 textA=new JTextField(3); 22 textB=new JTextField(3); 23 textC=new JTextField(3); 24 showArea=new JTextArea(); 25 controlButton=new JButton("计算面积"); 26 JPanel pNorth=new JPanel(); 27 pNorth.add(new JLabel("上底A:")); 28 pNorth.add(textA); 29 pNorth.add(new JLabel("高B:")); 30 pNorth.add(textB); 31 pNorth.add(new JLabel("下底C:")); 32 pNorth.add(textC); 33 pNorth.add(controlButton); 34 controlButton.addActionListener(this); 35 add(pNorth, BorderLayout.NORTH); 36 add(new JScrollPane(showArea),BorderLayout.CENTER); 37 } 38 public void actionPerformed(ActionEvent e) { 39 try { 40 double a=Double.parseDouble(textA.getText().trim()); 41 double b=Double.parseDouble(textB.getText().trim()); 42 double c=Double.parseDouble(textC.getText().trim()); 43 triangle.setA(a); 44 triangle.setB(b); 45 triangle.setC(c); 46 String area=triangle.getArea(); 47 showArea.append("梯形"+a+","+b+","+c+"的面积:"); 48 showArea.append(area+" "); 49 } 50 catch(Exception ex) { 51 showArea.append(" "+ex+" "); 52 } 53 } 54 }
    
    

    Triangle.java


    1
    public class Triangle { 2 double sideA,sideB,sideC,area; 3 boolean isTriange; 4 public String getArea() { 5 if(isTriange) { 6 double p=((sideA+sideC)*sideB)/2.0; 7 return String.valueOf(p); 8 }else { 9 return "无法计算面积"; 10 } 11 } 12 public void setA(double a) { 13 sideA=a; 14 if(sideA+sideC>sideC&&sideA+sideC>sideB&&sideB+sideC>sideA) 15 isTriange=true; 16 else 17 isTriange=false; 18 } 19 public void setB(double b) { 20 sideB=b; 21 if(sideA+sideC>sideC&&sideA+sideC>sideB&&sideB+sideC>sideA) 22 isTriange=true; 23 else 24 isTriange=false; 25 } 26 public void setC(double c) { 27 sideC=c; 28 if(sideA+sideC>sideC&&sideA+sideC>sideB&&sideB+sideC>sideA) 29 isTriange=true; 30 else 31 isTriange=false; 32 } 33 }

  • 相关阅读:
    实用画刷填充图形
    PDA连接远程数据库的三种解决方案
    图形编程入门之如何创建用于绘制的 Graphics 对象
    使用渐变画笔填充形状
    使用钢笔绘制线条和形状
    How to: Display a Gradient Fill
    实现防火墙的“电话已关机”,“此号码已停机”,“号码不存在”等提示音的方法
    MSDN课程
    android振动器
    android视频播放
  • 原文地址:https://www.cnblogs.com/20190308-zlz/p/13175752.html
Copyright © 2011-2022 走看看