📄 checkboxapplet.java
字号:
/*
* 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -