發新話題

[教學]JAVA展示利用JPanel做成的計算機

[教學]JAVA展示利用JPanel做成的計算機

引用:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ui19 extends JFrame implements ActionListener
{
    JButton bot[]=new JButton[10];
    JButton bot_dt=new JButton(".");
    JButton bot_a=new JButton("+");
    JButton bot_s=new JButton("-");
    JButton bot_m=new JButton("*");
    JButton bot_d=new JButton("/");
    JButton bot_e=new JButton("=");
    JTextField txt;
    Container c;
    JPanel jpl=new JPanel();
    String name;
    double A=0,B=0;
    int op=0,dot=0;
    public ui19()
    {
        super("UI 版面配置");
        int i;
        for (i=0;i<10;i=i+1)
        {
            bot=new JButton(Integer.toString(i));
        }
        txt=new JTextField(10);
        txt.setEditable(false);//設定成不能修改
        c=getContentPane();
        c.setLayout(new BorderLayout());//設定為用Borderlayout
        jpl.setLayout(new GridLayout(4,4));//設定為用gridlayout
        c.add(txt,BorderLayout.NORTH);
        c.add(jpl,BorderLayout.CENTER);
        //注意加入元件的順序
        for (i=0;i<10;i=i+1)
        {
            jpl.add(bot);
            bot.addActionListener(this);
        }
        jpl.add(bot_dt);
        bot_dt.addActionListener(this);
        jpl.add(bot_a);
        bot_a.addActionListener(this);
        jpl.add(bot_s);
        bot_s.addActionListener(this);
        jpl.add(bot_m);
        bot_m.addActionListener(this);
        jpl.add(bot_d);
        bot_d.addActionListener(this);
        jpl.add(bot_e);
        bot_e.addActionListener(this);
        setSize(600,480);
        show();
    }
    public void paint(Graphics g)
    {
        super.paint(g);
    }
   /***按鈕事件的傾聽方法****/
    public void actionPerformed(ActionEvent e) //按鈕事件的處理方法
    {
        int i;
        for (i=0;i<10;i=i+1)
        {
            if (e.getSource()==bot)
            {
                txt.setText(txt.getText()+Integer.toString(i));
                A=Double.parseDouble(txt.getText());
                return;
            }
        }
        if (e.getSource()==bot_a)
        {
            txt.setText("0");
            B=A;
            A=0;
            op=1;//add
            dot=0;
        }
        else if (e.getSource()==bot_s)
        {
            txt.setText("0");
            B=A;
            A=0;
            op=2;//減
            dot=0;
        }
        else if (e.getSource()==bot_m)
        {
            txt.setText("0");
            B=A;
            A=0;
            op=3;//乘
            dot=0;
        }
        else if (e.getSource()==bot_d)
        {
            txt.setText("0");
            B=A;
            A=0;
            op=4;//除
            dot=0;
        }
        else if (e.getSource()==bot_e)
        {
            txt.setText("=");
            if (op==0) A=B;
            else if (op==1) A=B+A;
            else if (op==2) A=B-A;
            else if (op==3) A=B*A;
            else if (op==4) A=B/A;
            txt.setText(Double.toString(A));
            dot=0;
        }
        else if (e.getSource()==bot_dt)
        {
            txt.setText(txt.getText()+".");
            dot=1;
        }
    }
   
    public static void main(String args[]) //程式起點
    {
        ui19 app=new ui19(); //畫圖
        //處理視窗關閉要求
        app.addWindowListener(
         new WindowAdapter()
         {
            public void windowClosing(WindowEvent e)
            {  System.exit(0);}
         });
    }
}
[ 本帖最後由 philxyz0316 於 2006-8-8 21:09 編輯 ]

TOP

引用:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import java.text.*;
import java.util.*;

public class test extends JFrame {
   
   private JPanel buttonPanel,fieldPanel;
   
   private JButton buttons0,buttons1,buttons2,buttons3,buttons4,buttons5,
                   buttons6,buttons7,buttons8,buttons9,buttonsp,//0~9~.
                   buttonse,buttonso,buttonsac,//=,c,ac
                   buttonsa,buttonsb,buttonsc,buttonsd,//+   -   *   /
                   buttonsdel,buttonssin;//del   -
   
