第4次作业类测试代码+019+李悦洲
Posted lyz007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第4次作业类测试代码+019+李悦洲相关的知识,希望对你有一定的参考价值。
类图:
代码:
package swingDesign; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Triangle extends JFrame { private JPanel contentPane; private JTextField aside; private JTextField bside; private JTextField cside; private JTextField resulttext; private JTextField square; private JTextField length; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Triangle frame = new Triangle(); frame.getContentPane().setLayout(null); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Triangle() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setLayout(null); setContentPane(contentPane); JLabel lab = new JLabel("\\u8BF7\\u8F93\\u5165\\u4E09\\u89D2\\u5F62\\u7684\\u4E09\\u6761\\u8FB9\\uFF1A"); lab.setFont(new Font("黑体", Font.PLAIN, 14)); contentPane.add(lab); lab.setBounds(129, 10, 196, 32); JLabel laba = new JLabel("a:"); laba.setFont(new Font("黑体", Font.PLAIN, 14)); contentPane.add(laba); laba.setBounds(64, 73, 36, 23); JLabel labb = new JLabel("b:"); labb.setFont(new Font("黑体", Font.PLAIN, 14)); labb.setBounds(187, 73, 36, 23); contentPane.add(labb); JLabel labc = new JLabel("c:"); labc.setFont(new Font("黑体", Font.PLAIN, 14)); labc.setBounds(307, 73, 36, 23); contentPane.add(labc); aside = new JTextField(); aside.setBounds(84, 73, 66, 21); contentPane.add(aside); aside.setColumns(10); bside = new JTextField(); bside.setBounds(206, 73, 66, 21); contentPane.add(bside); bside.setColumns(10); cside = new JTextField(); cside.setBounds(329, 73, 66, 21); contentPane.add(cside); cside.setColumns(10); final JButton btnOk = new JButton("OK"); btnOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getSource()==btnOk) { Caculate cacu =new Caculate(); int a=0,b=0,c=0; String A,B,C; A=aside.getText().toString(); B=bside.getText().toString(); C=cside.getText().toString(); //判断是否为数字 if(A.matches("[0-9]+") && B.matches("[0-9]+") && C.matches("[0-9]+")) { a = Integer.parseInt(A); b = Integer.parseInt(B); c = Integer.parseInt(C); resulttext.setText(cacu.triangle(a,b,c)); square.setText(cacu.square(a,b,c)+""); length.setText(cacu.length(a,b,c)+""); } else { resulttext.setText("输入有误,请重新输入"); } } } }); btnOk.setFont(new Font("宋体", Font.PLAIN, 14)); btnOk.setBounds(118, 119, 79, 23); contentPane.add(btnOk); final JButton btnCancel = new JButton("CANCEL"); btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getSource()==btnCancel) { aside.setText(""); bside.setText(""); cside.setText(""); resulttext.setText(""); square.setText(""); length.setText(""); JOptionPane.showMessageDialog(null,"已重置"); } } }); btnCancel.setFont(new Font("宋体", Font.PLAIN, 14)); btnCancel.setBounds(246, 119, 79, 23); contentPane.add(btnCancel); JLabel label = new JLabel("\\u8FD9\\u4E2A\\u4E09\\u89D2\\u5F62\\u662F:"); label.setFont(new Font("宋体", Font.PLAIN, 14)); label.setBounds(84, 164, 113, 23); contentPane.add(label); resulttext = new JTextField(); resulttext.setBounds(183, 165, 136, 21); contentPane.add(resulttext); resulttext.setColumns(10); JLabel label_1 = new JLabel("\\u9762\\u79EF\\u662F:"); label_1.setFont(new Font("宋体", Font.PLAIN, 14)); label_1.setBounds(64, 208, 54, 15); contentPane.add(label_1); square = new JTextField(); square.setBounds(111, 205, 66, 21); contentPane.add(square); square.setColumns(10); JLabel label_2 = new JLabel("\\u5468\\u957F\\u662F:"); label_2.setFont(new Font("宋体", Font.PLAIN, 14)); label_2.setBounds(233, 208, 54, 15); contentPane.add(label_2); length = new JTextField(); length.setBounds(283, 205, 66, 21); contentPane.add(length); length.setColumns(10); } }
package swingDesign; import java.text.DecimalFormat; public class Caculate { public final static int DENGBIAN = 0; public final static int DENGYAO = 1; public final static int ZHIJIAO = 2; public final static int YIBAN = 3; private int a; private int b; private int c; protected int checkType()//判断三角形种类 { if(a==b&&a==c&&b==c) return DENGBIAN; if(a==b||a==c||b==c) return DENGYAO; if(a*a+b*b==c*c||a*a==b*b+c*c||b*b==a*a+c*c) return ZHIJIAO; return YIBAN; } protected boolean checkTheOne()//检查范围条件 { if((a>=1&&a<=100)&&(b>=1&&b<=100)&&(c>=1&&c<=100)) return true; else return false; } protected boolean checkTheTwo()//检查是否组成三角形 { if((a<(b+c))&&(b<(a+c))&&(c<(a+b))) return true; else return false; } public String triangle( int a, int b, int c)//题目要求的函数 { String ch = null; this.a = a; this.b = b; this.c = c; if(!checkTheOne()) { ch = "边的值不在范围内"; } else if(!checkTheTwo()) { ch = "不构成三角形"; } else { switch(checkType()) { case DENGBIAN: ch = "等边三角形";break; case DENGYAO: ch = "等腰三角形";break; case ZHIJIAO: ch = "直角三角形";break; case YIBAN: ch = "一般三角形";break; } } return ch; } public String square(int a,int b,int c) { String temp = triangle(a, b, c); DecimalFormat df = new DecimalFormat("#.00"); float result=0; float p; String mianjiresult = null; if(!temp.equals("边的值不在范围内") &&!temp.equals("不构成三角形")) { p=(float) ((a+b+c)/2.0); result=(float) (Math.sqrt(p*(p-a)*(p-b)*(p-c))); mianjiresult=df.format(result); } return mianjiresult; } public int length(int a,int b,int c) { String temp = triangle(a, b, c); int result=0; if(!temp.equals("边的值不在范围内") &&!temp.equals("不构成三角形")) { result=a+b+c; } return result; } }
结果:
以上是关于第4次作业类测试代码+019+李悦洲的主要内容,如果未能解决你的问题,请参考以下文章