Java图形界面实现两个复数加减乘除
时间: 2023-07-11 07:50:45 浏览: 93
好的,我可以帮您解决这个问题。
首先,我们需要创建一个Java图形界面来获取两个复数的实部和虚部。您可以使用Java Swing来创建这个GUI。
下面是一个简单的GUI示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ComplexCalculator extends JFrame implements ActionListener {
private JLabel real1Label, imaginary1Label, real2Label, imaginary2Label;
private JTextField real1Field, imaginary1Field, real2Field, imaginary2Field;
private JButton addButton, subtractButton, multiplyButton, divideButton;
private JLabel resultLabel;
public ComplexCalculator() {
// 设置窗口标题和大小
setTitle("复数计算器");
setSize(400, 300);
// 创建GUI控件并添加到窗口
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(5, 5, 5, 5);
real1Label = new JLabel("第一个复数的实部:");
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(real1Label, constraints);
real1Field = new JTextField(10);
constraints.gridx = 1;
constraints.gridy = 0;
panel.add(real1Field, constraints);
imaginary1Label = new JLabel("第一个复数的虚部:");
constraints.gridx = 0;
constraints.gridy = 1;
panel.add(imaginary1Label, constraints);
imaginary1Field = new JTextField(10);
constraints.gridx = 1;
constraints.gridy = 1;
panel.add(imaginary1Field, constraints);
real2Label = new JLabel("第二个复数的实部:");
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(real2Label, constraints);
real2Field = new JTextField(10);
constraints.gridx = 1;
constraints.gridy = 2;
panel.add(real2Field, constraints);
imaginary2Label = new JLabel("第二个复数的虚部:");
constraints.gridx = 0;
constraints.gridy = 3;
panel.add(imaginary2Label, constraints);
imaginary2Field = new JTextField(10);
constraints.gridx = 1;
constraints.gridy = 3;
panel.add(imaginary2Field, constraints);
addButton = new JButton("加");
addButton.addActionListener(this);
constraints.gridx = 0;
constraints.gridy = 4;
panel.add(addButton, constraints);
subtractButton = new JButton("减");
subtractButton.addActionListener(this);
constraints.gridx = 1;
constraints.gridy = 4;
panel.add(subtractButton, constraints);
multiplyButton = new JButton("乘");
multiplyButton.addActionListener(this);
constraints.gridx = 0;
constraints.gridy = 5;
panel.add(multiplyButton, constraints);
divideButton = new JButton("除");
divideButton.addActionListener(this);
constraints.gridx = 1;
constraints.gridy = 5;
panel.add(divideButton, constraints);
resultLabel = new JLabel("");
constraints.gridx = 0;
constraints.gridy = 6;
constraints.gridwidth = 2;
panel.add(resultLabel, constraints);
add(panel);
// 设置窗口可见性和关闭操作
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
double real1 = Double.parseDouble(real1Field.getText());
double imaginary1 = Double.parseDouble(imaginary1Field.getText());
double real2 = Double.parseDouble(real2Field.getText());
double imaginary2 = Double.parseDouble(imaginary2Field.getText());
if (e.getSource() == addButton) {
ComplexNumber result = ComplexNumber.add(new ComplexNumber(real1, imaginary1), new ComplexNumber(real2, imaginary2));
resultLabel.setText("结果为:" + result.toString());
} else if (e.getSource() == subtractButton) {
ComplexNumber result = ComplexNumber.subtract(new ComplexNumber(real1, imaginary1), new ComplexNumber(real2, imaginary2));
resultLabel.setText("结果为:" + result.toString());
} else if (e.getSource() == multiplyButton) {
ComplexNumber result = ComplexNumber.multiply(new ComplexNumber(real1, imaginary1), new ComplexNumber(real2, imaginary2));
resultLabel.setText("结果为:" + result.toString());
} else if (e.getSource() == divideButton) {
ComplexNumber result = ComplexNumber.divide(new ComplexNumber(real1, imaginary1), new ComplexNumber(real2, imaginary2));
resultLabel.setText("结果为:" + result.toString());
}
}
public static void main(String[] args) {
ComplexCalculator calculator = new ComplexCalculator();
}
}
```
上述代码创建了一个Java Swing窗口,其中包含四个文本框用于输入两个复数的实部和虚部,以及四个按钮用于执行加、减、乘和除操作。当用户单击这些按钮时,将调用`actionPerformed`方法来执行相应的操作并显示结果。
接下来,您需要实现`ComplexNumber`类来执行复数加减乘除操作。下面是一个示例实现:
```java
public class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public static ComplexNumber add(ComplexNumber c1, ComplexNumber c2) {
return new ComplexNumber(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public static ComplexNumber subtract(ComplexNumber c1, ComplexNumber c2) {
return new ComplexNumber(c1.real - c2.real, c1.imaginary - c2.imaginary);
}
public static ComplexNumber multiply(ComplexNumber c1, ComplexNumber c2) {
double real = c1.real * c2.real - c1.imaginary * c2.imaginary;
double imaginary = c1.real * c2.imaginary + c1.imaginary * c2.real;
return new ComplexNumber(real, imaginary);
}
public static ComplexNumber divide(ComplexNumber c1, ComplexNumber c2) {
double denominator = c2.real * c2.real + c2.imaginary * c2.imaginary;
double real = (c1.real * c2.real + c1.imaginary * c2.imaginary) / denominator;
double imaginary = (c1.imaginary * c2.real - c1.real * c2.imaginary) / denominator;
return new ComplexNumber(real, imaginary);
}
public String toString() {
if (imaginary >= 0) {
return real + "+" + imaginary + "i";
} else {
return real + "" + imaginary + "i";
}
}
}
```
上述代码定义了一个`ComplexNumber`类,用于表示复数并执行加、减、乘和除操作。这个类包含一个构造函数和四个静态方法,分别用于执行加、减、乘和除操作。这个类还包含一个`toString`方法,用于将复数转换为字符串表示。
现在,您可以将这两个类组合在一起,创建一个Java应用程序,用于执行复数加减乘除操作。
阅读全文