   private String tmp1 = new String("");//TEMP1暫存字串
   private String tmp2 = new String("0");//TEMP2暫存字串初始為0
   private int op = 0;//把按入四則運算的數值直接作家法運算的state
   private int op2= 0;//連續計算狀態
   private int pt = 0;//小數點的state
   private int optr = 1;//四則運算的state
   private int flag = 0;//按下等於後再輸入數字的state
   private int sign = 0;//負數的state
   private double num1;//輸入暫存變數
   private double num2;//答案暫存變數

   private JTextField label1;
   
   public test()
   {
      super( "我是計算機" );

   
      Container container = getContentPane();

      Icon bug0   =new ImageIcon("gif0411.gif");
      Icon bug1   =new ImageIcon("gif0422.gif");
      Icon bug2   =new ImageIcon("gif0433.gif");
      Icon bug3   =new ImageIcon("gif0444.gif");
      Icon bug4   =new ImageIcon("gif0455.gif");
      Icon bug5   =new ImageIcon("gif0466.gif");
      Icon bug6   =new ImageIcon("gif0477.gif");
      Icon bug7   =new ImageIcon("gif0488.gif");
      Icon bug8   =new ImageIcon("gif0499.gif");
      Icon bug9   =new ImageIcon("gif0500.gif");
      Icon buga   =new ImageIcon("gif051.gif");
      Icon bugb   =new ImageIcon("gif052.gif");
      Icon bugc   =new ImageIcon("gif053.gif");
      Icon bugd   =new ImageIcon("gif054.gif");
      Icon buge   =new ImageIcon("gif055.gif");
      Icon bugo   =new ImageIcon("gif056.gif");
      Icon bugp   =new ImageIcon("gif057.gif");
      Icon bugac  =new ImageIcon("gif058.gif");
      Icon bugsign=new ImageIcon("gif059.gif");
      Icon bugdel =new ImageIcon("gif060.gif");
      
      Icon bug00  =new ImageIcon("gif041.gif");
      Icon bug11  =new ImageIcon("gif042.gif");
      Icon bug22  =new ImageIcon("gif043.gif");
      Icon bug33  =new ImageIcon("gif044.gif");
      Icon bug44  =new ImageIcon("gif045.gif");
      Icon bug55  =new ImageIcon("gif046.gif");
      Icon bug66  =new ImageIcon("gif047.gif");
      Icon bug77  =new ImageIcon("gif048.gif");
      Icon bug88  =new ImageIcon("gif049.gif");
      Icon bug99  =new ImageIcon("gif050.gif");
      
      buttons0=new JButton(bug0);
              buttons0.setRolloverIcon(bug00);
      buttons1=new JButton(bug1);
                buttons1.setRolloverIcon(bug11);
      buttons2=new JButton(bug2);
                buttons2.setRolloverIcon(bug22);
      buttons3=new JButton(bug3);
                buttons3.setRolloverIcon(bug33);
      buttons4=new JButton(bug4);
                buttons4.setRolloverIcon(bug44);
      buttons5=new JButton(bug5);
                buttons5.setRolloverIcon(bug55);
      buttons6=new JButton(bug6);
                buttons6.setRolloverIcon(bug66);
      buttons7=new JButton(bug7);
                buttons7.setRolloverIcon(bug77);
      buttons8=new JButton(bug8);
                buttons8.setRolloverIcon(bug88);
      buttons9=new JButton(bug9);
                buttons9.setRolloverIcon(bug99);
      
      buttonse=new JButton(buge);
      buttonso=new JButton(bugo);
      buttonsa=new JButton(buga);
      buttonsb=new JButton(bugb);
      buttonsc=new JButton(bugc);
      buttonsd=new JButton(bugd);
      buttonsp=new JButton(bugp);
      buttonsdel=new JButton(bugdel);
      buttonssin=new JButton(bugsign);
          buttonsac=new JButton(bugac);
      
          label1=new JTextField("0");
          label1.setHorizontalAlignment( JTextField.RIGHT );
         
      buttonPanel = new JPanel();
      fieldPanel = new JPanel();

      buttonPanel.setLayout( new GridLayout(4,4,5,32 ));
      fieldPanel.setLayout( new GridLayout() );

      fieldPanel.add(label1);
      
      buttonPanel.add(buttons1);
      buttonPanel.add(buttons2);
      buttonPanel.add(buttons3);
      buttonPanel.add(buttonsa);
      buttonPanel.add(buttonsac);
      buttonPanel.add(buttons4);
      buttonPanel.add(buttons5);
      buttonPanel.add(buttons6);
      buttonPanel.add(buttonsb);
      buttonPanel.add(buttonso);
      buttonPanel.add(buttons7);
      buttonPanel.add(buttons8);
      buttonPanel.add(buttons9);
      buttonPanel.add(buttonsc);
      buttonPanel.add(buttonsdel);
      buttonPanel.add(buttons0);
      buttonPanel.add(buttonsp);
      buttonPanel.add(buttonse);
      buttonPanel.add(buttonsd);
      buttonPanel.add(buttonssin);
      
          ButtonHandler handler = new ButtonHandler();
      
      buttons0.addActionListener( handler );
      buttons1.addActionListener( handler );
      buttons2.addActionListener( handler );
      buttons3.addActionListener( handler );
      buttons4.addActionListener( handler );
      buttons5.addActionListener( handler );
      buttons6.addActionListener( handler );
      buttons7.addActionListener( handler );
      buttons8.addActionListener( handler );
      buttons9.addActionListener( handler );
      buttonsp.addActionListener( handler );
      buttonsa.addActionListener( handler );
      buttonsb.addActionListener( handler );
      buttonsc.addActionListener( handler );
      buttonsd.addActionListener( handler );
      buttonse.addActionListener( handler );
      buttonso.addActionListener( handler );
      buttonsdel.addActionListener( handler );
      buttonssin.addActionListener( handler );
      buttonsac.addActionListener( handler );

      container.add( buttonPanel , BorderLayout.CENTER);
      container.add( fieldPanel , BorderLayout.NORTH);

      setSize(300,350);
      setVisible(true);

   }//end 建構式

