• 第4次作业类测试代码+037+吴烨倩


    一、类图

    二、代码及界面

      1 package triangle;
      2 
      3 import java.awt.EventQueue;
      4 import javax.swing.JFrame;
      5 import javax.swing.JOptionPane;
      6 import javax.swing.JPanel;
      7 import java.awt.BorderLayout;
      8 import javax.swing.JButton;
      9 import javax.swing.JTextField;
     10 import java.awt.Font;
     11 import javax.swing.UIManager;
     12 import java.awt.event.MouseAdapter;
     13 import java.awt.event.MouseEvent;
     14 import javax.swing.JLabel;
     15 
     16 public class TtiangleFrame {
     17 
     18     private JFrame frm;
     19     private JTextField textField_b;
     20     private JTextField textField_a;
     21     private JTextField textField_c;
     22     private JTextField textField_is;
     23     private JTextField textField_area;
     24     private JTextField textField_peri;
     25 
     26     /**
     27      * Launch the application.
     28      */
     29     public static void main(String[] args) {
     30         EventQueue.invokeLater(new Runnable() {
     31             public void run() {
     32                 try {
     33                     TtiangleFrame window = new TtiangleFrame();
     34                     window.frm.setVisible(true);
     35                 } catch (Exception e) {
     36                     e.printStackTrace();
     37                 }
     38             }
     39         });
     40     }
     41 
     42     /**
     43      * Create the application.
     44      */
     45     public TtiangleFrame() {
     46         initialize();
     47     }
     48 
     49     /**
     50      * Initialize the contents of the frame.
     51      */
     52     private void initialize() {
     53         frm = new JFrame();
     54         frm.setBackground(UIManager.getColor("Button.background"));
     55         frm.setFont(new Font("Dialog", Font.PLAIN, 12));
     56         frm.setForeground(UIManager.getColor("Button.background"));
     57         frm.setTitle("Triangle");
     58         frm.setBounds(300, 200, 400, 250);
     59         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     60         
     61         JPanel panel = new JPanel();
     62         panel.setBounds(0, 0, 400, 250);
     63         panel.setBackground(UIManager.getColor("Button.background"));
     64         frm.getContentPane().add(panel, BorderLayout.CENTER);
     65         
     66         JLabel label_title = new JLabel("u8BF7u8F93u5165u4E09u89D2u5F62u7684u4E09u6761u8FB9uFF1A");
     67         label_title.setBackground(UIManager.getColor("Button.background"));
     68         label_title.setFont(new Font("宋体", Font.BOLD, 16));
     69         label_title.setBounds(20, 100, 200, 20);
     70         panel.add(label_title);
     71         
     72         JPanel panel_1 = new JPanel();
     73         panel.add(panel_1);
     74         
     75         JLabel lblA = new JLabel("a:");
     76         panel_1.add(lblA);
     77         lblA.setFont(new Font("宋体", Font.BOLD, 16));
     78         lblA.setSize(20, 20);
     79         
     80         textField_a = new JTextField();
     81         panel_1.add(textField_a);
     82         textField_a.setColumns(4);
     83         
     84         JLabel lblB = new JLabel("b:");
     85         panel_1.add(lblB);
     86         lblB.setFont(new Font("宋体", Font.BOLD, 16));
     87         lblB.setSize(20, 20);
     88         
     89         textField_b = new JTextField();
     90         panel_1.add(textField_b);
     91         textField_b.setColumns(4);
     92         
     93         JLabel lblC = new JLabel("c:");
     94         panel_1.add(lblC);
     95         lblC.setFont(new Font("宋体", Font.BOLD, 16));
     96         lblC.setSize(20, 20);
     97         
     98         textField_c = new JTextField();
     99         panel_1.add(textField_c);
    100         textField_c.setColumns(4);
    101         
    102         JPanel panel_btn = new JPanel();
    103         panel_btn.setSize(200, 80);
    104         panel.add(panel_btn);
    105         
    106         JButton btnOK = new JButton("OK");
    107         btnOK.setSize(50, 50);
    108         btnOK.setFont(new Font("宋体", Font.PLAIN, 15));
    109         panel_btn.add(btnOK);
    110         
    111         JButton btnCancle = new JButton("Cancel");
    112         btnOK.setSize(50, 50);
    113         btnCancle.setFont(new Font("宋体", Font.PLAIN, 15));
    114         panel_btn.add(btnCancle);
    115         btnCancle.addMouseListener(new MouseAdapter() {
    116             @Override
    117             public void mouseClicked(MouseEvent arg0) {
    118                 Object object = arg0.getSource();
    119                 if(object == btnCancle){
    120                     textField_a.setText("");
    121                     textField_b.setText("");
    122                     textField_c.setText("");
    123                     textField_is.setText("");
    124                     textField_area.setText("");
    125                     textField_peri.setText("");
    126                 }
    127             }
    128         });
    129         btnOK.addMouseListener(new MouseAdapter() {
    130             @Override
    131             public void mouseClicked(MouseEvent arg0) {
    132                 Object object = arg0.getSource();
    133                 if(object == btnOK){
    134                     int i_a,i_b,i_c;
    135                     String a=textField_a.getText();
    136                     String b=textField_b.getText();
    137                     String c=textField_c.getText();
    138                     if(Sort.IsNum(a)&&Sort.IsNum(b)&&Sort.IsNum(c))
    139                     {
    140                          i_a = Integer.parseInt(a);
    141                          i_b = Integer.parseInt(b);
    142                          i_c = Integer.parseInt(c);
    143                          if (!(Sort.triangle(i_a, i_b, i_c)).equals("边的值不在范围内")&&!(Sort.triangle(i_a, i_b, i_c)).equals("不构成三角形")) {
    144                          String result=Sort.triangle(i_a, i_b, i_c);
    145                          textField_is.setText(result);
    146                          String area=String.valueOf(Sort.triangleArea(i_a, i_b, i_c));
    147                          textField_area.setText(area);
    148                          String peri=String.valueOf(Sort.perimeter(i_a, i_b, i_c));
    149                          textField_peri.setText(peri);
    150                          }
    151                          else{
    152                              String result=Sort.triangle(i_a, i_b, i_c);
    153                              textField_is.setText(result);
    154                              textField_area.setText("");
    155                              textField_peri.setText("");
    156                              textField_peri.setText("");
    157                          }
    158                     }
    159                     else{
    160                         JOptionPane.showMessageDialog(null, "输入有误,请重请输入正确整数");
    161                         new JFrame();
    162                         textField_a.setText("");
    163                         textField_b.setText("");
    164                         textField_c.setText("");
    165                         textField_is.setText("");
    166                         textField_area.setText("");
    167                         textField_peri.setText("");
    168                     }
    169                 }
    170             }
    171         });
    172         
    173         JPanel panel_result1 = new JPanel();
    174         panel_result1.setBounds(0, 0, 400, 50);
    175         panel.add(panel_result1);
    176         
    177         JLabel j_is = new JLabel();
    178         panel_result1.add(j_is,BorderLayout.SOUTH);
    179         j_is.setFont(new Font("宋体", Font.PLAIN, 16));
    180         j_is.setBackground(UIManager.getColor("Button.background"));
    181         j_is.setText("u8FD9u4E2Au4E09u89D2u5F62u662FuFF1A");
    182         
    183         textField_is = new JTextField();
    184         textField_is.setFont(new Font("宋体", Font.PLAIN, 15));
    185         panel_result1.add(textField_is);
    186         textField_is.setEditable(false);
    187         textField_is.setColumns(15);
    188         
    189         JPanel panel_result2 = new JPanel();
    190         panel.add(panel_result2);
    191         
    192         JLabel j_area = new JLabel();
    193         panel_result2.add(j_area);
    194         j_area.setFont(new Font("宋体", Font.PLAIN, 15));
    195         j_area.setBackground(UIManager.getColor("Button.background"));
    196         j_area.setText("u9762u79EFu662FuFF1A");
    197         
    198         textField_area = new JTextField();
    199         textField_area.setFont(new Font("宋体", Font.PLAIN, 15));
    200         textField_area.setEditable(false);
    201         panel_result2.add(textField_area);
    202         textField_area.setColumns(8);
    203         
    204         JLabel j_peri = new JLabel();
    205         panel_result2.add(j_peri);
    206         j_peri.setFont(new Font("宋体", Font.PLAIN, 15));
    207         j_peri.setBackground(UIManager.getColor("Button.background"));
    208         j_peri.setText("u5468u957Fu662FuFF1A");
    209         
    210         textField_peri = new JTextField();
    211         textField_peri.setFont(new Font("宋体", Font.PLAIN, 15));
    212         textField_peri.setEditable(false);
    213         panel_result2.add(textField_peri);
    214         textField_peri.setColumns(8);
    215         
    216     }
    217 
    218 } 
     1 package triangle;
     2 
     3 import java.math.BigDecimal;
     4 
     5 public class Sort{
     6     public static String triangle(int a,int b,int c){    
     7         if(a<1 || a>100 ||b<1 || b>100|| c<1|| c>100)
     8         {
     9             return "边的值不在范围内";
    10         }
    11         else if(a>=b+c ||b>=a+c||c>=a+b)
    12         {
    13             return "不构成三角形";
    14         }
    15         else if(a==b &&b==c &&c==a){
    16             return "等边三角形";
    17         }
    18         else if(a==b ||b==c ||c==a){
    19             return "等腰三角形";
    20         }
    21         else if(a*a+b*b==c*c ||b*b+c*c==a*a ||c*c+a*a==b*b){
    22             return "直角三角形";
    23         }
    24         else {
    25             return "一般三角形";
    26         }
    27     }
    28     public static boolean IsNum(String str)
    29     {
    30      if(str==null)
    31       {
    32         return false;
    33        }
    34      else{
    35           try{
    36            @SuppressWarnings("unused")
    37             int num=Integer.valueOf(str);//把字符串强制转换为数字 
    38            return true;//是数字,返回true
    39            }catch (Exception e) {
    40              return false;
    41             }
    42        }
    43    }
    44     public static int perimeter(int a,int b,int c){
    45         return a+b+c;
    46     }
    47     public static float triangleArea(int a,int b,int c){
    48          double p = ((a+b+c)*0.5);
    49          float s= (float) (p*(p-a)*(p-b)*(p-c));
    50          float r=(float)Math.sqrt(s); 
    51          BigDecimal  bigDecimal  =   new BigDecimal(r);  
    52          float   f   =  bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();  
    53          return f;
    54     }
    55 }

    三、实验结果

  • 相关阅读:
    sql语句中的一些常用语法
    torch_12_BigGAN全文解读
    torch_12_dataset和dataLoader,Batchnormalization解读
    torch_11_BEGAN
    torch_11_风格迁移和cycleGAN
    torch_10_stackGAN-V2
    torch_09_DCGAN_注意的细节
    torch_09_GAN
    pytorch-04-激活函数
    torch_07_卷积神经网络案例分析
  • 原文地址:https://www.cnblogs.com/WuYeqian/p/6785661.html
Copyright © 2020-2023  润新知