java NetBeans 中的文本字段禁用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/828706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:57:00  来源:igfitidea点击:

Text Field disabling in NetBeans

javaswingnetbeanstextfield

提问by 3yoon af

I want to ask if there is a way to make the text field active and inactive according to the radio button.

我想问一下是否有办法根据单选按钮使文本字段处于活动状态和非活动状态。

For example, the textfield will be inactive and when the user click on the radio button, the textfield will be active.

例如,文本字段将处于非活动状态,而当用户单击单选按钮时,文本字段将处于活动状态。

I am using Java language and NetBeans program

我正在使用 Java 语言和 NetBeans 程序

回答by willcodejavaforfood

You could have two radio buttons for representing the active/inactive state. Add an action listener to each and when the 'active' one is pressed you call setEditable(true) on the JTextField and when the 'inactive' JRadioButton is called you call setEditable(false).

您可以有两个单选按钮来表示活动/非活动状态。为每个添加一个动作侦听器,当按下“活动”时,您在 JTextField 上调用 setEditable(true),当调用“非活动”JRadioButton 时,您调用 setEditable(false)。

JTextField textField = new JTextField();
JRadioButton activeButton = new JRadioButton("Active");
JRadioButton inactiveButton = new JRadioButton("Inactive");
activeButton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        textField.setEditable(true);
    }
});
inactiveButton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        textField.setEditable(false);
    }
});