只简单的说一下设计的思路
1.整个计算机的组件我分为三个部分,数字按键0-9, 符号按键+-*/= ,结果框resultField
2.变量解释说明
private String numList = "0123456789";
定义字符串包含所有的数字,因为对于数字按键的监听操作都是差不多的,通过获取数字按键的值在通过与numList的对比,就能确定数字按键的类型
private static int operation = 1; //record pervious operation,"+"——1(default) "-"——2
符号标记值 记录上一次符号按键的值 1表示+ 2表示- 3表示* 4表示/
private static int result = 0;//record the result that stored currently
用来记录当前操作的最终结果值
private static int num_opera = 1;//if the pervious operation is pressing number,num_opera is 1,else if it is config operation, num_opera is 2
用来记录上一次的用户操作是数字按键还是符号按键
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author zero
*
*/
public class Calculator extends JFrame{
private String numList = "0123456789";
private static int operation = 1; //record pervious operation,"+"——1(default) "-"——2
private static int result = 0;//record the result that stored currently
private static int num_opera = 1;//if the pervious operation is pressing number,num_opera is 1,else if it is config operation, num_opera is 2
private static TextField resultField;
public Calculator() {
this.setTitle("简易计算器");
this.setLocation(20, 20);
setSizeAndCentralizeMe(320, 380);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
JOptionPane.showConfirmDialog(null, "Welcome to my litter calculator", "Welcome", JOptionPane.PLAIN_MESSAGE);
super.windowOpened(e);
}
@Override
public void windowClosing(WindowEvent e) {
JOptionPane.showConfirmDialog(null, "Thank you for your using...", "ByeBye", JOptionPane.PLAIN_MESSAGE);
super.windowClosing(e);
System.exit(0);
}
});
}
public void generateCalculator(Calculator calculator) {
calculator.setLayout(new BorderLayout());
JPanel mainPanel = new JPanel(new GridLayout(5, 3, 5, 5));
JPanel panel = new JPanel(new GridLayout(1, 2, 3, 5));
calculator.setBackground(Color.blue);
for(int i=0; i<15; i++){
if(i<9) {
JButton button = new JButton(String.valueOf(i+1));
button.addMouseListener(buttonListener);
mainPanel.add(button);
}else {
JButton button = new JButton();
switch(i) {
case 9 : {button.setText("-"); break;}
case 10 : {button.setText("0"); break;}
case 11 : {button.setText("+"); break;}
case 12 : {button.setText("*"); break;}
case 13 : {button.setText("C"); break;}
case 14 : {button.setText("/"); break;}
}
if(!button.getText().equals("C")) {
button.addMouseListener(buttonListener);
}else {
button.addMouseListener(cButtonListener);
}
mainPanel.add(button);
}
}
JButton button = new JButton("=");
button.addMouseListener(buttonListener);
resultField = new TextField();
panel.add(button);
panel.add(resultField);
panel.setVisible(true);
mainPanel.setVisible(true);
calculator.getContentPane().add("Center", mainPanel);
calculator.getContentPane().add("South", panel);
calculator.setVisible(true);
}
private void setSizeAndCentralizeMe(int width, int height) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(width, height);
this.setLocation(screenSize.width / 2 - width / 2, screenSize.height
/ 2 - height / 2);
}
private MouseListener cButtonListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
operation = 1;
result = 0;
num_opera = 1;
resultField.setText("0");
}
};
private MouseListener buttonListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String text = ((JButton)e.getSource()).getText();
if(numList.contains(text)) {
int number = Integer.valueOf(text);
if (num_opera == 1) {
String newtext = resultField.getText() + text;
resultField.setText(newtext);
}else if (num_opera == 2) {
resultField.setText(text);
num_opera = 1;
}
}else {
if (num_opera == 1) {
if (operation == 1) {
result = result + Integer.valueOf(resultField.getText());
resultField.setText(String.valueOf(result));
}
else if (operation == 2) {
result = result - Integer.valueOf(resultField.getText());
resultField.setText(String.valueOf(result));
}
else if (operation == 3) {
result = result * Integer.valueOf(resultField.getText());
resultField.setText(String.valueOf(result));
}
else if (operation == 4) {
result = result / Integer.valueOf(resultField.getText());
resultField.setText(String.valueOf(result));
}
}
switch(text) {
case "+" : {operation = 1;break;}
case "-" : {operation = 2;break;}
case "*" : {operation = 3;break;}
case "/" : {operation = 4;break;}
case "=" : {operation = 1;break;}
}
num_opera = 2;
}
}
};
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.generateCalculator(calculator);
}
}