一、程序效果图
二、运行结果
package java实训;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class SJ7_2 {
JLabel q1,q2,q3;
JTextField wenb1,wenb2,wenb3,wenb4;
JComboBox zxk;
JButton an1;
private class MyMonitor implements ActionListener{
public void actionPerformed(ActionEvent e) {
int s1=Integer.parseInt(wenb1.getText());
int s2=Integer.parseInt(wenb2.getText());
String n=(String)zxk.getSelectedItem();
if(n=="+")
{
wenb3.setText(""+(s1+s2));
}
else if(n=="-") {
wenb3.setText(""+(s1-s2));
}
else if(n=="*") {
wenb3.setText(""+(s1*s2));
}
else if(n=="/"){
wenb3.setText(""+(s1/s2));
}
}
}
public void myjf() {
JFrame f1=new JFrame("Calculated");
f1.setBounds(500, 300, 500, 600);
f1.setLayout(new FlowLayout(FlowLayout.LEFT,65,40));
q1=new JLabel("第一个数:");
q2=new JLabel("第二个数:"
+ " ");
q3=new JLabel("结果"
+ " ");
wenb1=new JTextField(30);
wenb2=new JTextField(30);
wenb3=new JTextField(30);
wenb3.setEditable(false);
String[] x= {"+","-","*","/"};
zxk=new JComboBox(x);
an1=new JButton("=");
an1.addActionListener(new MyMonitor());
f1.add(q1);
f1.add(wenb1);
f1.add(zxk);
f1.add(q2);
f1.add(wenb2);
f1.add(an1);
f1.add(q3);
f1.add(wenb3);
f1.setVisible(true);
}
public static void main(String[] args) {
SJ7_2 xx = new SJ7_2();
xx.myjf();
}
}