用java和JFrame编写一个输入三条边的大小并判断三角形成立与否的程序
时间: 2023-03-31 15:01:09 浏览: 106
您好,我可以回答这个问题。以下是Java和JFrame编写的输入三条边的大小并判断三角形成立与否的程序:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Triangle extends JFrame implements ActionListener {
private JTextField side1, side2, side3;
private JLabel result;
public Triangle() {
setTitle("判断三角形");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
panel.add(new JLabel("第一条边:"));
side1 = new JTextField(10);
panel.add(side1);
panel.add(new JLabel("第二条边:"));
side2 = new JTextField(10);
panel.add(side2);
panel.add(new JLabel("第三条边:"));
side3 = new JTextField(10);
panel.add(side3);
JButton button = new JButton("判断");
button.addActionListener(this);
panel.add(button);
result = new JLabel("");
panel.add(result);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
double a = Double.parseDouble(side1.getText());
double b = Double.parseDouble(side2.getText());
double c = Double.parseDouble(side3.getText());
if (a + b > c && a + c > b && b + c > a) {
result.setText("可以构成三角形");
} else {
result.setText("不能构成三角形");
}
}
public static void main(String[] args) {
new Triangle();
}
}
阅读全文