📄 calculator.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
private Container c = getContentPane();
public Calculator(){
JPanel head = getHeaderPanel();
c.add(head,BorderLayout.NORTH);
JPanel option = getOptionPanel();
c.add(option,BorderLayout.CENTER);
init(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("计算器");
setSize(270,135);
setResizable(false);
setVisible(true);
}
private JButton getButton(String text) {
JButton btnTemp = new JButton(text);
btnTemp.addActionListener(this);
return btnTemp;
}
private JTextField txtAccountResult;
private JPanel getHeaderPanel(){
JPanel panel = new JPanel();
txtAccountResult = new JTextField(42);
txtAccountResult.setEditable(false);
txtAccountResult.setBorder(BorderFactory.createLoweredBevelBorder());
txtAccountResult.setHorizontalAlignment(JTextField.RIGHT);
txtAccountResult.setCursor(new Cursor(Cursor.TEXT_CURSOR));
panel.add(txtAccountResult);
return panel;
}
private JPanel getOptionPanel(){
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,6,5,5));
panel.add(getButton("7"));
panel.add(getButton("8"));
panel.add(getButton("9"));
panel.add(getButton("0"));
panel.add(getButton("+"));
panel.add(getButton("-"));
panel.add(getButton("4"));
panel.add(getButton("5"));
panel.add(getButton("6"));
panel.add(getButton("."));
panel.add(getButton("*"));
panel.add(getButton("/"));
panel.add(getButton("1"));
panel.add(getButton("2"));
panel.add(getButton("3"));
panel.add(getButton("="));
panel.add(getButton("M"));
panel.add(getButton("C"));
return panel;
}
private char sign; //运算符号
private double result; //计算结果
private double textTempValue; //临时结果
private boolean state; //是否是第一次
private String textValue; //记录文本框的值
private boolean start; //是否刚刚按了"="
private char msign; //处于激活状态的符号
private double mresult; //处于激活状态的结果
private boolean activeM; //"M"键是否处于激活状态
public void actionPerformed(ActionEvent e){
String numberGroup = "0123456789";
String signGroup = "+-*/=";
String input = e.getActionCommand();
if(numberGroup.indexOf(input) != -1){
if(start){
init(true);
}
if(textValue.equals("0")){
textValue = "";
}
textValue += input;
txtAccountResult.setText(textValue);
}else if(signGroup.indexOf(input) != -1){
try{
textTempValue = Double.parseDouble(textValue);
}catch(NumberFormatException ex){}
if(state){
result = textTempValue;
state = false;
}else{
getValue(sign,textTempValue);
}
if(!input.equals("="))
sign = signGroup.charAt(signGroup.indexOf(input));
else
start = true;
textValue = "";
txtAccountResult.setText(String.valueOf(result));
}else if(input.equals("M")){
if(activeM){
try{
textTempValue = Double.parseDouble(textValue);
}catch(NumberFormatException ex){}
getValue(sign,textTempValue);
getValue(msign,mresult);
txtAccountResult.setText(String.valueOf(result));
textValue = String.valueOf(result);
msign = ' ';
mresult = 0.0;
activeM = false;
state = true;
}else{
msign = sign;
mresult = result;
init(false);
txtAccountResult.setText(String.valueOf(mresult));
activeM = true;
}
}else if(input.equals("C")){
init(true);
}else if(input.equals(".")){
if(textValue.indexOf(".") == -1){
textValue += input;
txtAccountResult.setText(textValue);
}
}
}
private void init(boolean isInit){
start = false;
sign = ' ';
result = 0.0;
textTempValue = 0.0;
state = true;
textValue = "0";
activeM = false;
txtAccountResult.setText(textValue);
if(isInit){
msign = ' ';
mresult = 0.0;
}
}
private void getValue(char sign,double temp){
switch (sign){
case '+':result += temp;break;
case '-':result -= temp;break;
case '*':result *= temp;break;
case '/':result /= temp;break;
}
}
private static String windows =
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
public static void main(String[] args){
try{
UIManager.setLookAndFeel(windows);
}catch(Exception e){
e.printStackTrace();
}
new Calculator();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -