1实验要求编写一个应用程序有一个标题为计算的窗口窗口的布局为FlowLayout布局。设计四个按钮分别命名为加、差、积、、除;另外窗口中还有三个文本框。单击相应的按钮将两个文本框的数字做运算在第三个文本框中显示结果。2实验指导public class SetCalculator extends Frame implements ActionListener static SetCalculato
import java.awt.; import java.awt.event.;
public class SetCalculator extends Frame implements ActionListener { static SetCalculator frm = new SetCalculator(); static Button btn1,btn2,btn3,btn4; static TextField txt1,txt2,txt3;
public static void main(String[] args) {
frm.setTitle("计算器");
frm.setBounds(100,100,300,150);
frm.setLayout(new FlowLayout());
frm.setVisible(true);
btn1 = new Button("加");
btn2 = new Button("减");
btn3 = new Button("乘");
btn4 = new Button("除");
btn1.addActionListener(frm);
btn2.addActionListener(frm);
btn3.addActionListener(frm);
btn4.addActionListener(frm);
txt1 = new TextField(10);
txt2 = new TextField(10);
txt3 = new TextField(10);
txt3.setEditable(false);
frm.add(txt1);
frm.add(new Label("+"));
frm.add(txt2);
frm.add(new Label("="));
frm.add(txt3);
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
frm.add(btn4);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1) {
int sum;
sum=Integer.parseInt(txt1.getText())+Integer.parseInt(txt2.getText());
String Sum=String.valueOf(sum);
txt3.setText(Sum);
}
if(e.getSource()==btn2) {
int diff;
diff=Integer.parseInt(txt1.getText())-Integer.parseInt(txt2.getText());
String Diff=String.valueOf(diff);
txt3.setText(Diff);
}
if(e.getSource()==btn3) {
int product;
product=Integer.parseInt(txt1.getText())*Integer.parseInt(txt2.getText());
String Product=String.valueOf(product);
txt3.setText(Product);
}
if(e.getSource()==btn4) {
double quotient;
quotient=(double)Integer.parseInt(txt1.getText())/Integer.parseInt(txt2.getText());
String Quotient=String.valueOf(quotient);
txt3.setText(Quotient);
}
}
原文地址: http://www.cveoy.top/t/topic/hjHg 著作权归作者所有。请勿转载和采集!