- /**********************************************
- *JAVA计算器测试0.5
- *
- *
- *徐方鑫2010.3.5
- *
- ***********************************************/
- importjavax.swing.*;
- importjava.awt.*;
- importjava.awt.event.*;
- publicclassUI{
- JTextFieldresult;
- Stringnumber;
- doublenumber1,number2,numberResult;
- charflag;
- booleanflag2;
- publicUI(){
- //TODOAuto-generatedconstructorstub
- //初始化全部参数
- number="";
- number1=0;
- number2=0;
- numberResult=0;
- flag='';
- flag2=false;
- //绘制面板
- JFrameframe=newJFrame("计算器");//主窗体
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭按钮
- JPanelmainPanel=newJPanel();//主面板
- BoxboxMain=Box.createVerticalBox();//BOX结构——主面板竖直分布
- frame.setContentPane(mainPanel);//选择面板为主面板
- BoxboxMenu=Box.createHorizontalBox();//菜单面板
- mainPanel.add(boxMain);//主面板上增加BOX主面板
- boxMain.add(boxMenu);//BOX主面板增加菜单面板
- //菜单条
- JMenuBarmenuBar=newJMenuBar();
- boxMenu.add(menuBar);
- JMenumenu1=newJMenu("查看");
- JMenumenu2=newJMenu("编辑");
- JMenumenu3=newJMenu("帮助");
- menuBar.add(menu1);
- menuBar.add(menu2);
- menuBar.add(menu3);
- JMenuItemmenu1Item1=newJMenuItem("清除");
- menu1Item1.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number="";
- number1=0;
- number2=0;
- numberResult=0;
- flag='';
- flag2=false;
- result.setText(number);
- }
- });
- JMenuItemmenu1Item2=newJMenuItem("等于");
- menu1Item2.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number2=Integer.parseInt(number);
- switch(flag)
- {
- case'+':
- numberResult=number1+number2;
- break;
- case'-':
- numberResult=number1-number2;;
- break;
- case'*':
- numberResult=number1*number2;;
- break;
- case'/':
- numberResult=number1/number2;;
- break;
- }
- number=String.valueOf(numberResult);
- result.setText(number);
- number="";
- number1=0;
- number2=0;
- numberResult=0;
- flag='';
- flag2=false;
- }
- });
- JMenuItemmenu1Item3=newJMenuItem("退出");
- menu1Item3.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- System.exit(0);
- }
- });
- menu1.add(menu1Item1);
- menu1.add(menu1Item2);
- menu1.addSeparator();
- menu1.add(menu1Item3);
- JMenuItemmenu2Item1=newJMenuItem("复制");
- JMenuItemmenu2Item2=newJMenuItem("粘帖");
- JMenuItemmenu2Item3=newJMenuItem("历史记录");
- menu2.add(menu2Item1);
- menu2.add(menu2Item2);
- menu2.addSeparator();
- menu2.add(menu2Item3);
- JMenuItemmenu3Item1=newJMenuItem("查看帮助");
- menu3Item1.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- JOptionPane.showMessageDialog(null,"帮助制作中");
- }
- });
- JMenuItemmenu3Item2=newJMenuItem("关于计算器");
- menu3Item2.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- JOptionPane.showMessageDialog(null,"计算器BATA0.5");
- }
- });
- JMenuItemmenu3Item3=newJMenuItem("制作人信息");
- menu3Item3.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- JOptionPane.showMessageDialog(null,"制作人:徐方鑫/n邮箱:280599580@qq.com");
- }
- });
- menu3.add(menu3Item1);
- menu3.add(menu3Item2);
- menu3.addSeparator();
- menu3.add(menu3Item3);
- //显示框
- BoxtextBox=Box.createHorizontalBox();
- boxMain.add(textBox);
- //JLabeltextLable=newJLabel("显示");
- //textBox.add(textLable);
- //JTextField
- result=newJTextField();
- result.setHorizontalAlignment(JTextField.RIGHT);
- textBox.add(result);
- //按钮面板
- BoxbuttonBoxMain=Box.createHorizontalBox();
- boxMain.add(buttonBoxMain);
- BoxbuttonBoxTouchMain=Box.createVerticalBox();
- buttonBoxMain.add(buttonBoxTouchMain);
- BoxbuttonBoxTouchMainIn1=Box.createHorizontalBox();
- BoxbuttonBoxTouchMainIn2=Box.createHorizontalBox();
- BoxbuttonBoxTouchMainIn3=Box.createHorizontalBox();
- BoxbuttonBoxTouchMainIn4=Box.createHorizontalBox();
- buttonBoxTouchMain.add(buttonBoxTouchMainIn1);
- JButtonbutton_7=newJButton("7");
- buttonBoxTouchMainIn1.add(button_7);
- button_7.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="7";
- result.setText(number);
- }
- });
- JButtonbutton_8=newJButton("8");
- buttonBoxTouchMainIn1.add(button_8);
- button_8.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="8";
- result.setText(number);
- }
- });
- JButtonbutton_9=newJButton("9");
- buttonBoxTouchMainIn1.add(button_9);
- button_9.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="9";
- result.setText(number);
- }
- });
- JButtonbutton_10=newJButton("加");
- buttonBoxTouchMainIn1.add(button_10);
- //加法调用
- button_10.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number1=Double.parseDouble(number);
- flag='+';
- number="";
- flag2=false;
- result.setText(number);
- }
- });
- buttonBoxTouchMain.add(buttonBoxTouchMainIn2);
- JButtonbutton_4=newJButton("4");
- buttonBoxTouchMainIn2.add(button_4);
- button_4.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="4";
- result.setText(number);
- }
- });
- JButtonbutton_5=newJButton("5");
- buttonBoxTouchMainIn2.add(button_5);
- button_5.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="5";
- result.setText(number);
- }
- });
- JButtonbutton_6=newJButton("6");
- buttonBoxTouchMainIn2.add(button_6);
- button_6.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="6";
- result.setText(number);
- }
- });
- JButtonbutton_11=newJButton("减");
- buttonBoxTouchMainIn2.add(button_11);
- //减法调用
- button_11.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number1=Double.parseDouble(number);
- flag='-';
- number="";
- flag2=false;
- result.setText(number);
- }
- });
- buttonBoxTouchMain.add(buttonBoxTouchMainIn3);
- JButtonbutton_1=newJButton("1");
- buttonBoxTouchMainIn3.add(button_1);
- button_1.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="1";
- result.setText(number);
- }
- });
- JButtonbutton_2=newJButton("2");
- buttonBoxTouchMainIn3.add(button_2);
- button_2.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="2";
- result.setText(number);
- }
- });
- JButtonbutton_3=newJButton("3");
- buttonBoxTouchMainIn3.add(button_3);
- button_3.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="3";
- result.setText(number);
- }
- });
- JButtonbutton_12=newJButton("乘");
- buttonBoxTouchMainIn3.add(button_12);
- //乘法调用
- button_12.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number1=Double.parseDouble(number);
- flag='*';
- number="";
- flag2=false;
- result.setText(number);
- }
- });
- buttonBoxTouchMain.add(buttonBoxTouchMainIn4);
- JButtonbutton_0=newJButton("0");
- buttonBoxTouchMainIn4.add(button_0);
- button_0.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number+="0";
- result.setText(number);
- }
- });
- JButtonbutton_13=newJButton(".");
- buttonBoxTouchMainIn4.add(button_13);
- //。调用
- button_13.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- if(flag2==false)
- {
- number+=".";
- result.setText(number);
- flag2=true;
- }
- }
- });
- JButtonbutton_14=newJButton("除");
- buttonBoxTouchMainIn4.add(button_14);
- //除法调用
- button_14.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number1=Double.parseDouble(number);
- flag='/';
- number="";
- flag2=false;
- result.setText(number);
- }
- });
- BoxbuttonBoxTouchResult=Box.createVerticalBox();
- buttonBoxMain.add(buttonBoxTouchResult);
- JButtonbutton_15=newJButton("清除");
- buttonBoxTouchResult.add(button_15);
- button_15.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number="";
- number1=0;
- number2=0;
- numberResult=0;
- flag='';
- result.setText(number);
- flag2=false;
- }
- });
- JButtonbutton_16=newJButton("负号");
- buttonBoxTouchResult.add(button_16);
- JButtonbutton_17=newJButton("百分");
- buttonBoxTouchResult.add(button_17);
- JButtonbutton_18=newJButton("等于");
- buttonBoxTouchResult.add(button_18);
- button_18.addActionListener(newActionListener()
- {
- publicvoidactionPerformed(ActionEvente)
- {
- number2=Double.parseDouble(number);
- switch(flag)
- {
- case'+':
- numberResult=number1+number2;
- break;
- case'-':
- numberResult=number1-number2;;
- break;
- case'*':
- numberResult=number1*number2;;
- break;
- case'/':
- numberResult=number1/number2;;
- break;
- }
- number=String.valueOf(numberResult);
- result.setText(number);
- number="";
- number1=0;
- number2=0;
- numberResult=0;
- flag='';
- flag2=false;
- }
- });
- frame.setSize(290,215);
- frame.setVisible(true);
- }
- }
开源一个简单的计算器
最新推荐文章于 2024-06-20 09:36:38 发布