实现代码:
package game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Calculator implements ActionListener {//实现ActionListener接口,可以引用监听
private Frame Fm = new JFrame();
//创建按钮
private JButton quit = new JButton("退出");
private JButton Add = new JButton("+");
private JButton Sub = new JButton("-");
private JButton Mul = new JButton("x");
private JButton Div = new JButton("÷");
private Label Addend = new Label("操作数1");
private Label Augend = new Label("操作数2");
private Label Headline = new Label("简易计算器");
private Label Result = new Label("结果");
//创建文本框
private TextField fieldAdd = new TextField();
private TextField fieldAug = new TextField();
private TextField areaResult = new TextField();
private Font Ft = new Font("宋体",Font.BOLD,18);//设置字体,颜色,型号
private EventObject e;
public Calculator() {
//设置标题
Fm.setTitle("计算器plus 8.0版本");
Fm.setLayout(null);
Fm.setBounds(200,300,400,300);
Headline.setForeground(Color.MAGENTA);
Headline.setBounds(140,20,120,30);
Fm.add(Headline);//标签--操作数1
Headline.setFont(new Font("宋体",Font.BOLD,18));//字体类型
Addend.setForeground(Color.blue);
Addend.setBackground(Color.LIGHT_GRAY);//浅灰色
Addend.setBounds(20,70,80,30);
Fm.add(Addend);//标签--操作数1
Addend.setFont(Ft);//字体类型
fieldAdd.setBounds(110,70,140,30);
Fm.add(fieldAdd);//输入区--操作数1
fieldAdd.setFont(Ft);//字体类型
Augend.setForeground(Color.blue);
Augend.setBackground(Color.LIGHT_GRAY);
Augend.setBounds(20,120,80,30);
Fm.add(Augend);
Augend.setFont(new Font("宋体",Font.BOLD,18));
fieldAug.setBounds(110,120,140,30);
Fm.add(fieldAug);
fieldAug.setFont(Ft);
Result.setForeground(Color.blue);
Result.setBackground(Color.LIGHT_GRAY);
Result.setBounds(20,170,60,30);
Fm.add(Result);
Result.setFont(Ft);
areaResult.setBounds(110,170,1140,30);
Fm.add(areaResult);
areaResult.setFont(Ft);
Add.setForeground(Color.RED);
Add.setBounds(260,50,100,30);
Fm.add(Add);
Add.addActionListener(this);
Sub.setForeground(Color.RED);
Sub.setBounds(260,90,100,30);
Fm.add(Sub);
Sub.addActionListener(this);
Mul.setForeground(Color.RED);
Mul.setBounds(260,130,100,30);
Fm.add(Mul);
Mul.addActionListener(this);
Div.setForeground(Color.RED);
Div.setBounds(260,170,100,30);
Fm.add(Div);
Div.addActionListener(this);
quit.setForeground(Color.RED);
quit.setBounds(260,210,100,30);
Fm.add(quit);
quit.addActionListener(this);
Fm.setVisible(true);//显示窗口
}
//监听
public void actionPerformed(ActionEvent e) {
//ActionEvent是一个事件传入的e是该事件的对象
double a,b;
a = Double.parseDouble(fieldAdd.getText());
b = Double.parseDouble(fieldAug.getText());
try {
if(e.getSource() == quit) {
System.exit(0);
}else if(e.getSource() == Add) {
areaResult.setText(String.valueOf(a+b));
}else if(e.getSource() == Sub) {
areaResult.setText(String.valueOf(a-b));
}else if(e.getSource() == Mul) {
areaResult.setText(String.valueOf(a*b));
}else if(e.getSource() == Div) {
areaResult.setText(String.valueOf(a/b));
}
}catch(Exception Ex) {
areaResult.setText("出错了:"+Ex);
}
}
public static void main(String args[]) {
Calculator pr = new Calculator();
}
}
运行结果:
欢迎各路神仙大佬指教!