Java课的一次GUI作业,写了一个简单的计算器,支持加减乘除连续运算。代码很少,一百多行吧。
运行起来是这样子的
上面那个文本框就很奇怪,可能是用了setLayout的缘故,用setSize好像对它不起作用。不管它了,就这样吧2333。
贴个代码
package calculator;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener{
private final String[] KEYS = {"√","C","⬅","÷","7","8","9","×","4","5","6","+","1","2","3","-","±",".","0","="};
private JButton keys[] = new JButton[KEYS.length]; //按钮
private JTextField resultText = new JTextField("0"); //文本框
private boolean firstDigit = true; //数字首位未按下标记
private String operator ;
private double lastNum = 0;
//构造函数
public Calculator(String title){
super(title);
initialize();
}
//初始化界面
private void initialize() {
JPanel keyPanel = new JPanel();
keyPanel.setLayout(new GridLayout(5,4,3,3)); //网络布局 5行4列 间隔3个像素
for(int i =0; i < KEYS.length; i++) {
keys[i] = new JButton(KEYS[i]);
keyPanel.add(keys[i]);
keys[i].addActionListener(this);
//数字键和算符键用颜色区分开
if("0123456789.".indexOf(KEYS[i]) >= 0) keys[i].setForeground(Color.blue);
else keys[i].setForeground(Color.RED);
}
this.add(keyPanel);
resultText.setHorizontalAlignment(JTextField.RIGHT);
resultText.setEditable(false);
resultText.setBackground(Color.white);
this.add("North", resultText);
this.setVisible(true);
this.pack();
this.setBounds(500, 300, 400, 400);
}
@Override
public void actionPerformed(ActionEvent e) { //根据不同事件源选择相应操作
// TODO Auto-generated method stub
// 获取事件源的标签
String label = e.getActionCommand();
if (label.equals("⬅")) {
handleBackspace();
} else if (label.equals("C")) {
handleC();
} else if ("0123456789.".indexOf(label) >= 0) {
handleNumber(label);
} else {
handleOperator(label);
}
}
//退格
private void handleBackspace() {
String text = resultText.getText();
int i = text.length();
if (i > 0) {
// 退格
text = text.substring(0, i - 1);
if (text.length() == 0) {
// 如果文本没有了内容,则初始化计算器的各种值
resultText.setText("0");
firstDigit = true;
operator = "";
} else {
// 显示新的文本
resultText.setText(text);
}
}
}
//数字键
private void handleNumber(String key) {
if (firstDigit) {
// 数字的第一位
resultText.setText(key);
} else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)) {
// 输入的是小数点,小数点附在结果文本框的后面
resultText.setText(resultText.getText() + ".");
} else if (!key.equals(".")) {
// 输入的是数字,将数字附在结果文本框的后面
resultText.setText(resultText.getText() + key);
}
firstDigit = false;
}
//归零
private void handleC() {
// 初始化
resultText.setText("0");
firstDigit = true;
operator = "";
}
//算符键
private void handleOperator(String key) {
double x = getNumberFromText(); //当前文本框中的操作数
if(key.equals("±")) {
x = x * (-1);
}
else if(key.equals("√"))
{
x = Math.sqrt(x);
}
else if(!key.equals("=")) //按下的是双目运算符e
{
lastNum = x; //存下第一个操作数
operator = key; //存下操作符
//System.out.println(key);
}
else {
System.out.println(operator);
if(operator.equals("+")) {
x += lastNum;
}
else if(operator.equals("-")) {
x = lastNum - x;
}
else if(operator.equals("×")) {
x *= lastNum;
}
else if(operator.equals("÷"))
{
if(x==0.0)
{
resultText.setText("Error");
return ;
}
else
x = lastNum/x;
}
}
firstDigit = true;
setText(x);
}
//更新文本框显示
private void setText(double res)
{
long t1;
double t2;
t1 = (long) res;
t2 = res - t1;
if (t2 == 0) {
resultText.setText(String.valueOf(t1));
} else {
resultText.setText(String.valueOf(res));
}
}
//从文本框中获取数值
private double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(resultText.getText()).doubleValue();
} catch (NumberFormatException e) {
}
return result;
}
public static void main(String[] args) {
Calculator cal = new Calculator("计算器");
}
}