📄 caculator.java
字号:
package myCculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Caculator extends JFrame{
double InputTemp,Input2;
double Output;
Stack Input;
static double memory=0.0;
static boolean decimalPressed=false;
static int operatorPressed=0;
static boolean signValue=true;
static boolean ErrorOccured=false;
static boolean BKSPLock=false;
final JTextField IOtext;
final JTextField memoryStatus;
final JButton BKSP;
final JButton CE;
final JButton C;
final JButton memoryClear;
final JButton Num[];
final JButton operator[];
final JButton sqrt;
final JButton memoryRecall;
final JButton percent;
final JButton memorySet;
final JButton reciprocal;
final JButton memoryPlus;
final JButton sign;
final JButton decimal;
final JButton equal;
public static void main(String args[]) {
try {
Caculator frame = new Caculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Caculator() {
super("");
setResizable(false);
getContentPane().setLayout(null);
setBounds(100, 100, 430, 235);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Input=new Stack();
IOtext = new JTextField();
IOtext.setFocusable(false);
IOtext.setDisabledTextColor(Color.BLACK);
IOtext.setAutoscrolls(false);
IOtext.setComponentOrientation
(ComponentOrientation.RIGHT_TO_LEFT);
IOtext.setText(".0");
IOtext.setColumns(35);
IOtext.setBounds(10, 10, 400, 21);
getContentPane().add(IOtext);
memoryStatus = new JTextField();
memoryStatus.setEditable(false);
memoryStatus.setBounds(10, 37, 25, 21);
getContentPane().add(memoryStatus);
BKSP = new JButton();
BKSP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false&&BKSPLock==false){
if(IOtext.getText().equals(".0")==false
&&IOtext.getText().length()>2
&&IOtext.getText().length()!=2&&
IOtext.getText().charAt
(IOtext.getText().length()-2)!='.'){
IOtext.setText(IOtext.getText().substring(0,
IOtext.getText().length()-1));
}
else if(IOtext.getText().length()>2&&
IOtext.getText().charAt
(IOtext.getText().length()-2)=='.'){
IOtext.setText("."+IOtext.getText().substring(0,
IOtext.getText().length()-2));
decimalPressed=false;
}
else{
IOtext.setText(".0");
decimalPressed=false;
}
}
}
});
BKSP.setText("Backspace");
BKSP.setBounds(93, 37, 101, 25);
getContentPane().add(BKSP);
CE = new JButton();
CE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
IOtext.setText(".0");
decimalPressed=false;
operatorPressed=0;
signValue=true;
ErrorOccured=false;
BKSPLock=false;
}
});
CE.setText(" CE ");
CE.setBounds(200, 37, 101, 25);
getContentPane().add(CE);
C = new JButton();
C.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
IOtext.setText(".0");
decimalPressed=false;
operatorPressed=0;
signValue=true;
ErrorOccured=false;
BKSPLock=false;
}
});
C.setText("C");
C.setBounds(307, 37, 101, 25);
getContentPane().add(C);
memoryClear = new JButton();
memoryClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(ErrorOccured==false){
memory=0;
memoryStatus.setText("");
}
}
});
memoryClear.setText("MC");
memoryClear.setBounds(10, 81, 53, 25);
getContentPane().add(memoryClear);
Num=new JButton[10];
for(int i=0;i<10;i++){
Num[i]=new JButton(Integer.toString(i));
Num[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false){
if(e.getSource()==Num[0]
&&IOtext.getText().equals(".0")
&&decimalPressed==false){}
else for(int i=0;i<10;i++){
if(e.getSource()==Num[i]
&&IOtext.getText().length()<33){
if((IOtext.getText().equals(".0")||
BKSPLock)&&decimalPressed==false){
IOtext.setText("."+Integer.toString(i));
BKSPLock=false;
if(Input.empty()){
operatorPressed=0;
}
}
else if(decimalPressed
&&IOtext.getText().charAt(0)=='.'){
IOtext.setText(IOtext.getText().substring(1)
+"."+Integer.toString(i));
}
else {
IOtext.setText(IOtext.getText()
+Integer.toString(i));
}
}
}
}
}
});
}
Num[7].setBounds(93, 81, 53, 25);
Num[8].setBounds(152, 81, 53, 25);
Num[9].setBounds(211, 81, 53, 25);
Num[4].setBounds(93, 112, 53, 25);
Num[5].setBounds(152, 112, 53, 25);
Num[6].setBounds(211, 112, 53, 25);
Num[1].setBounds(93, 143, 53, 25);
Num[2].setBounds(152, 143, 53, 25);
Num[3].setBounds(211, 143, 53, 25);
Num[0].setBounds(93, 174, 53, 25);
for(int i=0;i<10;i++){
getContentPane().add(Num[i]);
}
operator = new JButton[4];
for(int i=0;i<4;i++){
operator[i]=new JButton();
operator[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false){
for(int i=0;i<4;i++){
if(e.getSource()==operator[i]){
if(Input.empty()){}
else if(BKSPLock==false){
InputTemp=((Double)
(Input.pop())).doubleValue();
compute(InputTemp,
toDouble(IOtext.getText()));
}
Input.push(new Double
(toDouble(IOtext.getText())));
operatorPressed=i+1;
}
}
decimalPressed=false;
signValue=true;
BKSPLock=true;
}
}
});
}
operator[0].setText("/");
operator[1].setText("");
operator[2].setText("");
operator[3].setText("+");
operator[0].setBounds(270, 81, 54, 25);
operator[1].setBounds(270, 112, 54, 25);
operator[2].setBounds(270, 143, 54, 25);
operator[3].setBounds(270, 174, 54, 25);
for(int i=0;i<4;i++){
getContentPane().add(operator[i]);
}
equal = new JButton();
equal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false){
if(Input.empty()==false){
InputTemp=((Double)(Input.pop())).doubleValue();
Input2=toDouble(IOtext.getText());
}
else if(BKSPLock&&operatorPressed!=0){
InputTemp=toDouble(IOtext.getText());
}
compute(InputTemp,Input2);
decimalPressed=false;
BKSPLock=true;
}
}
});
equal.setText("=");
equal.setBounds(339, 174, 71, 25);
getContentPane().add(equal);
sqrt = new JButton();
sqrt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false){
if(toDouble(IOtext.getText())<0){
IOtext.setText("效");
ErrorOccured=true;
}
else {
resultOutput(Double.toString
(Math.sqrt(toDouble(IOtext.getText()))));
}
if(BKSPLock){
operatorPressed=0;
}
decimalPressed=false;
BKSPLock=true;
}
}
});
sqrt.setText("sqrt");
sqrt.setBounds(339, 81, 71, 25);
getContentPane().add(sqrt);
memoryRecall = new JButton();
memoryRecall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false){
resultOutput(Double.toString(memory));
decimalPressed=false;
if(memory<0)signValue=false;
else signValue=true;
}
}
});
memoryRecall.setText("MR");
memoryRecall.setBounds(10, 112, 53, 25);
getContentPane().add(memoryRecall);
percent = new JButton();
percent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
percent.setText("%");
percent.setBounds(339, 112, 71, 25);
getContentPane().add(percent);
memorySet = new JButton();
memorySet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false){
if(IOtext.getText().equals(".0")==false){
memory=toDouble(IOtext.getText());
memoryStatus.setText(" M");
}
else {
memory=0;
memoryStatus.setText("");
}
}
}
});
memorySet.setText("MS");
memorySet.setBounds(10, 143, 53, 25);
getContentPane().add(memorySet);
reciprocal = new JButton();
reciprocal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false){
if(IOtext.getText().equals(".0")){
IOtext.setText("为");
ErrorOccured=true;
}
else {
resultOutput(Double.toString(
1/toDouble(IOtext.getText())));
BKSPLock=true;
decimalPressed=false;
}
}
}
});
reciprocal.setText("1/x");
reciprocal.setBounds(339, 143, 71, 25);
getContentPane().add(reciprocal);
memoryPlus = new JButton();
memoryPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false){
memory+=toDouble(IOtext.getText());
if(memory==0){
memoryStatus.setText("");
}
else memoryStatus.setText(" M");
}
}
});
memoryPlus.setText("M+");
memoryPlus.setBounds(10, 174, 53, 25);
getContentPane().add(memoryPlus);
sign = new JButton();
sign.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(IOtext.getText().equals(".0")==false&&
ErrorOccured==false){
if(signValue){
if(IOtext.getText().charAt(0)!='.'){
IOtext.setText("-"+IOtext.getText());
}
else{
IOtext.setText(".-"+IOtext.getText().substring(1));
}
signValue=false;
}
else {
if(IOtext.getText().charAt(0)!='.'){
IOtext.setText(IOtext.getText().substring(1));
}
else{
IOtext.setText("."+IOtext.getText().substring(2));
}
signValue=true;
}
}
}
});
sign.setText("+/-");
sign.setBounds(152, 174, 53, 25);
getContentPane().add(sign);
decimal = new JButton();
decimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ErrorOccured==false){
if(decimalPressed==false){
if(BKSPLock==true){
IOtext.setText(".0");
signValue=true;
BKSPLock=false;
}
decimalPressed=true;
}
}
}
});
decimal.setText(".");
decimal.setBounds(211, 174, 53, 25);
getContentPane().add(decimal);
}
public double toDouble(String s){
if(s.charAt(0)=='.'){
return Double.parseDouble(s.substring(1));
}
else return Double.parseDouble(s);
}
public void resultOutput(String s){
while(s.charAt(s.length()-1)=='0'){
if(s.charAt(s.length()-2)=='.'){
IOtext.setText("."+s.substring(0,s.length()-2));
return;
}
else s=s.substring(0,s.length()-1);
}
IOtext.setText(s);
}
public void compute(double a,double b){
switch(operatorPressed){
case 1:if(b!=0){
resultOutput(Double.toString(a/b));
}
else{
IOtext.setText("为");
ErrorOccured=true;
}
break;
case 2:resultOutput(Double.toString(a*b));break;
case 3:resultOutput(Double.toString(a-b));break;
case 4:resultOutput(Double.toString(a+b));break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -