Catalog
java计算器
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//注意:运算符号并不显示

先导入需要的包

public class Calculator extends JFrame {
private Container container = this.getContentPane();
private GridBagLayout gridBagLayout = new GridBagLayout();
private GridBagConstraints gridBagConstraints= new GridBagConstraints();
private JTextField displayField;
private String lastCommand="=";
private double result=0;
private boolean start=true;

public Calculator() {
super("super计算器");
container.setLayout(gridBagLayout);
initDisplayArea();
initButtons();
initFrame();
}

private void initFrame() {
this.setSize(600, 700);
setVisible(true);
setResizable(true);
setForeground(Color.CYAN);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

private void initButtons() {
ActionListener insert = new Calculator.InsertAction();
ActionListener command = new Calculator.CommandAction();
addButton("Back", 0, 1, 2, 1,command);
//addButton("π", 0, 1, 1, 1, insert);
addButton("C", 2, 1, 2, 1, insert);
addButton("7", 0, 2, 1, 1, insert);
addButton("8", 1, 2, 1, 1, insert);
addButton("9", 2, 2, 1, 1, insert);
addButton("+", 3, 2, 1, 1, command);
addButton("4", 0, 3, 1, 1, insert);
addButton("5", 1, 3, 1, 1, insert);
addButton("6", 2, 3, 1, 1, insert);
addButton("-", 3, 3, 1, 1, command);
addButton("1", 0, 4, 1, 1, insert);
addButton("2", 1, 4, 1, 1, insert);
addButton("3", 2, 4, 1, 1, insert);
addButton("*", 3, 4, 1, 1, command);
addButton("0", 0, 5, 1, 1, insert);
addButton("-", 1, 5, 1, 1, command);
addButton(".", 2, 5, 1, 1, insert);
addButton("/", 3, 5, 1, 1, command);
addButton("=", 0, 7, 2, 2, command);
addButton("sin °", 2, 6, 1, 1, command);
addButton("cos °", 2, 7, 1, 1, command);
addButton("tan °", 2, 8, 1, 1, command);
addButton("x^y", 3, 8, 1, 1, command);
addButton("%", 3, 6, 1, 1, command);
addButton("√", 3, 7, 1, 1, command);
addButton("n!", 0, 6, 2, 1, command);
addButton2("designed by superzhaoyang", 0, 9, 4, 1, command);


}

private void initDisplayArea() {
displayField = new JTextField(20);
displayField.setHorizontalAlignment(SwingConstants.RIGHT);
displayField.setBackground(Color.gray);
displayField.setDisabledTextColor(Color.BLACK);
displayField.setEnabled(false);
displayField.setFont(new Font("宋体", Font.BOLD ,50));
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 4;
gridBagConstraints.gridheight = 1;
gridBagConstraints.fill = 1;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagLayout.setConstraints(displayField, gridBagConstraints);
container.add(displayField);
}

private void addButton(String label, int column, int row, int width, int height, ActionListener listener) {
JButton button = new JButton(label);
button.setForeground(Color.black);
button.setBackground(Color.white);
button.setFont(new Font("楷体", Font.BOLD, 36));
if(listener != null)
button.addActionListener(listener);

gridBagConstraints.gridx = column;
gridBagConstraints.gridy = row;
gridBagConstraints.gridwidth = width;
gridBagConstraints.gridheight = height;
gridBagConstraints.fill = GridBagConstraints.BOTH;//填充空间
gridBagConstraints.insets = new Insets(1,1,1,1);//彼此组件的距离
gridBagLayout.setConstraints(button, gridBagConstraints);
container.add(button);
}
private void addButton_(String label, int column, int row, int width, int height, ActionListener listener,ActionListener listener2) {
JButton button = new JButton(label);
button.setForeground(Color.black);
button.setBackground(Color.white);
button.setFont(new Font("楷体", Font.BOLD, 36));
button.addActionListener(listener);
button.addActionListener(listener2);

gridBagConstraints.gridx = column;
gridBagConstraints.gridy = row;
gridBagConstraints.gridwidth = width;
gridBagConstraints.gridheight = height;
gridBagConstraints.fill = GridBagConstraints.BOTH;//填充空间
gridBagConstraints.insets = new Insets(1,1,1,1);//彼此组件的距离
gridBagLayout.setConstraints(button, gridBagConstraints);
container.add(button);
}
private void addButton2(String label, int column, int row, int width, int height, ActionListener listener) {
JButton button = new JButton(label);
button.setForeground(Color.orange);
button.setBackground(Color.CYAN);
button.setFont(new Font("楷体", Font.BOLD, 36));
if(listener != null)
button.addActionListener(listener);

gridBagConstraints.gridx = column;
gridBagConstraints.gridy = row;
gridBagConstraints.gridwidth = width;
gridBagConstraints.gridheight = height;
gridBagConstraints.fill = GridBagConstraints.BOTH;//填充空间
gridBagConstraints.insets = new Insets(1,1,1,1);//彼此组件的距离
gridBagLayout.setConstraints(button, gridBagConstraints);
container.add(button);
}

public void calculate(double x) {
if (this.lastCommand.equals("+")) {
this.result += x;
} else if (this.lastCommand.equals("-")) {
this.result -= x;
} else if (this.lastCommand.equals("*")) {
this.result *= x;
} else if (this.lastCommand.equals("/")) {
this.result /= x;
} else if (this.lastCommand.equals("=")) {
this.result = x;
} else if (this.lastCommand.equals("%")) {
this.result = this.result % x;
} else if (this.lastCommand.equals("√")) {
this.result = Math.sqrt(x);
} else if (this.lastCommand.equals("sin °")) {
this.result = Math.sin(x*Math.PI / 180) ;
}
else if (this.lastCommand.equals("cos °")) {
this.result = Math.cos(x*Math.PI / 180) ;
}

else if (this.lastCommand.equals("x^y")) {
double t = this.result;
for(int i = 1;i < x;i++)
this.result *= t;
}
else if (this.lastCommand.equals("tan °")) {
this.result = Math.tan(x*Math.PI / 180) ;
}

else if (this.lastCommand.equals("n!")) {
int temp = 1;
for(int i = (int)x;i >= 1;i--)
temp = temp * i ;
this.result = temp;
}


this.displayField.setText("" + this.result);
}

public static void main(String[] args) {

new Calculator();
}

private class CommandAction implements ActionListener {
private CommandAction() {
}

public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
if (Calculator.this.start) {
Calculator.this.lastCommand = command;
} else {
Calculator.this.calculate(Double.parseDouble(Calculator.this.displayField.getText()));
Calculator.this.lastCommand = command;
Calculator.this.start = true;
}
}
}

private class InsertAction implements ActionListener {
private InsertAction() {
}

public void actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (Calculator.this.start) {
Calculator.this.displayField.setText("");
Calculator.this.start = false;
if (input.equals("-")) {
Calculator.this.displayField.setText(Calculator.this.displayField.getText() + "-");
}


}

if (!input.equals("-")) {
if (input.equals("Back")) {
String str = Calculator.this.displayField.getText();
if (str.length() > 0) {
Calculator.this.displayField.setText(str.substring(0, str.length() - 1));
}
} else if (input.equals("C")) {
Calculator.this.displayField.setText("0");
result=0.0d;
Calculator.this.start = true;
} else{
if (input.equals(".")) {
if (Calculator.this.displayField.getText().trim().indexOf(".") == -1) {
Calculator.this.displayField.setText(Calculator.this.displayField.getText() + input);
}
} else {
Calculator.this.displayField.setText(Calculator.this.displayField.getText() + input);
}
}
}

}
}
}

运行界面如下:

Author: superzhaoyang
Link: http://yoursite.com/2019/11/20/java计算器/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付宝
��