发生特定选择时显示组合框

时间:2023-01-27 20:24:32

I am coding a bookshop in Java and have a problem with when a new book is ordered I want the user to select whether it is a ebook or paper book. If it is an ebook I want another combo box to show on the page with called cboFormat. I have some code but it doesn't seem to work.

我正在用Java编写书店,并且在订购新书时遇到问题我希望用户选择它是电子书还是纸质书。如果是电子书,我想在页面上显示另一个名为cboFormat的组合框。我有一些代码,但似乎没有用。

This is in the constructor.

这是在构造函数中。

if("Ebook".equals(cboBookType.getSelectedItem()))
    {
        cboFormat.enable();
    }
     else 
     {
     cboFormat.disable();
     }

Why doesn't this work? I have also previously set the format input to disabled.

为什么这不起作用?我之前也将格式输入设置为禁用。

2 个解决方案

#1


1  

This could be that you do not have a actionlistener on your combo box ? As Andrew suggested, there could be more reasons why your block does not work. If you pasted more code it would be easier to determine what the problem is. If however you are missing action listener on your combo box, code below.

这可能是你的组合框上没有actionlistener?正如安德鲁建议的那样,可能有更多原因导致您的块不起作用。如果您粘贴了更多代码,则更容易确定问题所在。但是,如果您在组合框中缺少动作侦听器,请在下面编写代码。

  public void actionPerformed(ActionEvent e) {
            JComboBox cboBookType = (JComboBox)e.getSource();
            String bookType= (String)cboBookType.getSelectedItem();
            //and paste your ifs here
            if("Ebook".equals.....){
              ...
            }
        ... rest of code
    }

And if you don't know what action listener is, its basically interface used by other classes to listen for an action event. i.e. user clicking button, or user selecting checkbox etc.

如果你不知道什么是动作监听器,它的基本接口被其他类用来监听动作事件。即用户点击按钮,或用户选择复选框等。

#2


0  

Dont use enable and disable try this and dont put it in the constructor because then it wont get updated you have to make a new event like itemchanged or itemstatechanged i dont know it exactly

不要使用启用和禁用尝试这个并且不要把它放在构造函数中,因为它不会得到更新你必须创建像itemchanged或itemstatechanged这样的新事件我不知道它到底

   if("Ebook".equals(cboBookType.getSelectedItem()))
    {
        cboFormat.setvisible(true);
    }
     else 
     {
     cboFormat.setvisible(false);
     }

#1


1  

This could be that you do not have a actionlistener on your combo box ? As Andrew suggested, there could be more reasons why your block does not work. If you pasted more code it would be easier to determine what the problem is. If however you are missing action listener on your combo box, code below.

这可能是你的组合框上没有actionlistener?正如安德鲁建议的那样,可能有更多原因导致您的块不起作用。如果您粘贴了更多代码,则更容易确定问题所在。但是,如果您在组合框中缺少动作侦听器,请在下面编写代码。

  public void actionPerformed(ActionEvent e) {
            JComboBox cboBookType = (JComboBox)e.getSource();
            String bookType= (String)cboBookType.getSelectedItem();
            //and paste your ifs here
            if("Ebook".equals.....){
              ...
            }
        ... rest of code
    }

And if you don't know what action listener is, its basically interface used by other classes to listen for an action event. i.e. user clicking button, or user selecting checkbox etc.

如果你不知道什么是动作监听器,它的基本接口被其他类用来监听动作事件。即用户点击按钮,或用户选择复选框等。

#2


0  

Dont use enable and disable try this and dont put it in the constructor because then it wont get updated you have to make a new event like itemchanged or itemstatechanged i dont know it exactly

不要使用启用和禁用尝试这个并且不要把它放在构造函数中,因为它不会得到更新你必须创建像itemchanged或itemstatechanged这样的新事件我不知道它到底

   if("Ebook".equals(cboBookType.getSelectedItem()))
    {
        cboFormat.setvisible(true);
    }
     else 
     {
     cboFormat.setvisible(false);
     }