JAVA-复数计算器

使用java组件做一个复数计算器

首先,建三个类:一个组件类,一个类(将文本框与字符封装起来),一个复数类。下面是组件类的代码:

package b;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class JComplex extends JFrame implements ActionListener{
//设置组件
public JComboBox<String> combox_add,combox_minus;
private static String[] add={"+","-"};
private static String[] minus={"+","-"};
private JButton button_equal;
//建面板类的对象
ComJPanel x1=new ComJPanel();
ComJPanel x2=new ComJPanel();
ComJPanel x3=new ComJPanel();
ComJPanel result=new ComJPanel();

public JComplex()
{
super("复数运算器");
this.setLayout(null);
this.setSize(400,220);
this.setLocationRelativeTo(null);
//添加组件
this.add(this.combox_add=new JComboBox<String>(JComplex.add));
this.add(this.combox_minus=new JComboBox<String>(JComplex.minus));
this.add(this.button_equal=new JButton("="));
this.add(x1);
this.add(x2);
this.add(x3);
this.add(result);
//设置组件的距离和大小
combox_add.setBounds(20, 70, 60, 30);
combox_minus.setBounds(20, 110, 60, 30);
button_equal.setBounds(20, 150, 60, 30);
x1.setBounds(100, 5, 300, 50);
x2.setBounds(100, 50, 300, 50);
x3.setBounds(100, 100, 300, 50);
result.setBounds(100, 150, 300, 50);
//设置动作监听
combox_add.addActionListener(this);
combox_minus.addActionListener(this);
button_equal.addActionListener(this);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{//获取文本框的内容
String s1=x1.text_real.getText();
String s2=x1.text_i.getText();
String s3=x2.text_real.getText();
String s4=x2.text_i.getText();
String s5=x3.text_real.getText();
String s6=x3.text_i.getText();
//建复数类的对象
Complex c1=new Complex(s1,s2);
Complex c2=new Complex(s3,s4);
Complex c3=new Complex(s5,s6);
//动作响应
if(e.getSource().equals(combox_add))
{
if(combox_add.getSelectedItem().equals("+"))
combox_add.setSelectedItem("+");
else if(combox_add.getSelectedItem().equals("-"))
combox_add.getSelectedItem().equals("-");
}
if(e.getSource().equals(combox_minus))
{
if(combox_minus.getSelectedItem().equals("+"))
combox_minus.setSelectedItem("+");
else if(combox_minus.getSelectedItem().equals("-"))
combox_minus.setSelectedItem("-");
}
if(e.getSource().equals(button_equal))
{
if(combox_add.getSelectedItem().equals("+"))
{
c1.add(c2);
}
else if(combox_add.getSelectedItem().equals("-"))
{
c1.minus(c2);
}
if(combox_minus.getSelectedItem().equals("+"))
{
c1.add(c3);
}
else if(combox_minus.getSelectedItem().equals("-"))
{
c1.minus(c3);
}
}
result.text_real.setText(Double.toString(c1.x));
result.text_i.setText(Double.toString(c1.y));
}
public static void main(String []args)
{
new JComplex();
}

}
class ComJPanel extends JPanel {
private JLabel label_add,label_i;
JTextField text_real,text_i;
String s1,s2;

public ComJPanel()
{//面板类
this.setLayout(null);
this.add(this.text_real=new JTextField());
this.add(this.label_add=new JLabel("+"));
this.add(this.text_i=new JTextField());
this.add(this.label_i=new JLabel("i"));

text_real.setBounds(20,0,100,30);
text_i.setBounds(150,0,100, 30);
label_add.setBounds(130,0,20, 25);
label_i.setBounds(260,0,20, 25);

}

}

复数类就是简单的复数类,就不贴代码啦

posted @ 2019-11-27 22:02  邱s  阅读(1088)  评论(0编辑  收藏  举报