   public static void main( String args[] )
   {
      test t = new test();
      
      t.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }//end main
   
   private class ButtonHandler implements ActionListener {

      public void actionPerformed( ActionEvent e )
      {
                        
       if (e.getSource()==buttonso)//清除
       {                           //把全部的狀態重設為0並重新輸入
                   label1.setText("0");           
                    tmp1 = "";
                    pt = 0;
                    sign = 0;
       }//end if
       if (e.getSource()==buttonsac)//ac
       {                                                        //把全部的資料清空並呼叫begin狀態初始設定
                   label1.setText("0");
                    begin();
       }//end if  
       if (e.getSource()==buttons1)//1
       {
                    op = 0;                                   //設定op state為0
                    
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if(label1.getText().equals("0"))
                    {
                      label1.setText("");  //先清空輸入資料
                      flag = 0;                           //把flag state設定為0
                    }
                    
                    if (flag == 1)                   //如果是運算過後的資料劉在field內
                    {
                      label1.setText("");  //先清空輸入資料
                      flag = 0;                           //把flag state設定為0
                    }
                    tmp1 = label1.getText();//取得目前field內的內容
                    tmp1 += "1";                        //並更新其內容為累加1
                    label1.setText(tmp1);        //輸出字串
       }//end if
       if (e.getSource()==buttons2)//2
       {
                    op = 0;                                   
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if(label1.getText().equals("0"))
                    {
                      label1.setText("");  
                      flag = 0;                           
                    }
                    if (flag == 1)                  
                    {
                      label1.setText("");
                      flag = 0;
                    }
                    tmp1 = label1.getText();
                    tmp1 += "2";
                    label1.setText(tmp1);
       }//end if
       if (e.getSource()==buttons3)//3
       {
                    op = 0;
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if(label1.getText().equals("0"))
                    {
                      label1.setText("");  
                      flag = 0;                           
                    }
                    if (flag == 1)
                    {
                      label1.setText("");
                      flag = 0;
                    }
                    tmp1 = label1.getText();
                    tmp1 += "3";
                    label1.setText(tmp1);
       }//end if
       if (e.getSource()==buttons4)//4
       {
                    op = 0;
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if(label1.getText().equals("0"))
                    {
                      label1.setText("");  
                      flag = 0;                           
                    }
                    if (flag == 1)
                    {
                      label1.setText("");
                      flag = 0;
                    }
                    tmp1 = label1.getText();
                    tmp1 += "4";
                    label1.setText(tmp1);
       }//end if
       if (e.getSource()==buttons5)//5
       {
                    op = 0;
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if(label1.getText().equals("0"))
                    {
                      label1.setText("");  
                      flag = 0;                           
                    }
                    if (flag == 1)
                    {
                      label1.setText("");
                      flag = 0;
                    }
                    tmp1 = label1.getText();
                    tmp1 += "5";
                    label1.setText(tmp1);
       }//end if
       if (e.getSource()==buttons6)//6
       {
                    op = 0;
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if(label1.getText().equals("0"))
                    {
                      label1.setText("");  
                      flag = 0;                           
                    }
                    if (flag == 1)
                    {
                      label1.setText("");
                      flag = 0;
                    }
                    tmp1 = label1.getText();
                    tmp1 += "6";
                    label1.setText(tmp1);
       }//end if
       if (e.getSource()==buttons7)//7
       {
                    op = 0;
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if(label1.getText().equals("0"))
                    {
                      label1.setText("");  
                      flag = 0;                           
                    }
                    if (flag == 1)
                    {
                      label1.setText("");
                      flag = 0;
                    }
                    tmp1 = label1.getText();
                    tmp1 += "7";
                    label1.setText(tmp1);
       }//end if
       if (e.getSource()==buttons8)//8
       {
                    op = 0;
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if(label1.getText().equals("0"))
                    {
                      label1.setText("");  
                      flag = 0;                           
                    }
                    if (flag == 1)
                    {
                      label1.setText("");
                      flag = 0;
                    }
                    tmp1 = label1.getText();
                    tmp1 += "8";
                    label1.setText(tmp1);
       }//end if
       if (e.getSource()==buttons9)//9
       {
                    op = 0;
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if(label1.getText().equals("0"))
                    {
                      label1.setText("");  
                      flag = 0;                           
                    }
                    if (flag == 1)
                    {
                      label1.setText("");
                      flag = 0;
                    }
                    tmp1 = label1.getText();
                    tmp1 += "9";
                    label1.setText(tmp1);
       }//end if
       if (e.getSource()==buttons0)//0
       {
                    op = 0;
                    if (op2 == 1)                  
                    {
                      begin();
                      label1.setText("");  
                      op2 = 0;                           
                    }
                    if (flag == 1)
                    {
                      label1.setText("");
                      flag = 0;
                    }//
                    if (tmp1.equals("0") != true)//如果目前內容不為0
                    {
                      tmp1 = label1.getText();         //取得目前資料內容
                      tmp1 = tmp1 + "0";                 //把目前資料累加0
                      label1.setText(tmp1);                 //輸出運算過後的內容
                    }
       }//end if
       if (e.getSource()==buttonsp)//點
       {
                   if (pt == 0)                                 //判斷小數點是否輸入過
                   {                                                         //如果沒有輸入過
                      tmp1 = label1.getText();   //先取得目前資料內容
                      if (tmp1.equals(""))                 //如果沒有資料的話
                      {
                        tmp1 = "0" + "." + tmp1; //在小數點前面加0
                      }else tmp1 = tmp1 + ".";   //如果目前資料不為0在目前資料後面加小數點
                    
                            label1.setText(tmp1);    //輸出運算過後的資料
                            pt = 1;                                         //把pt state設定為1
                    }
           }//end if
       if (e.getSource()==buttonsd)//除
       {
                   op++;                        //把op狀態累加為1
                    calculator(tmp1);            //呼叫calculator()把目前的資料內容加起來
                    optr = 4;                    //設定運算狀態為4做除法
                    op2=0;
       }//end if   
       if (e.getSource()==buttonsc)//乘
       {
                   op++;                        //把op狀態累加為1
                    calculator(tmp1);            //呼叫calculator()把目前的資料內容加起來
                    optr = 3;                    //設定運算狀態為3做乘法
                    op2=0;
       }//end if            
       if (e.getSource()==buttonsb)//減
       {
                   op++;                        //把op狀態累加為1
                    calculator(tmp1);            //呼叫calculator()把目前的資料內容加起來
                    optr = 2;                    //設定運算狀態為2做減法
                    op2=0;
       }//end if      
       if (e.getSource()==buttonsa)//加
       {
                   op++;                        //把op狀態累加為1
            calculator(tmp1);            //呼叫calculator()把目前的資料內容加起來
            optr = 1;                    //設定運算狀態為1做加法
            op2=0;
       }//end if
       if (e.getSource()==buttonse)//==
       {
                   if (op == 1)calculator(tmp2);//如果按下四則運算就呼叫calculator()把目前資料累加至被加數
                    if (op == 0)                 //如果沒有按四則運算就按等於
                    {
                              op = 1;                                         //運算狀態設定為做加法
                              calculator(tmp1);                 //呼叫calculator()把目前資料累加至加數
                    }
                    op2=1;//begin();                     //運算過後呼叫begin()重新設定狀態
       }//end if
       if (e.getSource()==buttonsdel)//del
       {
                   int i = tmp1.length();       //先取得目前資料的長度
                    tmp1 = tmp1.substring(0,i-1);//把目前長度減1的數字字串取消掉
                    label1.setText(tmp1);        //輸出運算過後的資料字串
       }//end if
       if (e.getSource()==buttonssin)//負號
       {
                if (sign == 0)                                 //如果未輸入負號
                tmp1 = "-" + tmp1;                         //把負號累加到目前的資料
                int i = tmp1.length();                 //取得目前資料內容
                if (sign == 1)                                 //如果已經輸入過負號
                tmp1 = tmp1.substring(1, i); //取出目前長度的資料起點為1也就是沒有負號
                label1.setText(tmp1);        //輸出運算過後的資料
                sign = 1 - sign;             //更改負號state的狀態
       }//end if   
        }//end actionPerformed
}//end ButtonHandler
   public void calculator(String tmp1)
   {
            if(op == 1){
              num1 = Double.valueOf(tmp1).doubleValue();//(加減乘除)數
              num2 = Double.valueOf(tmp2).doubleValue();//被(加減乘除)數
              pt = 0;                                                                        //重新設定pt state為0
              sign = 0;                                 //設定負數state為0
              if (optr == 1){                                                        
                num2 = num2 + num1;                     //累加過後的數字輸出至被加數
                tmp2 = Double.toString(num2);                        //把運算過後的資料轉態成字串
              }
              if (optr == 2){
                num2 = num2 - num1;                     //累減過後的數字輸出至被減數
                tmp2 = Double.toString(num2);           //把運算過後的資料轉態成字串
              }
              if (optr == 3){
                num2 = num2 * num1;                    //累加過後的數字輸出至被加數
                tmp2 = Double.toString(num2);          //把運算過後的資料轉態成字串
              }
              if (optr == 4){
                num2 = num2 / num1;                    //累加過後的數字輸出至被加數
                tmp2 = Double.toString(num2);          //把運算過後的資料轉態成字串
              }
            }
            label1.setText(tmp2);                                           //輸出運算過後的資料
            tmp1 = "";                                 //把下一次的加數設定為空字串
            op = 1;                                                                           //運算狀態設為1做加法
            flag = 1;                                                                   //設定flag state為1以便下次輸入數字可以先清空在輸入數字
  }//end calculator
  public void begin()
  {
    tmp1 = new String("");                         //把下一次的加數設定為空字串
    tmp2 = new String("0");                                                   //把下一次的被加數設定為0
    pt = 0;      //初始小數點狀態為0
    optr = 1;    //設定運算狀態為加法
    sign = 0;    //設定負數state為0
    flag = 0;    //把flag state設定為0
  }//end begin
}//end test class

TOP

引用:
請問一下,第二個程式為何跑出來的視窗,在按鍵上是沒有數字的,請問要怎麼修改才會有數字出現?
自己放圖進去讀取
複製內容到剪貼板
代碼:
請問一下第一個程式是不是有問題阿?我compile了之後好笑有一些錯誤,可以修改一下嗎
要修改功能請自行修改

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。