java swing相关笔记

参考文档:
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 的菜单类
JToggleButtonJCheckBox, 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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值