checkboxapplet.java

来自「Java the UML Way 书中所有源码」· Java 代码 · 共 52 行

JAVA
52
字号
/*
 * CheckboxApplet.java  E.L. 2000-01-04
 *
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CheckboxApplet extends JApplet {
  private JCheckBox dinner = new JCheckBox("Dinner");
  private JCheckBox lunch = new JCheckBox("Lunch");

  public void init() {
    Container guiContainer = getContentPane();
    /*
     * To make the group box around the checkboxes visible,
     * we have to put one empty panel in north and one in south.
     */
    guiContainer.add(new JPanel(), BorderLayout.NORTH);
    guiContainer.add(new JPanel(), BorderLayout.SOUTH);

    SelectionPanel middle = new SelectionPanel();
    guiContainer.add(middle, BorderLayout.CENTER);
  }

  /* Describes the panel with the checkboxes */
  private class SelectionPanel extends JPanel {
    public SelectionPanel() {
      add(dinner);
      add(lunch);
      CheckBoxListener listener = new CheckBoxListener();
      dinner.addActionListener(listener);
      lunch.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, "Meals");
      setBorder(groupBox);
    }
  }

  /* Describes listeners, which fires every change in a check box */
  private class CheckBoxListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
      System.out.println();
      if (dinner.isSelected()) System.out.println("Will have dinner.");
      if (lunch.isSelected()) System.out.println("Will have lunch.");
    }
  }
}

⌨️ 快捷键说明

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