熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Swing寫的簡單的計算器

2022-06-13   來源: Java核心技術 

   import javaawteventActionEvent;

   import javaawteventActionListener;

   import javaawtContainer;

   import javaawtFlowLayout;

   import javaxswingJFrame;

   import javaxswingJTextField;

   import javaxswingJButton;

   public class Computer {

  

       /**

        * @param args

        */

       private static int numClicks=;

       public static void main(String[] args) {

           // TODO Autogenerated method stub

           final JFrame jf=new JFrame(計算器);

           final Container c=jfgetContentPane();

           csetLayout(new FlowLayout(FlowLayoutCENTER));

           final JTextField f=new JTextField();

           final JTextField f=new JTextField();

           final JTextField f=new JTextField();

           final JButton b=new JButton(+);//內部類如果要使用外部類的變量必須使用final修飾符

           final JButton b=new JButton(=);

           baddActionListener(new ActionListener(){

               public void actionPerformed(ActionEvent e)

               {

                   numClicks++;

                   switch(numClicks%)

                   {

                   case :bsetText(+);break;

                   case :bsetText();break;

                   case :bsetText(*);break;

                   case :bsetText(/);break;

                   }

               }

           });

           baddActionListener(new ActionListener(){

               public void actionPerformed(ActionEvent e)

               {

                   int n=IntegerparseInt(fgetText());

                   int n=IntegerparseInt(fgetText());

                   int n=n+n;

                   switch(numClicks%)

                   {

                   case :n=n+n;break;

                   case :n=nn;break;

                   case :n=n*n;break;

                   case :n=n/n;break;

                   }

                   fsetText(StringvalueOf(n));

               }

           });

           cadd(f);

           cadd(b);

           cadd(f);

           cadd(b);

           cadd(f);

           jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

           jfsetSize( );

           jfsetVisible(true);

       }

   }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26463.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.