如何用JAVA做一个两数相加的界面,并实现该功能
我平时上课没听课,现在期末要求现场做一个JAVA小程序:做一个两数相加的界面,并实现该功能。希望大家提供下代码,越简单越好,并且帮我详细解释一下代码相关的含义,让我便于理...
我平时上课没听课,现在期末要求现场做一个JAVA小程序:做一个两数相加的界面,并实现该功能。希望大家提供下代码,越简单越好,并且帮我详细解释一下代码相关的含义,让我便于理解和记忆
展开
2个回答
展开全部
加法和减法我都给你实现了 你感受下
import java.awt.*;
import java.awt.event.*;
public class jiafa extends WindowAdapter implements ActionListener {
Frame f;
TextField txt1;
TextField txt2;
TextField txt3;
Label lbl1;
Label lbl2;
Label lbl3;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
void init() {
f = new Frame("计算器");
lbl1 = new Label("x");
lbl2 = new Label("y");
lbl3 = new Label("结果");
txt1 = new TextField(10);
txt2 = new TextField(10);
txt3 = new TextField(10);
btn1 = new Button(" 求 和 ");
btn2 = new Button(" 求 差 ");
btn3 = new Button(" 清 除 ");
btn4 = new Button(" 退 出 ");
f.setLayout(new FlowLayout());
f.add(lbl1);
f.add(txt1);
f.add(lbl2);
f.add(txt2);
f.add(lbl3);
f.add(txt3);
f.add(btn1);
f.add(btn2);
f.add(btn3);
f.add(btn4);
f.setVisible(true);
f.pack();
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
f.addWindowListener(this);
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
String s1 = txt1.getText();
String s2 = txt2.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double d3 = d1 + d2;
String s3 = Double.toString(d3);
txt3.setText(s3);
}
if (e.getSource() == btn2) {
String s1 = txt1.getText();
String s2 = txt2.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double d3 = d1 - d2;
String s3 = Double.toString(d3);
txt3.setText(s3);
}
if (e.getSource() == btn3) {
String s = "";
txt1.setText(s);
txt2.setText(s);
txt3.setText(s);
}
if (e.getSource() == btn4) {
System.exit(0);
}
}
public static void main(String[] args) {
new jiafa().init();
}
}
import java.awt.*;
import java.awt.event.*;
public class jiafa extends WindowAdapter implements ActionListener {
Frame f;
TextField txt1;
TextField txt2;
TextField txt3;
Label lbl1;
Label lbl2;
Label lbl3;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
void init() {
f = new Frame("计算器");
lbl1 = new Label("x");
lbl2 = new Label("y");
lbl3 = new Label("结果");
txt1 = new TextField(10);
txt2 = new TextField(10);
txt3 = new TextField(10);
btn1 = new Button(" 求 和 ");
btn2 = new Button(" 求 差 ");
btn3 = new Button(" 清 除 ");
btn4 = new Button(" 退 出 ");
f.setLayout(new FlowLayout());
f.add(lbl1);
f.add(txt1);
f.add(lbl2);
f.add(txt2);
f.add(lbl3);
f.add(txt3);
f.add(btn1);
f.add(btn2);
f.add(btn3);
f.add(btn4);
f.setVisible(true);
f.pack();
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
f.addWindowListener(this);
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
String s1 = txt1.getText();
String s2 = txt2.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double d3 = d1 + d2;
String s3 = Double.toString(d3);
txt3.setText(s3);
}
if (e.getSource() == btn2) {
String s1 = txt1.getText();
String s2 = txt2.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double d3 = d1 - d2;
String s3 = Double.toString(d3);
txt3.setText(s3);
}
if (e.getSource() == btn3) {
String s = "";
txt1.setText(s);
txt2.setText(s);
txt3.setText(s);
}
if (e.getSource() == btn4) {
System.exit(0);
}
}
public static void main(String[] args) {
new jiafa().init();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询