Java编写能完成复数运算的程序

Java编写能完成复数运算的程序

Java编写能完成复数运算的程序

题目简介:

Java编写能完成复数运算的程序-LMLPHP

整体分析:

Java编写能完成复数运算的程序-LMLPHP

界面分析:

Java编写能完成复数运算的程序-LMLPHP

实验代码:

package complex;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Font; public class Complex implements ActionListener { String s,s1,s2,s3,s4; //s为运算符号,s1,s3为两个虚数的实部,s2,s4为两个虚数的虚部
double a,b,c,d,ans1,ans2; //对应的字符串转化为数值后:a,c为两个虚数的实部,b,d为两个虚数的虚部
//ans1,ans2为计算结果 private JFrame frame;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JButton button; public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Complex window = new Complex(); //插件自动生成对象,并设置可见性为可见
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /*GUi插件自动生成*/
public Complex() {
frame = new JFrame();
frame.setBounds(100, 100, 709, 377);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 1, 0, 0)); JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(20, 20, 20, 20));
frame.getContentPane().add(panel);
panel.setLayout(new GridLayout(4, 5, 20, 20));
/*public GridLayout(x,y,hgap,vgap) x,y为表格布局的行和列,hgap和vgap为表格元素间的间距*/ JLabel label = new JLabel("\u590D\u65701\uFF1A");
label.setFont(new Font("宋体", Font.BOLD, 19));
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label); textField_1 = new JTextField();
textField_1.setFont(new Font("宋体", Font.BOLD, 18));
textField_1.setHorizontalAlignment(SwingConstants.CENTER);
textField_1.setColumns(10);
panel.add(textField_1); JLabel label_1 = new JLabel("+");
label_1.setFont(new Font("宋体", Font.BOLD, 19));
label_1.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_1); textField_2 = new JTextField();
textField_2.setFont(new Font("宋体", Font.BOLD, 18));
textField_2.setHorizontalAlignment(SwingConstants.CENTER);
textField_2.setColumns(10);
panel.add(textField_2); JLabel label_2 = new JLabel("i");
label_2.setFont(new Font("宋体", Font.BOLD, 19));
label_2.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_2); JLabel label_3 = new JLabel("\u8FD0\u7B97\u7B26");
label_3.setFont(new Font("宋体", Font.BOLD, 19));
label_3.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_3); JLabel label_4 = new JLabel("");
label_4.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_4); textField = new JTextField();
textField.setFont(new Font("宋体", Font.BOLD, 19));
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setColumns(10);
panel.add(textField);
/*
* TextArea与TextField的最基本区别
* TextArea可以输入多行文本,
* TextField只能输入单行文本。
*/ JLabel label_5 = new JLabel("");
label_5.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_5); JLabel label_6 = new JLabel("");
panel.add(label_6); JLabel label_7 = new JLabel("\u590D\u65702\r\n");
label_7.setFont(new Font("宋体", Font.BOLD, 19));
label_7.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_7); textField_3 = new JTextField();
textField_3.setFont(new Font("宋体", Font.BOLD, 18));
textField_3.setHorizontalAlignment(SwingConstants.CENTER);
textField_3.setColumns(10);
panel.add(textField_3); JLabel label_8 = new JLabel("+");
label_8.setFont(new Font("宋体", Font.BOLD, 19));
label_8.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_8); textField_4 = new JTextField();
textField_4.setFont(new Font("宋体", Font.BOLD, 18));
textField_4.setHorizontalAlignment(SwingConstants.CENTER);
textField_4.setColumns(10);
panel.add(textField_4); JLabel label_9 = new JLabel("i");
label_9.setFont(new Font("宋体", Font.BOLD, 18));
label_9.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_9); button = new JButton("=");
button.setFont(new Font("宋体", Font.BOLD, 20));
panel.add(button);
button.addActionListener(this); textField_5 = new JTextField();
textField_5.setFont(new Font("宋体", Font.BOLD, 18));
textField_5.setHorizontalAlignment(SwingConstants.CENTER);
textField_5.setColumns(10);
panel.add(textField_5); JLabel label_11 = new JLabel("+");
label_11.setFont(new Font("宋体", Font.BOLD, 19));
label_11.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_11); textField_6 = new JTextField();
textField_6.setFont(new Font("宋体", Font.BOLD, 18));
textField_6.setHorizontalAlignment(SwingConstants.CENTER);
textField_6.setColumns(10);
panel.add(textField_6); JLabel label_12 = new JLabel("i");
label_12.setFont(new Font("宋体", Font.BOLD, 19));
label_12.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label_12);
} /*点击按钮事件*/
public void actionPerformed(ActionEvent e) {
//得到输入框的内容
s1 = textField_1.getText();
s2 = textField_2.getText();
s3 = textField_3.getText();
s4 = textField_4.getText();
s = textField.getText();
//字符串转化为数值
a = Double.parseDouble(s1);
b = Double.parseDouble(s2);
c = Double.parseDouble(s3);
d = Double.parseDouble(s4);
//选择运算类型:加,减,乘
switch (s) {
case "+":
ans1 = a + b;
ans2 = c + d;
break;
case "-":
ans1 = a - b;
ans2 = c - d;
break;
case "*":
ans1 = a*c - b*d;
ans2 = a*d + b*c;
break;
}
//设置文本框内容
textField_5.setText(ans1+"");
textField_6.setText(ans2+""); } }

实验结果:

Java编写能完成复数运算的程序-LMLPHP

05-11 11:31