📄 mycalculation.java
字号:
package myprogram;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyCalculation extends JFrame{
String hhnomber;
Boolean clear=false;
JPanel jpanel1=new JPanel();
JPanel jpanel2=new JPanel();
JButton[] jbutton=new JButton[20];
JTextField jfield=new JTextField(27);
JMenuBar m_bar=new JMenuBar();
JMenu jmenu=new JMenu("进制转化");
JMenuItem bin=new JMenuItem("二进制");
JMenuItem hh=new JMenuItem("十进制");
private boolean first=true;
private double number1;
private double number2;
private int jjcc;
private double result;//结果
private int t=0;
public MyCalculation(String title){
super(title);
setJMenuBar(m_bar);
m_bar.add(jmenu);
jmenu.add(bin);
jmenu.add(hh);
jfield.setHorizontalAlignment(JTextField.RIGHT);
jfield.setForeground(Color.black);
jfield.setBackground(Color.green);
jfield.setText("0");
jfield.setEditable(false);//定义文本框的属性
String anNu[]={"1","2","3","+","C","4","5","6","-","退格","7","8","9",
"*","1/x","0","+/-",".","/","="};//从0~19
for(int i=0;i<20;i++){
jbutton[i]=new JButton(anNu[i]);
jbutton[i].setForeground(Color.blue);
}
A a=new A();//数字按钮
B b=new B();//清零按键
C c=new C();//退格键的事件
D d=new D();//1/x的监听
E e=new E();//小数点监听
F f=new F();//运算监听
G g=new G();//运算结果
H h=new H();//正负号监听
Bin BIN =new Bin();
Hin HIN=new Hin();
for(int i=0;i<3;i++)
jbutton[i].addActionListener(a);
for(int i=5;i<8;i++)
jbutton[i].addActionListener(a);
for(int i=10;i<13;i++)
jbutton[i].addActionListener(a);
jbutton[15].addActionListener(a);
jbutton[4].addActionListener(b);
jbutton[9].addActionListener(c);
jbutton[14].addActionListener(d);
jbutton[17].addActionListener(e);
jbutton[3].addActionListener(f);
jbutton[8].addActionListener(f);
jbutton[13].addActionListener(f);
jbutton[18].addActionListener(f);
jbutton[19].addActionListener(g);
jbutton[16].addActionListener(h);
bin.addActionListener(BIN);
hh.addActionListener(HIN);
//hh.addActionLlistener();
jpanel1.add(jfield);//显示输入数字及运算结果的标签
jpanel2.setLayout(new GridLayout(4,5));//设置成为4行5列的butoon布局
for(int i=0;i<20;i++)
jpanel2.add(jbutton[i]);
getContentPane().add(jpanel1,BorderLayout.NORTH);
getContentPane().add(jpanel2,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
pack();
setVisible(true);//可显示
}
/**数字按钮的监听*/
class A implements ActionListener{
public void actionPerformed(ActionEvent e){
if(clear){
clear=false;
jfield.setText("");
}
String input = e.getActionCommand();
if (first)
{ if(input=="0"){
jfield.setText("");
first=true;
}
else{
jfield.setText("");
first = false;
}
}
jfield.setText(jfield.getText() + input);
}
}
class Bin implements ActionListener{
public void actionPerformed(ActionEvent e){
String str=jfield.getText();
hhnomber=str;
int count=Integer.parseInt(str);
String str2=Integer.toBinaryString(count);
jfield.setText(str2);
}
}
class Hin implements ActionListener{
public void actionPerformed(ActionEvent e){
jfield.setText(hhnomber);
}
}
/**清零按钮的监听*/
class B implements ActionListener{
public void actionPerformed(ActionEvent e){
jfield.setText("0");
first=true;
}
}
/**退格按钮的监听*/
class C implements ActionListener{
public void actionPerformed(ActionEvent e){
String s1,s2;
int jflength;
s1=jfield.getText();
jflength=jfield.getText().length();
if(jflength==0||jflength==1)
jfield.setText("0");
else{
s2=s1.substring(0,jflength-1);
jfield.setText(s2);
}
first=true;
}
}
/**数字按钮的监听*/
class D implements ActionListener{
public void actionPerformed(ActionEvent e){
double x=Double.parseDouble(jfield.getText());
if(x==0)
jfield.setText("正无穷大");
else
jfield.setText(""+1/x);
}
}
/**小数点按钮的监听*/
class E implements ActionListener{
public void actionPerformed(ActionEvent e){
if(jfield.getText().trim().indexOf('.')!=-1)
;
else if(jfield.getText().trim().equals("0"))
jfield.setText("0"+e.getActionCommand());
else
jfield.setText(jfield.getText()+e.getActionCommand());
first=false;
}
}
/**运算按钮的监听*/
class F implements ActionListener{
int operation;
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="+")
operation=1;
else if(e.getActionCommand()=="-")
operation=2;
else if(e.getActionCommand()=="*")
operation=3;
else if(e.getActionCommand()=="/")
operation=4;
number1=Double.parseDouble(jfield.getText());
switch(operation){
case 1:
jjcc=1;
first=true;
break;
case 2:
jjcc=2;
first=true;
break;
case 3:
jjcc=3;
first=true;
break;
case 4:
jjcc=4;
first=true;
break;
}
}
}
/**运算按钮的监听*/
class G implements ActionListener{
public void actionPerformed(ActionEvent e){
clear=true;
number2=Double.parseDouble(jfield.getText());
if(number1>Double.MAX_VALUE&&number1<Double.MIN_VALUE||number2>Double.MAX_VALUE&&number2<Double.MIN_VALUE)
jfield.setText("数据越界");
else{
switch(jjcc){
case 1://加法
result=number1+number2;
result=number1+number2;
jfield.setText(""+result);
break;
case 2: //减法
result=number1-number2;
jfield.setText(""+result);
break;
case 3://乘法
result=number1*number2;
jfield.setText(""+result);
break;
case 4://除法
if(number2==0)
jfield.setText("Error! 除数不能为零!");
else{
result=number1/number2;
jfield.setText(""+result);
}
break;
}
}
}
}
/**正负号按钮的监听*/
class H implements ActionListener{
public void actionPerformed(ActionEvent e){
double s=Double.parseDouble(jfield.getText());
if(t%2==0)
jfield.setText("+"+Math.abs(s));//返回为"+原数的绝对值"
else
jfield.setText("-"+Math.abs(s));//返回为"-原数的绝对值"
t++;
}
}
public static void main(String[] args){
new MyCalculation("计算器");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -