求一个Java源程序的代码
求一个java小程序的源代码有简单的图形界面,进行复数的加减乘运算,每一个数的虚实部分分开输入急求,谢谢!!不行啊,没用...
求一个java小程序的源代码
有简单的图形界面,进行复数的加减乘运算,每一个数的虚实部分分开输入
急求,谢谢!!
不行啊,没用 展开
有简单的图形界面,进行复数的加减乘运算,每一个数的虚实部分分开输入
急求,谢谢!!
不行啊,没用 展开
1个回答
展开全部
很粗糙的一个程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComplexNumber
{
public static void main(String[] args)
{
new Jframe().builtFrame();
}
}
class Jframe extends JFrame
{
JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6;
JLabel jb1, jb2, jb3;
Choice jc;
JButton jbtn;
String str = "+";
public Jframe()
{
}
public void builtFrame()
{
this.setLayout(new FlowLayout());
jtf1 = new JTextField(10);
jtf2 = new JTextField(10);
jtf3 = new JTextField(10);
jtf4 = new JTextField(10);
jtf5 = new JTextField(10);
jtf6 = new JTextField(10);
jbtn = new JButton("结果是");
jc = new Choice();
jc.add("+");
jc.add("-");
jc.add("*");
jb1 = new JLabel("第一个复数的实部和虚部");
jb2 = new JLabel("第二个复数的实部和虚部");
jb3 = new JLabel("得出的复数的实部和虚部");
add(jb1);
add(jtf1);
add(jtf2);
add(jc);
add(jb2);
add(jtf3);
add(jtf4);
add(jbtn);
add(jb3);
add(jtf5);
add(jtf6);
jc.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
Choice ch = (Choice) e.getItemSelectable();
if (ch.getSelectedItem() == "+")
{
str = "+";
} else if (ch.getSelectedItem() == "-")
{
str = "-";
} else if (ch.getSelectedItem() == "*")
{
str = "*";
}
}
});
jbtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String str1 = jtf1.getText().trim().toString();
String str2 = jtf2.getText().trim().toString();
String str3 = jtf3.getText().trim().toString();
String str4 = jtf4.getText().trim().toString();
// double d1 = Double.parseDouble(str1);
double d1 = Double.parseDouble(str1);
double d2 = Double.parseDouble(str2);
double d3 = Double.parseDouble(str3);
double d4 = Double.parseDouble(str4);
double d5 = 0;
double d6 = 0;
if (str == "+")
{
d5 = d1 + d3;
d6 = d2 + d4;
} else if (str == "-")
{
d5 = d1 - d3;
d6 = d2 - d4;
} else if (str == "*")
{
d5 = d1 * d3 - d2 * d4;
d6 = d1 * d4 + d2 * d3;
}
jtf5.setText(d5 + "");
jtf6.setText(d6 + "");
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 200);
setResizable(false);
setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComplexNumber
{
public static void main(String[] args)
{
new Jframe().builtFrame();
}
}
class Jframe extends JFrame
{
JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6;
JLabel jb1, jb2, jb3;
Choice jc;
JButton jbtn;
String str = "+";
public Jframe()
{
}
public void builtFrame()
{
this.setLayout(new FlowLayout());
jtf1 = new JTextField(10);
jtf2 = new JTextField(10);
jtf3 = new JTextField(10);
jtf4 = new JTextField(10);
jtf5 = new JTextField(10);
jtf6 = new JTextField(10);
jbtn = new JButton("结果是");
jc = new Choice();
jc.add("+");
jc.add("-");
jc.add("*");
jb1 = new JLabel("第一个复数的实部和虚部");
jb2 = new JLabel("第二个复数的实部和虚部");
jb3 = new JLabel("得出的复数的实部和虚部");
add(jb1);
add(jtf1);
add(jtf2);
add(jc);
add(jb2);
add(jtf3);
add(jtf4);
add(jbtn);
add(jb3);
add(jtf5);
add(jtf6);
jc.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
Choice ch = (Choice) e.getItemSelectable();
if (ch.getSelectedItem() == "+")
{
str = "+";
} else if (ch.getSelectedItem() == "-")
{
str = "-";
} else if (ch.getSelectedItem() == "*")
{
str = "*";
}
}
});
jbtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String str1 = jtf1.getText().trim().toString();
String str2 = jtf2.getText().trim().toString();
String str3 = jtf3.getText().trim().toString();
String str4 = jtf4.getText().trim().toString();
// double d1 = Double.parseDouble(str1);
double d1 = Double.parseDouble(str1);
double d2 = Double.parseDouble(str2);
double d3 = Double.parseDouble(str3);
double d4 = Double.parseDouble(str4);
double d5 = 0;
double d6 = 0;
if (str == "+")
{
d5 = d1 + d3;
d6 = d2 + d4;
} else if (str == "-")
{
d5 = d1 - d3;
d6 = d2 - d4;
} else if (str == "*")
{
d5 = d1 * d3 - d2 * d4;
d6 = d1 * d4 + d2 * d3;
}
jtf5.setText(d5 + "");
jtf6.setText(d6 + "");
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 200);
setResizable(false);
setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询