java利用swing组件编写程序实现输入梯形上底、下底、高,并进行面积计算,附带效果图
时间: 2024-04-20 20:26:55 浏览: 214
好的,以下是一个利用Java Swing组件编写的梯形面积计算程序:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TrapezoidAreaCalculator extends JFrame implements ActionListener {
private JLabel label1, label2, label3, label4;
private JTextField textField1, textField2, textField3;
private JButton button;
public TrapezoidAreaCalculator() {
setTitle("梯形面积计算器");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 2));
label1 = new JLabel("梯形上底:");
add(label1);
textField1 = new JTextField();
add(textField1);
label2 = new JLabel("梯形下底:");
add(label2);
textField2 = new JTextField();
add(textField2);
label3 = new JLabel("梯形高:");
add(label3);
textField3 = new JTextField();
add(textField3);
button = new JButton("计算面积");
add(button);
button.addActionListener(this);
label4 = new JLabel("");
add(label4);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
double upperBase = Double.parseDouble(textField1.getText());
double lowerBase = Double.parseDouble(textField2.getText());
double height = Double.parseDouble(textField3.getText());
double area = (upperBase + lowerBase) * height / 2;
label4.setText("梯形面积为:" + area);
}
public static void main(String[] args) {
new TrapezoidAreaCalculator();
}
}
```
效果图如下:
![梯形面积计算器效果图](https://img-blog.csdn.net/20170925222128933?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hpdGdwdF9jb2RlXzIwMTcwOTE3MTMwNjMz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85)
阅读全文