📄 a.java
字号:
import java.awt.*;
import java.awt.event.*;
class This_number_too_big extends Exception
{}//自定义的一个结果溢出异常类
class Jsp2003 extends Frame implements ItemListener,ActionListener {
public Jsp2003() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
static Jsp2003 mainFrame = new Jsp2003();
static Label lab=new Label("0");
static Panel pnl1=new Panel(new GridLayout(4,3,3,3));
static Panel pnl2=new Panel(new GridLayout(4,1,3,3));
static Panel pnl3=new Panel(new GridLayout(1,2,3,3));
static Panel pnl4=new Panel(new GridLayout(6,1,3,3));
static Button bt9=new Button("9");
static Button bt8=new Button("8");
static Button bt7=new Button("7");
static Button bt6=new Button("6");
static Button bt5=new Button("5");
static Button bt4=new Button("4");
static Button bt3=new Button("3");
static Button bt2=new Button("2");
static Button bt1=new Button("1");
static Button bt0=new Button("0");
static Button btdot=new Button(".");
static Button btequ=new Button("=");
static Button btadd=new Button("+");
static Button btsub=new Button("-");
static Button btmul=new Button("*");
static Button btdev=new Button("/");
static Button btA=new Button("A");
static Button btB=new Button("B");
static Button btC=new Button("C");
static Button btD=new Button("D");
static Button btE=new Button("E");
static Button btF=new Button("F");
static Checkbox ckb8=new Checkbox("八进制");
static Checkbox ckb10=new Checkbox("十进制");
static Checkbox ckb16=new Checkbox("十六进制");
static Button btc=new Button("clear");
static Button btt=new Button("backspace");
public int number=0,length=18;
//number是用来记录输入的数字个数,length是用来设置在不同进制下允许输入的最多数字位数,默认为十进制
public char mark='n'; //设置运算符号为没有即为‘n’,它的值是‘+’‘-’‘*’‘/’
public double data=0; //设置运算数据为零
public char ch; //用来临时存放所按按钮的第一个字符
public boolean Refresh=false;//设置lab是否要在输入数据的时候刷新,初始为否
public static void main(String args[]) {
System.out.println("Starting Jsp2003...");
lab.setAlignment(Label.RIGHT);
lab.setBackground(Color.lightGray);
lab.setForeground(Color.BLUE);
lab.setFont(new Font("Serief",Font.BOLD,18));
lab.setBounds(14,33,216,40);
mainFrame.add(lab);
CheckboxGroup grp=new CheckboxGroup();
ckb8.setCheckboxGroup(grp);
ckb10.setCheckboxGroup(grp);
ckb16.setCheckboxGroup(grp);
ckb8.setBounds(14,75,55,25);
ckb10.setBounds(69,75,55,25);
ckb16.setBounds(124,75,65,25);
ckb8.setState(false);
ckb10.setState(true);
ckb16.setState(false);
mainFrame.add(ckb8);
mainFrame.add(ckb10);
mainFrame.add(ckb16);
pnl1.setBounds(14,140,120,150);
pnl2.setBounds(144,140,40,150);
pnl3.setBounds(14,100,170,36);
pnl4.setBounds(190,100,40,190);
pnl1.add(bt7);
pnl1.add(bt8);
pnl1.add(bt9);
pnl1.add(bt4);
pnl1.add(bt5);
pnl1.add(bt6);
pnl1.add(bt1);
pnl1.add(bt2);
pnl1.add(bt3);
pnl1.add(bt0);
pnl1.add(btdot);
pnl1.add(btequ);
pnl2.add(btadd);
pnl2.add(btsub);
pnl2.add(btmul);
pnl2.add(btdev);
pnl3.add(btt);
pnl3.add(btc);
pnl4.add(btA);
pnl4.add(btB);
pnl4.add(btC);
pnl4.add(btD);
pnl4.add(btE);
pnl4.add(btF);
btA.enable(false);
btB.enable(false);
btC.enable(false);
btD.enable(false);
btE.enable(false);
btF.enable(false);
mainFrame.add(pnl1);
mainFrame.add(pnl2);
mainFrame.add(pnl3);
mainFrame.add(pnl4);
mainFrame.setResizable(false);
mainFrame.setLayout(null);
mainFrame.setSize(240,300 );
mainFrame.setTitle("计算器");
mainFrame.setVisible(true);
ckb8.addItemListener(mainFrame);
ckb10.addItemListener(mainFrame);
ckb16.addItemListener(mainFrame);
//把事件聆听者向各个组键注册
bt1.addActionListener(mainFrame);
bt2.addActionListener(mainFrame);
bt3.addActionListener(mainFrame);
bt4.addActionListener(mainFrame);
bt5.addActionListener(mainFrame);
bt6.addActionListener(mainFrame);
bt7.addActionListener(mainFrame);
bt8.addActionListener(mainFrame);
bt9.addActionListener(mainFrame);
bt0.addActionListener(mainFrame);
btadd.addActionListener(mainFrame);
btsub.addActionListener(mainFrame);
btmul.addActionListener(mainFrame);
btdev.addActionListener(mainFrame);
btt.addActionListener(mainFrame);
btc.addActionListener(mainFrame);
btdot.addActionListener(mainFrame);
btequ.addActionListener(mainFrame);
btA.addActionListener(mainFrame);
btB.addActionListener(mainFrame);
btC.addActionListener(mainFrame);
btD.addActionListener(mainFrame);
btE.addActionListener(mainFrame);
btF.addActionListener(mainFrame);
}// end main() mothed
//---------------------------------------------
//checkbox 事件的处理
public void itemStateChanged(ItemEvent e)
{ mark='n';
Refresh=false;
//规定当进制转换时以前输入的运算符失效
if (ckb8.getState()==true)
{ btA.enable(false);
btB.enable(false);
btC.enable(false);
btD.enable(false);
btE.enable(false);
btF.enable(false);
bt9.enable(false);
bt8.enable(false);
btdot.enable(false);
btadd.enable(false);
btmul.enable(false);
btsub.enable(false);
btdev.enable(false);
btequ.enable(false);
//在八进制的情况下对有些按件的管理
if(length==18) { lab.setText(goto10_8(lab.getText()));
number=lab.getText().length();
}
if(length==14) { lab.setText(goto_10(lab.getText(),16L));
lab.setText(goto10_8(lab.getText()));
number=lab.getText().length();
}
//将其他进制转换成八进制
length=19;
//在八进制下允许输入数字个数最多为19位
}
if(ckb10.getState()==true)
{ btA.enable(false);
btB.enable(false);
btC.enable(false);
btD.enable(false);
btE.enable(false);
btF.enable(false);
bt9.enable(true);
bt8.enable(true);
btdot.enable(true);
btadd.enable(true);
btmul.enable(true);
btsub.enable(true);
btdev.enable(true);
btequ.enable(true);
//在十进制的情况下对有些按件的管理
if(length==19) { lab.setText(goto_10(lab.getText(),8L));
number=lab.getText().length();}
if(length==14) { lab.setText(goto_10(lab.getText(),16L));
number=lab.getText().length();}
//进制转换成十进制
length=18;
//在十进制下允许输入数字个数最多为18位
}
if(ckb16.getState()==true)
{ btA.enable(true);
btB.enable(true);
btC.enable(true);
btD.enable(true);
btE.enable(true);
btF.enable(true);
bt9.enable(true);
bt8.enable(true);
btdot.enable(false);
btadd.enable(false);
btmul.enable(false);
btsub.enable(false);
btdev.enable(false);
btequ.enable(false);
//在十六进制的情况下对有些按件的管理
if(length==18) { lab.setText(goto10_16(lab.getText()));
number=lab.getText().length();}
if(length==19) { lab.setText(goto_10(lab.getText(),8L));
lab.setText(goto10_16(lab.getText()));
number=lab.getText().length();}
//将其他进制转换成十六进制
length=14;
//在十六进制下允许输入数字个数最多为14位
}
}//end itemStateChanged(ItemEvent e) method
//------------------------------------------------------------ //按钮事件的处理
public void actionPerformed(ActionEvent m)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -