문제

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