import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalculatorB implements ActionListener {
private JFrame frame;
private JTextField field;
private JButton[] allButtons;
private JButton clearButton;
// 构造初始化 成员变量
public CalculatorB() {
frame = new JFrame("Calculator v1.1");
field = new JTextField(25);
allButtons = new JButton[16];
String str = "123+456-789*0.=/";
for (int i = 0; i < 16; i++) {
allButtons[i] = new JButton(str.substring(i, i + 1));
}
clearButton = new JButton("CLEAR");
// 调用界面的初始化方法
init();
// 调用设置文本框的字体和颜色 方法
setFontAndColor();
addEventHandler();
}
public void addEventHandler() {
for (int i = 0; i < allButtons.length; i++) {
allButtons[i].addActionListener(this);
clearButton.addActionListener(this);
}
}
private String op = null;
private String m = null;
private String n = null;
private boolean cls = false;
private void setResult() {
if (field.getText().equals("")) {
cls = true;
m = null;
n = null;
} else if (m != null) {
n = field.getText();
if (op.equals("+")) {
field.setText(Double.parseDouble(m) + Double.parseDouble(n)
+ "");
} else if (op.equals("-")) {
field.setText(Double.parseDouble(m) - Double.parseDouble(n)
+ "");
} else if (op.equals("*")) {
field.setText(Double.parseDouble(m) * Double.parseDouble(n)
+ "");
} else if (op.equals("/")) {
field.setText(Double.parseDouble(m) / Double.parseDouble(n)
+ "");
}
cls = true;
m = null;
n = null;
}
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();// 得到被点的按钮名称
if ("0123456789.".indexOf(str) != -1) {
if (cls) {
field.setText("");
cls = false;
}
field.setText(field.getText() + str);
} else if ("+-*/".indexOf(str) != -1) {
if (field.getText().equals("")) {
op = str;
} else if (m == null) {
m = field.getText();
op = str;
field.setText("");
} else if (m != null) {
setResult();
m = field.getText();
op = str;
}
} else if (str.equals("=")) {
setResult();
} else if (str.equals("CLEAR")) {
field.setText("");
m = null;
n = null;
}
}
public void init() {
frame.setLayout(new BorderLayout());
JPanel northPannel = new JPanel();
JPanel centerPannel = new JPanel();
JPanel southPannel = new JPanel();
northPannel.setLayout(new FlowLayout());
centerPannel.setLayout(new GridLayout(4, 4));
southPannel.setLayout(new FlowLayout());
northPannel.add(field);
southPannel.add(clearButton);
for (int i = 0; i < allButtons.length; i++) {
centerPannel.add(allButtons[i]);
}
frame.add(northPannel, BorderLayout.NORTH);
frame.add(southPannel, BorderLayout.SOUTH);
frame.add(centerPannel, BorderLayout.CENTER);
}
public void setFontAndColor() {
field.setFont(new Font("宋体", Font.BOLD, 20));
field.setForeground(Color.PINK);
field.setBackground(new Color(0x2a, 0x3b, 0x4f));
}
public void showMe() {
frame.pack(); // 系统调整大小---很重要
// frame.setSize(600, 400);// 自定义窗体大小
frame.setLocation(500, 400);// 设置窗体的启动位置
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
}
public static void main(String[] args) {
new CalculatorB().showMe();
}
}
以上代码是我的原创回答,可以直接运行的代码。并且此计算器可以实现连续计算,楼主试试去,有什么意见和建议再提