?? calculatorframe.java
字號:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
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 CalculatorFrame extends JFrame {
private String str1 = "";
private String str2 = "";
private StringBuffer str = new StringBuffer("");
private char ch1;
private char ch2;
private boolean minusFlag;
private boolean hasPoint;
private JTextField jtf = new JTextField();
private JPanel jpUp = new JPanel();
private JPanel jpCenter = new JPanel();
private JButton jb1 = new JButton("1");
private JButton jb2 = new JButton("2");
private JButton jb3 = new JButton("3");
private JButton jb4 = new JButton("4");
private JButton jb5 = new JButton("5");
private JButton jb6 = new JButton("6");
private JButton jb7 = new JButton("7");
private JButton jb8 = new JButton("8");
private JButton jb9 = new JButton("9");
private JButton jb0 = new JButton("0");
private JButton jbPoint = new JButton(".");
private JButton jbCPL = new JButton("+/-");
private JButton jbAdd = new JButton("+");
private JButton jbMinus = new JButton("-");
private JButton jbMulti = new JButton("*");
private JButton jbDivide = new JButton("/");
private JButton jbResult = new JButton("=");
private JButton jbBackspace = new JButton("Backspace");
private JButton jbC = new JButton("C");
private JButton jbCE = new JButton("CE");
public void go() {
jpUp.setLayout(new BorderLayout());
jpUp.add(jtf, BorderLayout.CENTER);
JPanel jpUpDown = new JPanel();
jpUpDown.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
jpUpDown.add(jbBackspace);
jpUpDown.add(jbC);
jpUpDown.add(jbCE);
jpUpDown.add(jbResult);
jpUp.add(jpUpDown, BorderLayout.SOUTH);
JPanel jpCenterLeft = new JPanel();
jpCenterLeft.setLayout(new GridLayout(4, 1, 5, 5));
jpCenterLeft.add(jbAdd);
jpCenterLeft.add(jbMinus);
jpCenterLeft.add(jbMulti);
jpCenterLeft.add(jbDivide);
JPanel jpCenterCenter = new JPanel();
jpCenterCenter.setLayout(new GridLayout(4, 3, 5 ,5));
jpCenterCenter.add(jb7);
jpCenterCenter.add(jb8);
jpCenterCenter.add(jb9);
jpCenterCenter.add(jb4);
jpCenterCenter.add(jb5);
jpCenterCenter.add(jb6);
jpCenterCenter.add(jb1);
jpCenterCenter.add(jb2);
jpCenterCenter.add(jb3);
jpCenterCenter.add(jb0);
jpCenterCenter.add(jbCPL);
jpCenterCenter.add(jbPoint);
jpCenter.setLayout(new BorderLayout(5, 5));
jpCenter.add(jpCenterCenter);
jpCenter.add(jpCenterLeft, BorderLayout.WEST);
Container container = getContentPane();
container.add(jpUp, BorderLayout.NORTH);
container.add(jpCenter);
jtf.setHorizontalAlignment(JTextField.RIGHT);
}
public void addAllListeners() {
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("1");
jtf.setText(str.toString());
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("2");
jtf.setText(str.toString());
}
});
jb3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("3");
jtf.setText(str.toString());
}
});
jb4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("4");
jtf.setText(str.toString());
}
});
jb5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("5");
jtf.setText(str.toString());
}
});
jb6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("6");
jtf.setText(str.toString());
}
});
jb7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("7");
jtf.setText(str.toString());
}
});
jb8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("8");
jtf.setText(str.toString());
}
});
jb9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("9");
jtf.setText(str.toString());
}
});
jb0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append("0");
jtf.setText(str.toString());
}
});
jbAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opMethod('+');
}
});
jbMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opMethod('-');
}
});
jbMulti.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opMethod('*');
}
});
jbDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opMethod('/');
}
});
jbResult.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str2 = str.toString();
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
int result = 0;
switch(ch1) {
case '+': result = int1 + int2;break;
case '-': result = int1 - int2;break;
case '*': result = int1 * int2;break;
case '/': result = int1/int2;break;
}
jtf.setText("" + result);
str.delete(0, str.length());
str1 = "";
str2 = "";
}
});
jbBackspace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.deleteCharAt(str.length() - 1);
jtf.setText(str.toString());
}
});
jbCE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.delete(0, str.length());
jtf.setText(str.toString());
}
});
jbC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.delete(0, str.length());
str1 = "";
str2 = "";
jtf.setText(str.toString());
}
});
jbCPL.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!minusFlag) {
str.insert(0, '-');
jtf.setText(str.toString());
}
else {
str.deleteCharAt(0);
jtf.setText(str.toString());
}
minusFlag = true;
}
});
jbPoint.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str.append('.');
hasPoint = true;
}
});
}
public void opMethod(char op) {
if(!IsTwo()) {
str1 = str.toString();
ch1 = op;
ch2 = op;
str.delete(0, str.length());
ch1 = ch2;
}
else {
ch2 = op;
str2 = str.toString();
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
int result = 0;
switch(ch1) {
case '+': result = int1 + int2;break;
case '-': result = int1 - int2;break;
case '*': result = int1 * int2;break;
case '/': result = int1/int2;break;
}
jtf.setText("" + result);
str1 = String.valueOf(result);
str2 = "";
str.delete(0, str.length());
ch1 = ch2;
}
}
public boolean IsTwo() {
if(!str1.equals("") | !str2.equals(""))
return true;
return false;
}
public static void main(String[] args) {
CalculatorFrame cf = new CalculatorFrame();
cf.setTitle("Calculator");
cf.go();
cf.addAllListeners();
cf.pack();
cf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cf.setVisible(true);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -