帮我编写图形界面的Java Application程序,输入两个复数,输出复数之和、差、积和商。程序为两层架构
时间: 2024-12-17 12:38:28 浏览: 4
在Java中,创建一个图形用户界面(GUI)的Java Application程序来处理复数运算通常需要使用Swing或JavaFX库。这里是一个简单的示例,展示如何使用Swing框架来实现这个功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
class ComplexNumber extends JFrame {
private JTextField realPartAField, imagPartAField, realPartBField, imagPartBField;
private JTextField sumField, differenceField, productField, quotientField;
public ComplexNumber() {
setTitle("复数计算器");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(4, 2));
JLabel labelRealA = new JLabel("复数A实部:");
realPartAField = new JTextField();
JLabel labelImagA = new JLabel("虚部:");
imagPartAField = new JTextField();
JLabel labelRealB = new JLabel("复数B实部:");
realPartBField = new JTextField();
JLabel labelImagB = new JLabel("虚部:");
imagPartBField = new JTextField();
JButton calculateButton = new JButton("计算");
inputPanel.add(labelRealA);
inputPanel.add(realPartAField);
inputPanel.add(labelImagA);
inputPanel.add(imagPartAField);
inputPanel.add(labelRealB);
inputPanel.add(realPartBField);
inputPanel.add(labelImagB);
inputPanel.add(imagPartBField);
inputPanel.add(calculateButton);
JPanel resultPanel = new JPanel();
resultPanel.setLayout(new GridLayout(4, 1));
sumField = new JTextField("0 + 0i");
differenceField = new JTextField("0 + 0i");
productField = new JTextField("0 + 0i");
quotientField = new JTextField("0 + 0i");
resultPanel.add(new JLabel("和:"));
resultPanel.add(sumField);
resultPanel.add(new JLabel("差:"));
resultPanel.add(differenceField);
resultPanel.add(new JLabel("积:"));
resultPanel.add(productField);
resultPanel.add(new JLabel("商:"));
resultPanel.add(quotientField);
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JLabel("\n"));
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double realA = Double.parseDouble(realPartAField.getText());
double imagA = Double.parseDouble(imagPartAField.getText());
double realB = Double.parseDouble(realPartBField.getText());
double imagB = Double.parseDouble(imagPartBField.getText());
double sumRe = realA + realB;
double sumIm = imagA + imagB;
double differenceRe = realA - realB;
double differenceIm = imagA - imagB;
double productRe = realA * realB - imagA * imagB;
double productIm = realA * imagB + imagA * realB;
double quotientRe = (realA * realB + imagA * imagB) / Math.pow(realB, 2) + imagA * imagB / Math.pow(realB, 2);
double quotientIm = (imagA * realB - realA * imagB) / Math.pow(realB, 2);
sumField.setText(String.format("%.2f + %.2fi", sumRe, sumIm));
differenceField.setText(String.format("%.2f + %.2fi", differenceRe, differenceIm));
productField.setText(String.format("%.2f + %.2fi", productRe, productIm));
quotientField.setText(String.format("%.2f + %.2fi", quotientRe, quotientIm));
}
});
setLayout(new BorderLayout());
add(inputPanel, BorderLayout.NORTH);
add(resultPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
ComplexNumber complexApp = new ComplexNumber();
complexApp.setVisible(true);
});
}
}
```
当你运行这个程序,用户可以在文本框中输入复数的实部和虚部,然后点击“计算”按钮,就会看到结果出现在相应的字段中。
阅读全文