java GUI复数计算器
时间: 2023-11-20 19:56:40 浏览: 43
以下是一个Java实现的GUI复数计算器的例子:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComplexCalculator extends JFrame implements ActionListener {
private JTextField jtf1, jtf2, jtf3, jtf4;
private JButton jb1, jb2, jb3, jb4;
public ComplexCalculator() {
super("复数计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 4));
JLabel jl1 = new JLabel("实部1:");
add(jl1);
jtf1 = new JTextField(10);
add(jtf1);
JLabel jl2 = new JLabel("虚部1:");
add(jl2);
jtf2 = new JTextField(10);
add(jtf2);
JLabel jl3 = new JLabel("实部2:");
add(jl3);
jtf3 = new JTextField(10);
add(jtf3);
JLabel jl4 = new JLabel("虚部2:");
add(jl4);
jtf4 = new JTextField(10);
add(jtf4);
jb1 = new JButton("+");
add(jb1);
jb1.addActionListener(this);
jb2 = new JButton("-");
add(jb2);
jb2.addActionListener(this);
jb3 = new JButton("*");
add(jb3);
jb3.addActionListener(this);
jb4 = new JButton("/");
add(jb4);
jb4.addActionListener(this);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
double a1 = Double.parseDouble(jtf1.getText());
double b1 = Double.parseDouble(jtf2.getText());
double a2 = Double.parseDouble(jtf3.getText());
double b2 = Double.parseDouble(jtf4.getText());
ComplexNumber x = new ComplexNumber(a1, b1);
ComplexNumber y = new ComplexNumber(a2, b2);
if (e.getSource() == jb1) {
ComplexNumber z = x.add(y);
JOptionPane.showMessageDialog(null, "结果为:" + z.toString());
} else if (e.getSource() == jb2) {
ComplexNumber z = x.subtract(y);
JOptionPane.showMessageDialog(null, "结果为:" + z.toString());
} else if (e.getSource() == jb3) {
ComplexNumber z = x.multiply(y);
JOptionPane.showMessageDialog(null, "结果为:" + z.toString());
} else if (e.getSource() == jb4) {
ComplexNumber z = x.divide(y);
JOptionPane.showMessageDialog(null, "结果为:" + z.toString());
}
}
public static void main(String[] args) {
new ComplexCalculator();
}
}
class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber add(ComplexNumber other) {
double real = this.real + other.real;
double imaginary = this.imaginary + other.imaginary;
return new ComplexNumber(real, imaginary);
}
public ComplexNumber subtract(ComplexNumber other) {
double real = this.real - other.real;
double imaginary = this.imaginary - other.imaginary;
return new ComplexNumber(real, imaginary);
}
public ComplexNumber multiply(ComplexNumber other) {
double real = this.real * other.real - this.imaginary * other.imaginary;
double imaginary = this.real * other.imaginary + this.imaginary * other.real;
return new ComplexNumber(real, imaginary);
}
public ComplexNumber divide(ComplexNumber other) {
double denominator = other.real * other.real + other.imaginary * other.imaginary;
double real = (this.real * other.real + this.imaginary * other.imaginary) / denominator;
double imaginary = (this.imaginary * other.real - this.real * other.imaginary) / denominator;
return new ComplexNumber(real, imaginary);
}
public String toString() {
if (imaginary == 0) {
return Double.toString(real);
} else if (real == 0) {
return Double.toString(imaginary) + "i";
} else if (imaginary < 0) {
return Double.toString(real) + "-" + Double.toString(-imaginary) + "i";
} else {
return Double.toString(real) + "+" + Double.toString(imaginary) + "i";
}
}
}
```