开发测试学习
目录

为了看一个更复杂的独立的图形界面应用程序,下面有一个数字转换的例子:

importjava.awt.*;
  publicclassd2xextendsFrame{
    intdecimalValue=0;
    StringbaseXValue=newString("0");
    TextFielddDisplay,xDisplay;
    d2xconstructorpublicd2x(){
      super("DecimalConverter");
      setthetitleoftheframeMenuBarmb=new MenuBar();
      Buttond2Binary=new Button(Binary);
      Buttond2Octal=new Button(Octal);
      Buttond2Hex=new Button(Hex);
      Buttond2Base36=new Button(Base36);
      Panelp1=new Panel();
      Panelp2=new Panel();
      Panelp3=new Panel();
      addasimplemenuMenum=new Menu(Application);
      m.add(newCheckboxMenuItem(Base36Active);
      m.add(newMenuItem(Exit));
      addmenutomenubarmb.add(m);
      setMenuBar(mb);
      installthismenubar
      intheframe
      Addbuttonstotheirownpanelp3.setLayout(newFlowLayout());
      p3.add(d2Binary);
      p3.add(d2Octal);
      p3.add(d2Hex);
      p3.add(d2Base36);
      AddtextfieldsLabeldLabel=new Label(EnterDeecimal:);
      LabelxLabel=new Label(ConvertedValue:);
      dDisplay=new TextField(integer.toString(decimalValue),7);
      xDisplay=new TextField(baseXValue,32);
      xDisplay.setEditable(false);
      p1.setLayout(new FlowLayout(FlowLayout.LEFT));
      p2.setLayout(new FlowLayout(FlowLayout.LEFT));
      p1.add(dLabel);
      p1.add(dDisplay);
      p2.add(xLabel);
      p2.add(xDisplay);
      Addthepanelsadd(North,p1);
      add(Center,p2);
      add(South,p3);
  }
  endd2xconstructor
  publicvoidstart(){
    resize(400,150);
    show();
  }
  publicvoidupdateXDisplay(){
    xDisplay.setText(baseXValue);
  }
  publicbooleanhandleEvent(Eventevt){
    if(evt.targtintanceofMenuItem){
      if(Exit.equals(((MenuItem)evt.target).getLabel())){
        hide();
        dispose();
        System.exit(0);
        returnfalse;
        }
        retruntrue;
      }elseif(evt.targetinstanceofButton){
        Stringwhick=((Button)evt.target).getLabel();
        if(whick.equals(Binary)){
        decimalValue=Integer.parseInt(dDisplay.getText());
        baseXValue=Interger.toString(decimalValue,2);
      }if(whick.equals(Octal)){
        decimalValue=Integer.parseInt(dDisplay.getText());
        baseXValue=Interger.toString(decimalValue,8);
      }if(whick.equals(Hex)){
        decimalValue=Integer.parseInt(dDisplay.getText());
        baseXValue=Interger.toString(decimalValue,16);
      }if(whick.equals(36)){decimalValue=Integer.parseInt(dDisplay.getText());
      baseXValue=Interger.toString(decimalValue,36);
      }
      updateXDisplay();
      returntrue;
    }
      returnfalse;
  }
  publicstaticvoidmain(Stringargs[]){
    d2xm=newd2x();
    m.start();
    }
  }