带有八进制和十六进制转换的计算器
截图
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();
it(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("");
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);
able(false);
able(false);
able(false);
able(false);
able(false);
able(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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论