java jradiobutton或jcheckbox for text

时间:2017-03-31 05:48:18

标签: java string swing jcheckbox jradiobutton

我正在尝试创建一个基于所选单选按钮显示一行文本的桌面应用程序,但到目前为止我所能得到的只是基于最新选择的JRadioButton的单个部分。

理想情况下,我希望结束输出基于选择的按钮,而不是基于所选最后一个按钮的单个输出。

更改为JCheckBox然后根据所选行生成文本会更好吗?

编辑:现在如果选择了hvba和hfba,文本只会显示最后一个按钮而不是两者都有,所以我想知道如何让它们显示出来。添加了一个编辑。

public class Gui extends JFrame{
private JTextArea ta;
private JRadioButton hvba, hvbr, hfba, hfbr, cab;
private ButtonGroup group1, group2;
private String hva, hvr, hfa, hfr, ca;

public Gui(){
    super("QA Narrative tool");
    setLayout(new FlowLayout());

    ta = new JTextArea("QA Narrative", 5, 25);
    add(ta);

    hvba = new JRadioButton("Add HV", false);
    hvbr = new JRadioButton("Remove HV", false);
    hfba = new JRadioButton("Add HF", false);
    hfbr = new JRadioButton("Remove HF", false);
    cab = new JRadioButton("Clear all", true);
    add(hvba);
    add(hvbr);
    add(hfba);
    add(hfbr);
    add(cab);

    group1 = new ButtonGroup();
    group1.add(hvba);
    group1.add(hvbr);
    group1.add(cab);

    group2 = new ButtonGroup();
    group2.add(hfba);
    group2.add(hfbr);
    group2.add(cab);

    hva = new String("add HV reason code");
    hvr = new String("remove HV reason code");
    hfa = new String("add HF reason code");
    hfr = new String("remove HF reason code");
    ca = new String("");

    hvba.addItemListener(new HandlerClass(hva));
    hvbr.addItemListener(new HandlerClass(hvr));
    hfba.addItemListener(new HandlerClass(hfa));
    hfbr.addItemListener(new HandlerClass(hfr));
    cab.addItemListener(new HandlerClass(ca));
}

private class HandlerClass implements ItemListener{
    private String string;

    public HandlerClass(String s){
        string = s;
    }

    public void itemStateChanged(ItemEvent event){
        ta.setText("Please " + string + " .");
    }
}

}

0 个答案:

没有答案