gpt4 book ai didi

java - 单选按钮上的 Action 监听器

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:14:26 26 4
gpt4 key购买 nike

我想根据单选按钮的选择设置文本框的可编辑选项?如何编写单选按钮上的 Action 监听器?

最佳答案

这是我在这种情况下会使用的解决方案。

    //The text field
JTextField textField = new JTextField();

//The buttons
JRadioButton rdbtnAllowEdit = new JRadioButton();
JRadioButton rdbtnDisallowEdit = new JRadioButton();

//The Group, make sure only one button is selected at a time in the group
ButtonGroup editableGroup = new ButtonGroup();
editableGroup.add(rdbtnAllowEdit);
editableGroup.add(rdbtnDisallowEdit);

//add allow listener
rdbtnAllowEdit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setEditable(true);

}
});

//add disallow listener
rdbtnDisallowEdit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setEditable(false);

}
});

关于java - 单选按钮上的 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11316778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com