丁保国的博客

收集整理工作生活,点点滴滴

  :: :: 博问 :: 闪存 :: :: :: :: 管理 ::

应用软件打包出品

――以Java计算器小软件为例

程序设计(源程序清单)

package cal;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

class MyJButtons implementsActionListener {

    String[] keys={"7","8","9","/","4","5","6",

       "*","1","2","3","-","0",".","=","+","Clear"};

    JButton[] keyb = new JButton[keys.length];

    JTextField t;

    double num1=0.0,num2;

    intflag=0;

    intn=1;

    intmath=0;   

    MyJButtons(JTextField t){

       this.t=t;

       for( int i = 0; i <keys.length; i++ ) {

           keyb[i]=new JButton(keys[i]);

           keyb[i].addActionListener(this);

       }     

    }

    public void actionPerformed(ActionEvent e){

       Object JButton=e.getSource();

       String s1=JButton+"";

       int l=s1.indexOf("text=");

       String s2=s1.substring(l+5);

       char c=s2.charAt(0);

       String s;

       if(c=='C'){

           t.setText("0");

           num1=0;

           flag=0;

       }

       if((int)(c-'0')>=0&&(int)c<=(int)'9'){

           if (flag==0){

              t.setText(c+"");

              flag=1;

           }

           else{

              t.setText(t.getText()+c);

           }

       }

       else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='='){

           num2=Double.parseDouble(t.getText());

           flag=0;

           switch(math){

              case 0:{

                  num1=num2;

                  t.setText(num1+"");

                  break;

              }

              case 1:{

                  num1=num1+num2;

                  t.setText(num1+"");

                  break;

              }

              case 2:{

                  num1=num1-num2;

                  t.setText(num1+"");

                  break;

              }

              case 3:{

                  num1=num1*num2;

                  t.setText(num1+"");

                  break;

              }

              case 4:{

                  if(num2==0){

                     t.setText("Error!");

                     num1=0.0;

                  }else{

                     num1=num1/num2;

                     t.setText(num1+"");

                  }

                  break;

              }

           }

           if(c=='+') math=1;

           else if(c=='-')math=2;

           else if(c=='*')math=3;

           else if(c=='/')math=4;

           else math=0;

       }

       else if(c=='.'){

           s=t.getText();

           if(s.indexOf(".")==-1){

              t.setText(s+c);

           }

           else if(flag==0){

              t.setText(".");

              flag=1;

           }

       }

    }

}

 

class MyText extends JTextField{

    MyText(int w){

       super(w);

       this.setText("0.0");

       this.setEditable( false );

       this.setHorizontalAlignment(this.RIGHT);

    }

}

 

public class Calculator extends JFrame {

    MyText t= new MyText(13);

    MyJButtons keyJButton=new MyJButtons(t);

    public Calculator() {

       super("计算器");

        setDefaultCloseOperation(EXIT_ON_CLOSE );

       Container cc = getContentPane();

       cc.setLayout( new BorderLayout() );

       JPanel p = new JPanel();

       p.setLayout( new GridLayout(4,4,0,2));

       for( int i = 0;i <keyJButton.keyb.length-1; i++ ) {

           p.add(keyJButton.keyb[i]);

       }

       cc.add(    t, "North" );

       cc.add(    p, "Center" );

       cc.add(    keyJButton.keyb[keyJButton.keyb.length-1], "South" );

       pack();

       this.setLocation(350,300);

       this.setResizable(false);

       this.setVisible(true);

    }

 

 

    public static void main( String[] args ) {

       new Calculator();

    }

}

 

编译生成相关的class文件

编译、纠错,调试成功生成三个class文件

cal

 +-----Calculator.class

 +-----MyJButtons.class

 +-----Mytext.class

 

创建标明文件设置主类

创建标明文件(manifest.mf)

Main-Class: cal.Calculator(回车)

 

说明:可执行主类全名(包含包名),前面必须有一个空格。

此时的目录结构

javatry

  |

  +-----cal

         +-----Calculator.class

         +-----MyButtons.class

         +-----Mytext.class

  +-----manifest.mf

 

打包成可执行的jar文件包

打包命令:jar cvfm cal.jarmanifest.mfcal/.

此时的目录结构

javatry

  |

  +-----cal

         +-----Calculator.class

         +-----MyButtons.class

         +-----Mytext.class

  +-----manifest.mf

  +-----cal.jar

 

打包成功后,双击cal.jar即可执行。

posted on 2007-07-21 14:29  丁保国  阅读(252)  评论(2编辑  收藏  举报