⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 radiobuttonapplet.java

📁 Java the UML Way 书中所有源码
💻 JAVA
字号:
/*
 * RadioButtonApplet.java  E.L. 2001-08-22
 *
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class RadioButtonApplet extends JApplet {
  private JRadioButton female = new JRadioButton("Female", true);
  private JRadioButton male = new JRadioButton("Male", false);

  public void init() {
    Container guiContainer = getContentPane();

    /* The container is constructed in the same way as in the CheckBoxApplet */
    guiContainer.add(new JPanel(), BorderLayout.NORTH); // a little space
    guiContainer.add(new JPanel(), BorderLayout.SOUTH); // a little space
    SelectionPanel middle = new SelectionPanel();
    guiContainer.add(middle, BorderLayout.CENTER);
  }

  /* Describes the panel with the radio buttons */
  private class SelectionPanel extends JPanel {
    public SelectionPanel() {
      ButtonGroup group = new ButtonGroup();
      group.add(female);
      group.add(male);
      add(female);
      add(male);
      RadioButtonListener listener = new RadioButtonListener();
      female.addActionListener(listener);
      male.addActionListener(listener);

      /* We make a group box by surrounding the boxes with a border, or more correctly:
         make a border around the panel which contains the check boxes */
      SoftBevelBorder border = new SoftBevelBorder(BevelBorder.RAISED);
      Border groupBox = BorderFactory.createTitledBorder(border, "Sex");
      setBorder(groupBox);
    }
  }

  /* Describes listeners, which fires every change in the radio button group */
  private class RadioButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
      String sex = event.getActionCommand();
      if (sex.equals("Female")) System.out.println("Female is selected");
      else System.out.println("Male is selected");
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -