根据netbeans中的单选按钮切换组件的“enable”属性 [英] Toggle a component's 'enable' property according to a radio button in netbeans

查看:204
本文介绍了根据netbeans中的单选按钮切换组件的“enable”属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个按钮组中有两个单选按钮,在同一个面板中我有一个文本框和一个按钮。我想仅在选择第二个按钮时启用文本框和按钮,并在选择另一个单选按钮时禁用。我试过这个并没有用。

I have two radio buttons in a button group and in the same panel I have a text box and a button. I want to enable the text box and the button only when the second button is selected and be disabled when the other radio button is selected. I've tried this and it didn't work.

private void radio_button2ActionPerformed(java.awt.event.ActionEvent evt) {
if(buttonGroup1.getSelection()==radio_button2)
{
    button.setEnabled(true);
    textbox.setEnabled(true);
}

我哪里出错?

推荐答案

您不想使用ActionListener,因为只有在您单击按钮时才会触发事件。相反,您可以使用ItemListener,以便在选择或取消选择项目时生成事件(通过单击其他单选按钮)。类似于:

You don't want to use an ActionListener because the event only fires when you click the button. Instead you can use an ItemListener so an event is generated when the item is selected or deselected (by clicking the other radio button). Something like:

radioButton2.addItemListener( new ItemListener()
{
    public void itemStateChanged(ItemEvent e)
    {
        JRadioButton button = (JRadioButton)e.getSource();
        component1.setEnabled( button.isSelected() );
        component2.setEnabled( button.isSelected() );
    }
});

这篇关于根据netbeans中的单选按钮切换组件的“enable”属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