I am working with GUIs on my java homework and I have to specify what other is within a JCheckBox. I have everything else finished except for this minor requirement. I'm not quite sure how to go about this, I've looked in my book and tried researching it online


Requirement:

A series of check boxes where the supervisor can choose which sports events their residents are most interested in (basketball, hockey, swimming, football, soccer, tennis, wrestling, other). Next to the check box that says "other" should be a text box so the supervisor can specify what "other" means.


If you need any clarification let me know I'm new to posting here.

有帮助吗?

解决方案 2

Your code will look like :

JCheckBox chk_other = new JCheckBox("Other");
JTextField txt_other = new JTextField();
txt_other.setVisible(false);

    chk_other.addActionListener( new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                txt_other.setVisible(true);
            }
        });

其他提示

Just have a text field that becomes visible (or keep it visible the whole time) when other is selected. On the back end if other is selected read the value of the parameter sent from that text box.

Figured it out :D

JTextField jtfOther = new JTextField(10);

JCheckBox jcbOther= new JCheckBox("Other");

adds the textbox and check box

if(jcbOther.isSelected() ) message += "\nOther: " + jtfOther.getText() + " is selected";

displays it in the actionPerformed window

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top