请完成下列Java程序:创建一个具有2行3列的GridLayout管理器,包括Choice,Label,Button构件,布局为第1行包括一个Choice构件(包括2个选项item1和item2),一个Label构件(当选择Choice构件中的选项时,La

admin2009-12-22  20

问题 请完成下列Java程序:创建一个具有2行3列的GridLayout管理器,包括Choice,Label,Button构件,布局为第1行包括一个Choice构件(包括2个选项item1和item2),一个Label构件(当选择Choice构件中的选项时,Label构件显示相应的名称,即如果单击item1则Label中显示item1),和一个exit按钮(单击则退出应用程序),第2行包括3个Button构件。程序运行结果如下:
   
   注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
   import java.awt.*;
   import java.awt.event.*;
   public class ex2_2 extends Frame implements ActionListener,ItemListener
   {
      private Label 1;
      private String str="Label";
      private Choice choice2_2;
      public static void main(String[] args)
      {
         new ex2_2();
      }
      ex2_2
      {
         setLayout(______);
         choice2_2=new Choice();
         choice2_2.addItem("item1");
         choice2_2.addItem("item2");
         choice2_2.______;
         add(choice2_2);
         1=new Label(str);
         add(1);
         Button exit2_2=new Button("exit");
         Exit2_2.addActionListener(this);
         add(exit2_2);
         for(int i=0;i<3;i++)
            add(new Button("button" +i));
         setSize(300,300);
         pack();
         show();
      }
      public void actionPerformed(ActionEvent event)
      {
         If (event.getActionCommand().equals("exit"))
          {
           System.exit(0);
         }
      }
      public void itemStateChanged(ItemEvent event)
      {
         Str=choice2_2.getSelectedItem();
         1.setText(str);
      }
   }

选项

答案new GridLayout(2,3) addltemlistener(this)

解析 本题主要考查Java常用构件Choice和高级事件ItemEvent以及GridLayout布局管理器的用法。解题关键是,熟悉GridLayout布局管理器和ItemEvent的用法。ItemEvent在教材中并没有给出确切的用法,但是可以根据ActionEvent的addActionEvent()方法和重载actionPerformed()方法来完成对动作事件监听的用法来类推ItemEvent事件的监听方法,这里要求有根据已有知识进行单一反三的能力。
转载请注明原文地址:https://kaotiyun.com/show/ucnp777K
0

最新回复(0)