📄 jisuanji.java
字号:
import java.awt.*;
import java.awt.event.*;
public class jisuanji implements ActionListener,WindowListener{
Frame f;
TextField msg;
Panel pan;
Button buttons[];
Button button_Add,button_Del,button_Mul,button_Div,button_Equ,button_C;
float Left,Right,Result;
int type;
int temp;
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<buttons.length;i++){
if(e.getSource()==buttons[i]){
msg.setText(msg.getText()+Integer.toString(i));
}
}
if(e.getSource()==button_Add){
Left=Float.parseFloat(msg.getText());
msg.setText("");
type=1;
}
if(e.getSource()==button_Del){
Left=Float.parseFloat(msg.getText());
msg.setText("");
type=2;
}
if(e.getSource()==button_Mul){
Left=Float.parseFloat(msg.getText());
msg.setText("");
type=3;
}
if(e.getSource()==button_Div){
Left=Float.parseFloat(msg.getText());
msg.setText("");
type=4;
}
if(e.getSource()==button_C)
msg.setText("");
if(e.getSource()==button_Equ){
switch(type)
{
case 1: Right=Float.parseFloat(msg.getText());
Result=Left+Right;
temp=(int)Result;
if((float)temp==Result)
msg.setText(Integer.toString(temp));
else
msg.setText(Float.toString(Result));
break;
case 2: Right=Float.parseFloat(msg.getText());
Result=Left-Right;
temp=(int)Result;
if((float)temp==Result)
msg.setText(Integer.toString(temp));
else
msg.setText(Float.toString(Result));
break;
case 3: Right=Float.parseFloat(msg.getText());
Result=Left*Right;
temp=(int)Result;
if((float)temp==Result)
msg.setText(Integer.toString(temp));
else
msg.setText(Float.toString(Result));
break;
case 4: Right=Float.parseFloat(msg.getText());
Result=Left/Right;
temp=(int)Result;
if((float)temp==Result)
msg.setText(Integer.toString(temp));
else
msg.setText(Float.toString(Result));
break;
}
}
}
public jisuanji()
{
f=new Frame("计算器");
msg=new TextField(20);
pan=new Panel();
buttons=new Button[10];
button_Add=new Button("+");
button_Del=new Button("-");
button_Mul=new Button("*");
button_Div=new Button("/");
button_Equ=new Button("=");
button_C=new Button("CE");
for(int i=0;i<buttons.length;i++){
buttons[i]=new Button(""+i);
}
pan.setLayout(new GridLayout(4,4));
pan.add(buttons[1]);
pan.add(buttons[2]);
pan.add(buttons[3]);
pan.add(button_Add);
pan.add(buttons[4]);
pan.add(buttons[5]);
pan.add(buttons[6]);
pan.add(button_Del);
pan.add(buttons[7]);
pan.add(buttons[8]);
pan.add(buttons[9]);
pan.add(button_Mul);
pan.add(buttons[0]);
pan.add(button_Div);
pan.add(button_Equ);
pan.add(button_C);
f.add(msg,BorderLayout.NORTH);
f.add(pan,BorderLayout.CENTER);
for(int i=0;i<10;i++)
buttons[i].addActionListener(this);
button_Add.addActionListener(this);
button_Del.addActionListener(this);
button_Mul.addActionListener(this);
button_Div.addActionListener(this);
button_Equ.addActionListener(this);
button_C.addActionListener(this);
f.addWindowListener(this);
f.setSize(150,240);
f.setVisible(true);
}
public void windowClosing(WindowEvent e){System.exit(0);}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public static void main(String args[])
{
new jisuanji();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -