类测试代码的具体要求如下:
设计三角形完整程序
已经完成的方法是: String triangle(int a,int b,int c)
现在要求继续增加新的功能:
建立界面,至少包含以下元素,但不限于此:
完成面积的方法:float triangleArea(int a,int b,int c) ,完成周长的方法:int perimeter(int a,int b,int c)
一、类图
二、功能界面
1
2
3
4
5
6
三、代码:
import java.awt.EventQueue; import java.awt.Color; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.Font; import javax.swing.UIManager; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JLabel; public class Jframe { private JFrame jframe; private static TriangleTest text; private JTextField texta; private JTextField textb; private JTextField textc; private JTextField perimeter; private JTextField number; private JTextField outarea; public void Triangle() { jframe = new JFrame(); jframe.setForeground(Color.green); jframe.setTitle("欢迎使用三角形管理系统"); jframe.setBounds(500, 350, 500, 430); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jpanel = new JPanel(); jpanel.setBounds(50, 50, 400, 280); jframe.getContentPane().add(jpanel, BorderLayout.CENTER); JLabel input = new JLabel("请输入三角形的三条边:"); jpanel.add(input); JPanel jp = new JPanel(); jpanel.add(jp); JLabel jpa = new JLabel("a:"); jp.add(jpa); texta = new JTextField(); jp.add(texta); texta.setColumns(5); JLabel jpb = new JLabel("b:"); jp.add(jpb); textb = new JTextField(); jp.add(textb); textb.setColumns(5); JLabel jpc = new JLabel("c:"); jp.add(jpc); textc = new JTextField(); jp.add(textc); textc.setColumns(5); JPanel bto = new JPanel(); bto.setSize(600, 90); jpanel.add(bto); final JButton btok = new JButton("OK"); bto.add(btok); final JButton btc = new JButton("Cancel"); bto.add(btc); JPanel outpanel = new JPanel(); jpanel.add(outpanel); JLabel output = new JLabel(); output.setText("这个三角形是:"); number = new JTextField(); number.setFont(new Font("宋体", Font.PLAIN, 15)); outpanel.add(number); number.setEditable(false); number.setColumns(18); JPanel jpout = new JPanel(); jpanel.add(jpout); JLabel outare = new JLabel(); jpout.add(outare); outare.setText("面积为:"); outarea = new JTextField(); outarea.setEditable(false); jpout.add(outarea); outarea.setColumns(9); JLabel outper = new JLabel(); jpout.add(outper); outper.setText("周长为:"); perimeter = new JTextField(); perimeter.setEditable(false); jpout.add(perimeter); btc.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent event) { Object object = event.getSource(); if(object == btc){ texta.setText(""); textb.setText(""); textc.setText(""); number.setText(""); outarea.setText(""); perimeter.setText(""); } } }); btok.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent event) { Object object = event.getSource(); if(object == btok){ int aa,bb,cc; //获取文本 String a=texta.getText(); String b=textb.getText(); String c=textc.getText(); //p if(text.IsNotInt(a)&&text.IsNotInt(b)&&text.IsNotInt(c)) { aa = Integer.parseInt(a); bb = Integer.parseInt(b); cc = Integer.parseInt(c); // if (!(TriangleTest.triangle(aa, bb, cc)).equals("您输入的边的值不在范围内")&&!(TriangleTest.triangle(aa, bb, cc)).equals("不能构成三角形")) { String pe=String.valueOf(text.perimeter(aa, bb, cc)); String ar=String.valueOf(text.triangleArea(aa, bb, cc)); String re=TriangleTest.triangle(aa, bb, cc); perimeter.setText(pe); outarea.setText(ar); number.setText(re); } else{ // String result=TriangleTest.triangle(aa, bb, cc); number.setText(result); outarea.setText(""); perimeter.setText(""); perimeter.setText(""); } } else{ // JOptionPane.showMessageDialog(null, "您的输入有误,请重请输入"); new JFrame(); texta.setText(""); textb.setText(""); textc.setText(""); number.setText(""); outarea.setText(""); perimeter.setText(""); } } } }); } public static void main(String[] args) { text=new TriangleTest(); EventQueue.invokeLater(new Runnable() { public void run() { Jframe window = new Jframe(); // window.jframe.setVisible(true); } }); } public Jframe() { Triangle(); } }