📄 calculator.java
字号:
package com.cn.dxj;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
class Calculator extends JFrame{
JButton J0,J1,J2,J3,J4,J5,J6,J7,J8,J9,Jdian,Jjia,Jjian,Jchen,Jchu,Jdeng,JC;
JTextField JTField1;
String Temp1;//存放前一个数
Double Temp2;//存放结果
int n;//判断使用哪种运算符
int count=1;//判断是否按过加减乘除,以便按运算符号的时候屏幕正确显示
int x=1;//判断小数点是否被按过
Double j;
public Calculator(){
setBounds(300,300,400,400);//方框的大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
J0=new JButton("0");//定义各个键
J1=new JButton("1");
J2=new JButton("2");
J3=new JButton("3");
J4=new JButton("4");
J5=new JButton("5");
J6=new JButton("6");
J7=new JButton("7");
J8=new JButton("8");
J9=new JButton("9");
Jdian=new JButton(".");
Jjia=new JButton("+");
Jjian=new JButton("-");
Jchen=new JButton("*");
Jchu=new JButton("/");
Jdeng=new JButton("=");
JC=new JButton("C");
JTField1=new JTextField(0);
getContentPane().setLayout(null);//显示各个数字键
J0.setBounds(70,60,50,50);//确定各个键的位置
J1.setBounds(120,60,50,50);
J2.setBounds(170,60,50,50);
J3.setBounds(70,110,50,50);
J4.setBounds(120,110,50,50);
J5.setBounds(170,110,50,50);
J6.setBounds(70,160,50,50);
J7.setBounds(120,160,50,50);
J8.setBounds(170,160,50,50);
J9.setBounds(70,210,50,50);
Jdian.setBounds(120,210,50,50);
Jjia.setBounds(220,60,50,50);
Jjian.setBounds(220,110,50,50);
Jchen.setBounds(220,160,50,50);
Jchu.setBounds(220,210,50,50);
Jdeng.setBounds(170,210,50,50);
JC.setBounds(70,260,200,50);
JTField1.setBounds(70,10,200,30);
J0.setBackground(Color.black);
getContentPane().add(J0);
getContentPane().add(J1);
getContentPane().add(J2);
getContentPane().add(J3);
getContentPane().add(J4);
getContentPane().add(J5);
getContentPane().add(J6);
getContentPane().add(J7);
getContentPane().add(J8);
getContentPane().add(J9);
getContentPane().add(Jdian);
getContentPane().add(Jjia);
getContentPane().add(Jjian);
getContentPane().add(Jchen);
getContentPane().add(Jchu);
getContentPane().add(Jdeng);
getContentPane().add(JC);
getContentPane().add(JTField1);
J0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"0");
else JTField1.setText("0");
count=1;
}}); //键入数字
J1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"1");
else JTField1.setText("1");
count=1;
}});
J2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"2");
else JTField1.setText("2");
count=1;
}});
J3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"3");
else JTField1.setText("3");
count=1;
}});
J4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"4");
else JTField1.setText("4");
count=1;
}});
J5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"5");
else JTField1.setText("5");
count=1;
}});
J6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"6");
else JTField1.setText("6");
count=1;
}});
J7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"7");
else JTField1.setText("7");
count=1;
}});
J8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"8");
else JTField1.setText("8");
count=1;
}});
J9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(count==1)
JTField1.setText(JTField1.getText()+"9");
else JTField1.setText("9");
count=1;
}});
Jdian.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(x==1)
JTField1.setText(JTField1.getText()+".");
x=0;//取消小数点的使用
count=1;
}});
Jjia.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
count=0;
x=1;//恢复小数点的使用
n=1;
Temp1=JTField1.getText();//储存按加号前的数字
}});
Jjian.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
count=0;
x=1;//恢复小数点的使用
n=2;
Temp1=JTField1.getText();//储存按减号前的数字
}});
Jchen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
count=0;
x=1;//恢复小数点的使用
n=3;
Temp1=JTField1.getText();//储存按乘号前的数字
}});
Jchu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
count=0;
x=1;//恢复小数点的使用
n=4;
Temp1=JTField1.getText();//储存按除号前的数字
}});
Jdeng.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int s=JTField1.getText().length();
if (s>0)
{switch(n){//switch语句判断使用的是什么运算符号
case 1: Temp2=Double.parseDouble(Temp1)+Double.parseDouble(JTField1.getText());break;
case 2: Temp2=Double.parseDouble(Temp1)-Double.parseDouble(JTField1.getText());break;
case 3: Temp2=Double.parseDouble(Temp1)*Double.parseDouble(JTField1.getText());break;
case 4: Temp2=Double.parseDouble(Temp1)/Double.parseDouble(JTField1.getText());break;}
JTField1.setText(Temp2+"");}
}});
JC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JTField1.setText("");
}});
setVisible(true);
setTitle("该计算器是网上找来经过修改而来!");
//setResizable(false);
}
public static void main(String[] args) {
Calculator x=new Calculator();//清零后等待下一次的输入
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -