Java如何创建JRadioButton并定义一些动作?
在下面,我们创建一个JRadioButton并将动作句柄传递给该组件,以便我们知道何时单击该组件。
package org.nhooo.example.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class RadioButtonCreateAction extends JFrame {
public RadioButtonCreateAction() {
initializeUI();
}
private void initializeUI() {
setSize(300, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
Action action = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
JRadioButton button = (JRadioButton) e.getSource();
System.out.println("actionPerformed " + button.getText());
}
};
//创建一个单选按钮,并将AbstractAction作为其构造函数传递。
//每次单击单选按钮或
//按下。但是,当它以编程方式选择时,动作将不会
//叫。
JRadioButton button = new JRadioButton(action);
button.setText("Click Me!");
button.setSelected(true);
getContentPane().add(button);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new RadioButtonCreateAction().setVisible(true);
}
});
}
}
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短