位置:首頁 > Java技術 > AWT > AWT CheckBoxGroup類

AWT CheckBoxGroup類

介紹

checkboxgroup類用於分組組複選框。

類的聲明

以下是聲明為java.awt.CheckboxGroup類:

public class CheckboxGroup
   extends Object
      implements Serializable

類的構造函數

S.N. 構造函數與說明
1 CheckboxGroup() () 
Creates a new instance of CheckboxGroup.

類方法

S.N. 方法和說明
1 Checkbox getCurrent() 
Deprecated. As of JDK version 1.1, replaced by getSelectedCheckbox().
2 Checkbox getSelectedCheckbox() 
Gets the current choice from this check box group.
3 void setCurrent(Checkbox box)
Deprecated. As of JDK version 1.1, replaced by setSelectedCheckbox(Checkbox).
4 void setSelectedCheckbox(Checkbox box) 
Sets the currently selected check box in this group to be the specified check box.
5 String toString() 
Returns a string representation of this check box group, including the value of its current selection.

繼承的方法

這個類繼承的方法從以下類:

  • java.lang.Object

checkboxgroup的實例

選擇使用編輯器創建以下java程序D:/ > AWT > com > gitbook.net > gui >

AwtControlDemo.java
package com.yiibai.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showCheckBoxGroupDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showCheckBoxGroupDemo(){
    
      headerLabel.setText("Control in action: CheckBoxGroup"); 

      CheckboxGroup fruitGroup = new CheckboxGroup();

      Checkbox chkApple = new Checkbox("Apple",fruitGroup,true);
      Checkbox chkMango = new Checkbox("Mango",fruitGroup,false);
      Checkbox chkPeer = new Checkbox("Peer",fruitGroup,false);

      statusLabel.setText("Apple Checkbox: checked");
      chkApple.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {             
            statusLabel.setText("Apple Checkbox: checked");
         }
      });

      chkMango.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Mango Checkbox: checked");
         }
      });

      chkPeer.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Peer Checkbox: checked");
         }
      });

      controlPanel.add(chkApple);
      controlPanel.add(chkMango);   
      controlPanel.add(chkPeer);       

      mainFrame.setVisible(true);  
   }
}

編譯程序,使用命令提示符。到D:/ > AWT 然後鍵入以下命令。

D:AWT>javac comyiibaiguiAwtControlDemo.java

如果冇有錯誤出現,這意味著編譯成功。使用下麵的命令來運行程序。

D:AWT>java com.yiibai.gui.AwtControlDemo

驗證下麵的輸出

AWT CheckBoxGroup