📄 jlab1201.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Face extends JFrame{
JTextField text,textM;
JButton bBack,bCe,bC;
JButton bMc,b7,b8,b9,bDiv,bSqrt;
JButton bMr,b4,b5,b6,bMul,bPen;
JButton bMs,b1,b2,b3,bSub,bRec;
JButton bMa,b0,bNeg,bPnt,bAdd,bEql;
Face(String s){
super(s);
this.setVisible(true);
this.setLayout(new BorderLayout(3,1));
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
JPanel pan3=new JPanel();
this.add(pan1,"North");
this.add(pan2,"Center");
this.add(pan3,"South");
pan1.setLayout(new GridLayout(1,1));
pan2.setLayout(new GridLayout(1,4));
pan3.setLayout(new GridLayout(4,6));
pan1.add(text=new JTextField("0.",24));
text.setHorizontalAlignment(JTextField.RIGHT);
pan2.add(textM=new JTextField(4));
pan2.add(bBack=new JButton("Backspace"));
pan2.add(bCe=new JButton("CE"));
pan2.add(bC=new JButton("C"));
pan3.add(bMc=new JButton("MC"));
pan3.add(b7=new JButton("7"));
pan3.add(b8=new JButton("8"));
pan3.add(b9=new JButton("9"));
pan3.add(bDiv=new JButton("/"));
pan3.add(bSqrt=new JButton("sqrt"));
pan3.add(bMr=new JButton("MR"));
pan3.add(b4=new JButton("4"));
pan3.add(b5=new JButton("5"));
pan3.add(b6=new JButton("6"));
pan3.add(bMul=new JButton("*"));
pan3.add(bPen=new JButton("%"));
pan3.add(bMs=new JButton("MS"));
pan3.add(b1=new JButton("1"));
pan3.add(b2=new JButton("2"));
pan3.add(b3=new JButton("3"));
pan3.add(bSub=new JButton("-"));
pan3.add(bRec=new JButton("1/x"));
pan3.add(bMa=new JButton("M+"));
pan3.add(b0=new JButton("0"));
pan3.add(bNeg=new JButton("+/-"));
pan3.add(bPnt=new JButton("."));
pan3.add(bAdd=new JButton("+"));
pan3.add(bEql=new JButton("="));
this.pack();
}
}
class JLab1201 extends Face implements ActionListener{
private double one=0;
private String other="";
private String out="";
private double r=10;
JLab1201(){
super("计算器");
bBack.addActionListener(this);
bCe.addActionListener(this);
bC.addActionListener(this);
bMc.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bDiv.addActionListener(this);
bSqrt.addActionListener(this);
bMr.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
bMul.addActionListener(this);
bPen.addActionListener(this);
bMs.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
bSub.addActionListener(this);
bRec.addActionListener(this);
bMs.addActionListener(this);
b0.addActionListener(this);
bNeg.addActionListener(this);
bPnt.addActionListener(this);
bAdd.addActionListener(this);
bEql.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
try{
other=e.getActionCommand().toString();
if(other=="0"||other=="1"||other=="2"||other=="3"||other=="4"||
other=="5"||other=="6"||other=="7"||other=="8"||other=="9"
){
one=one*r+Double.parseDouble(other);
text.setText(Double.toString(one));
}
if(other=="."){
r=0.1;
}
if(other=="Backspace"){
one-=one%r;
text.setText(Double.toString(one));
}
if(other=="C"){
one=0;
r=10;
text.setText("0.");
}
if(other=="sqrt"){
one=Math.sqrt(one);
text.setText(Double.toString(one));
}
if(other=="%"){
one/=100;
text.setText(Double.toString(one));
}
if(other=="+/-"){
one*=-1;
text.setText(Double.toString(one));
}
}
catch(Exception ex){
}
}
public static void main(String []args){
JLab1201 xie=new JLab1201();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -