参考文档:
java documentation
JLablel
使用JLAbel类可以显示无法被选择的文本or image。
如果希望组件是交互式且需要具有某种状态,建议使用button
ImageIcon icon = createImageIcon("images/middle.gif");
. . .
label1 = new JLabel("Image and Text",
icon,
JLabel.CENTER);
//Set the position of the text, relative to the icon:
label1.setVerticalTextPosition(JLabel.BOTTOM);
label1.setHorizontalTextPosition(JLabel.CENTER);
label2 = new JLabel("Text-Only Label");
label3 = new JLabel(icon);
Buttons 、Check Boxes 、 Radio Buttons
类 | 说明 |
---|---|
JButton | 一般按钮 |
JCheckBox | 复选框 |
JRadioButton | 单选框 |
JMenuItem | 菜单中的类 |
JRadioButtonMenuItem | 有JRadioButton 的菜单类 |
JCheckBoxMenuItem | 有JCheckBox 的菜单类 |
JToggleButton | JCheckBox, JRadioButton的父类 ,可以实例化或子类化以创建两个状态按钮。 |
Buttons触发的写法
ActionListener 接口
public interface ActionListener extends EventListener {
/**
* Invoked when an action occurs.
* @param e the event to be processed
*/
public void actionPerformed(ActionEvent e);
}
Buttons触发的写法①
{//初始化代码
button1 = new JButton();
button2 = new JButton();
button1.setActionCommand("命令1");
button1.setMnemonic(KeyEvent.VK_D);//触发按钮
button2.setActionCommand("命令2");
button2.setMnemonic(KeyEvent.VK_M);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if ("命令1".equals(e.getActionCommand())) {
// do something
} else if("命令2".equals(e.getActionCommand()){
//do something
}else{//do something
}
}
特点:
- 可以使用同一个实现了ActionListener 接口的类
- 在类中通过不同的ActionCommand区别触发命令
Buttons触发的写法②
{//初始化代码
button1 = new JButton();
button1.addActionListener(e -> button1Action(e));
}
private void button1Action(ActionEvent e) {
// do something
}
特点:
- 触发方法写在类的内部
- 无需在类中通过不同的ActionCommand区别触发命令
方法②实现条理更清晰
Check Boxes 复选框
{
chinButton = new JCheckBox("Chin");
chinButton.setMnemonic(KeyEvent.VK_C);
chinButton.setSelected(true);
glassesButton = new JCheckBox("Glasses");
glassesButton.setMnemonic(KeyEvent.VK_G);
glassesButton.setSelected(true);
hairButton = new JCheckBox("Hair");
hairButton.setMnemonic(KeyEvent.VK_H);
hairButton.setSelected(true);
chinButton.addItemListener(this);
glassesButton.addItemListener(this);
hairButton.addItemListener(this);
...
}
public void itemStateChanged(ItemEvent e) {
//do something
}
...
Radio Buttons 单选框
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand(birdString);
birdButton.setSelected(true);
JRadioButton catButton = new JRadioButton(catString);
catButton.setMnemonic(KeyEvent.VK_C);
catButton.setActionCommand(catString);
JRadioButton dogButton = new JRadioButton(dogString);
dogButton.setMnemonic(KeyEvent.VK_D);
dogButton.setActionCommand(dogString);
JRadioButton rabbitButton = new JRadioButton(rabbitString);
rabbitButton.setMnemonic(KeyEvent.VK_R);
rabbitButton.setActionCommand(rabbitString);
JRadioButton pigButton = new JRadioButton(pigString);
pigButton.setMnemonic(KeyEvent.VK_P);
pigButton.setActionCommand(pigString);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
group.add(dogButton);
group.add(rabbitButton);
group.add(pigButton);
//Register a listener for the radio buttons.
birdButton.addActionListener(this);
catButton.addActionListener(this);
dogButton.addActionListener(this);
rabbitButton.addActionListener(this);
pigButton.addActionListener(this);
...
public void actionPerformed(ActionEvent e) {
//do something
}