GUI加减乘除计算

来源:互联网 发布:排刀数控车床编程实例 编辑:程序博客网 时间:2024/06/11 08:59

刚刚接触到GUI的时候,似懂非懂。不过后来写的多了,做的多了,也就明白啦!下面的程序大家可以亲自运行一下啊

packagea;

//importjava.awt.*;

importjava.awt.Button;

importjava.awt.Choice;

importjava.awt.Component;

importjava.awt.Frame;

importjava.awt.GridLayout;

importjava.awt.Label;

importjava.awt.TextField;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.util.Vector;

 

importjavax.swing.ComboBoxModel;

importjavax.swing.JComboBox;

importorg.omg.CORBA.PUBLIC_MEMBER;

 

importcom.sun.org.apache.bcel.internal.generic.NEW;

importcom.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

publicclass AddFrame extends Frame

{

  public static void main(String args[])

  {

     AddFrame  b=new AddFrame ();

  }

  

  TextField num1=new TextField();

  TextField num2=new TextField();

  TextField num3=new TextField();

  Choice osChoice=new Choice();

 

  public AddFrame(){

     setName("addFrame");

     setSize(500,500);

     setVisible(true);

     initView();

      }

  

   void initView()

   {

      Button okButton=newButton("计算");

      Button exitButton=newButton("退出");

      this.setLayout(newGridLayout(5,2));

      add(newLabel("第一个数:"));

      add(num1);

     

      add(newLabel("符号:"));

     

     osChoice.add("+");

     osChoice.add("-");

     osChoice.add("*");

     osChoice.add("/");

     osChoice.add("%");

     add(osChoice); 

     

     

      add(newLabel("第二个数:"));

      add(num2);

     

      add(newLabel("结果:"));

      add(num3);

     

      add(okButton);

     add(exitButton);

     

     

     

      AddListener addListener=newAddListener();

     okButton.addActionListener(addListener);

     

   

      ExitListener listener=newExitListener();

     exitButton.addActionListener(listener);

     

      }

   class AddListener implements ActionListener

   {

 

     private static final boolean String = false;

 

     public void actionPerformed(ActionEvent e)

       {

       String str1=num1.getText();

       String str2=num2.getText();

       if("+"==osChoice.getSelectedItem())

       {

       intresult=Integer.parseInt(str1)+Integer.parseInt(str2);

       num3.setText(Integer.toString(result));

       }

       if("-"==osChoice.getSelectedItem())

       {

         intresult=Integer.parseInt(str1)-Integer.parseInt(str2);

         num3.setText(Integer.toString(result));

       }

       if("*"==osChoice.getSelectedItem())

       {

         intresult=Integer.parseInt(str1)*Integer.parseInt(str2);

         num3.setText(Integer.toString(result));

       }

       if("/"==osChoice.getSelectedItem())

       {

         doubleresult=(Integer.parseInt(str1)/Integer.parseInt(str2));

         num3.setText(Double.toString(result));

       }

       if("%"==osChoice.getSelectedItem())

       {

         intresult=Integer.parseInt(str1)%Integer.parseInt(str2);

         num3.setText(Integer.toString(result));

       }

     }

   }

   class ExitListener implements ActionListener

   {

 

      public voidactionPerformed(ActionEvent e)

      {

 

        System.exit(0);

      }

  

  }

}

原创粉丝点击