无法启用灰色(.setEnabled(false))jtextfield或jtextarea

发布于 2025-01-29 10:36:11 字数 1396 浏览 2 评论 0原文

不幸的是,我无法打开JTEXTFIELD或JTEXTFIELD(两者都尝试)的.setenable()。它一直保持灰色,因此用户无法输入。请帮助我。

详细信息:Tatwo可以是JTEXTFIELD或JTEXTAREA,但我尝试的任何尝试都无法启用。应该禁用A,但应启用B的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
            }
        }
    });
}

Unfortunately, I cannot turn on .setEnable() for a JTextField, or a JTextField (tried both). It keeps remaining gray, so users cannot type. Please help me out.

Details: taTwo can be either a JTextField or JTextArea but any I try cannot be enabled. It should be disabled for A but should be enabled for B, so if user choose A he/she can NOT enter a value in taTwo field, but if the user choose B he/she can write in taTwo.

The method is the following:

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
            }
        }
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2025-02-05 10:36:11

在您的ActionPerformed方法中,您正在创建 new JTEXTFIELD您未添加到GUI中。我认为您有另一个名为tatwo的变量,您未发布的代码中的某些变量。您是 ActionPerformed方法中更改该变量。尝试删除您的代码的这一行:

JTextField taTwo = new JTextField();

In your actionPerformed method, you are creating a new JTextField which you are not adding to your GUI. I presume that you have another variable named taTwo somehere in the code that you did not post. You are not changing that variable inside the actionPerformed method. Try removing this line of your code:

JTextField taTwo = new JTextField();
国粹 2025-02-05 10:36:11

如果我正确理解您的问题,则需要将enableMet/禁用代码移动到另一个actionListener中,然后将其添加到ComboBox中。这样的事情:

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);
        }
    });
}

If I correctly understand your question, you need to move your enablemet/disablement code into another ActionListener and add it to your combobox. Something like this:

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);
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文