import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; public class TestTriangle extends JFrame { JLabel title; JLabel first; JLabel second; JLabel third; JTextField firstL; JTextField secondL; JTextField thirdL; JButton button; public TestTriangle() { //JOptionPane joption = new JOptionPane(); title = new JLabel(); title.setText("三角形判定问题"); first = new JLabel(); first.setText("第一个参数"); firstL = new JTextField(); second = new JLabel(); second.setText("第二个参数"); secondL = new JTextField(); third = new JLabel(); third.setText("第三个参数"); thirdL = new JTextField(); button = new JButton(); button.setText("判断"); this.add(title); this.add(first); this.add(firstL); this.add(second); this.add(secondL); this.add(third); this.add(thirdL); this.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(firstL.getText()); int b = Integer.parseInt(secondL.getText()); int c = Integer.parseInt(thirdL.getText()); if (!(a + b <= c || a + c <= b || b + c <= a) && (a >= 1 && a <= 100) && (b >= 1 && b <= 100) && (c >= 1 && c <= 100)) { if (a == b && a == c) JOptionPane.showMessageDialog(null, "等边三角形"); else if (a == b || a == c || b == c) JOptionPane.showMessageDialog(null, "等腰三角形"); else JOptionPane.showMessageDialog(null , "一般三角形"); } else { if (a < 1 || a > 100) JOptionPane.showMessageDialog(null , "a不在范围内,不满足条件1,不能构成三角形"); else if (b < 1 || b > 100) JOptionPane.showMessageDialog(null , "b不在范围内,不满足条件2,不能构成三角形"); else if (c < 1 || c > 100) JOptionPane.showMessageDialog(null , "c不在范围内,不满足条件3,不能构成三角形"); else if (a >= b + c) JOptionPane.showMessageDialog(null , "不满足a<b+c,不能构成三角形"); else if (b >= a + c) JOptionPane.showMessageDialog(null , "不满足b<a+c,不能构成三角形"); else if (c >= b + a) JOptionPane.showMessageDialog(null , "不满足c<a+b,不能构成三角形"); } JOptionPane.showMessageDialog(null , "判断结束!"); } }); this.setLayout(new GridLayout()); this.setSize(600, 300); this.setLocation(500, 300); this.setVisible(true); } public static void main(String[] args) { TestTriangle testTriangle = new TestTriangle(); } }