无法启用灰显 (.setEnabled(false)) JtextField 或 JTextArea

Cannot enable grayed-out (.setEnabled(false)) JtextField or JTextArea

提问人:Thend 提问时间:5/16/2022 更新时间:5/16/2022 访问量:80

问:

不幸的是,我无法为 JTextField 或 JTextField 打开 .setEnable()(两者都尝试过)。它保持灰色,因此用户无法键入。请帮帮我。

详细信息:taTwo 可以是 JTextField 或 JTextArea,但我尝试的任何都无法启用。它应该对 A 禁用,但应该对 B 启用,因此如果用户选择 A,他/她不能在 taTwo 字段中输入值,但如果用户选择 B,他/她可以写入 taTwo。

方法如下:

public void btnAddtreeAction() {
    this.btnAddtree.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            
            JPanel dialogPanel = new JPanel();
            dialogPanel.setPreferredSize(new Dimension(60,60));
            
            String[] choices = {"A", "B"};
            JComboBox<String> cb = new JComboBox<String>(choices);
            
            JTextArea taOne = new JTextArea(1,30);
            JTextField taTwo = new JTextField();
            taTwo.setEnabled(false);

            Object[] myObject = {"Options:", cb,
                                "Input first:", taOne,
                                "Input second:", taTwo};
            
            JOptionPane.showConfirmDialog(frame, myObject, "Form", JOptionPane.OK_CANCEL_OPTION);
            
            if (cb.getSelectedItem().toString().equals("A")) {
               //something will happen here
            } else if (cb.getSelectedItem().toString().equals("B")) {
                taTwo.setEnabled(true);
              //something will happen here
            }
        }
    });
}
Java Swing 用户界面

评论

1赞 Reimeus 5/16/2022
你为什么要创建一个新的,每次都被触发?发布一个最小的可重现示例JTextFieldJTextAreaaddActionListener

答:

0赞 Abra 5/16/2022 #1

在您的方法中,您正在创建一个新的,您不会将其添加到您的 GUI 中。我假设您在代码中还有另一个名为 somehere 的变量,但您没有发布。您不会在方法中更改该变量。尝试删除以下代码行:actionPerformedJTextFieldtaTwoactionPerformed

JTextField taTwo = new JTextField();

评论

0赞 Thend 5/19/2022
最初,概念如下:当用户点击按钮 (btnAddtree) 时,会出现一个弹出输入窗口,如果用户从组合框中选择 A 只能填充 taOne,但如果用户选择 B,则可以同时填充 taOne 和 taTwo。最后,我删除了 taTwo。谢谢你的帮助。:)
0赞 Sergiy Medvynskyy 5/16/2022 #2

如果我正确理解了您的问题,您需要将启用/禁用代码移动到另一个代码中并将其添加到您的组合框中。像这样的东西:ActionListener

public void btnAddtreeAction() {
    this.btnAddtree.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            
            JPanel dialogPanel = new JPanel();
            dialogPanel.setPreferredSize(new Dimension(60,60));
            
            String[] choices = {"A", "B"};
            JComboBox<String> cb = new JComboBox<String>(choices);
            
            JTextArea taOne = new JTextArea(1,30);
            JTextField taTwo = new JTextField();
            taTwo.setEnabled(false);
            cb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (cb.getSelectedItem().toString().equals("A")) {
                        taTwo.setEnabled(false);
                        //something will happen here
                    } else if (cb.getSelectedItem().toString().equals("B")) {
                        taTwo.setEnabled(true);
                        //something will happen here
                    }
                }
            });

            Object[] myObject = {"Options:", cb,
                                "Input first:", taOne,
                                "Input second:", taTwo};
            
            JOptionPane.showConfirmDialog(frame, myObject, "Form", JOptionPane.OK_CANCEL_OPTION);
        }
    });
}