JRadioButton在CardLayout面板中意外切换

时间:2017-01-02 17:30:53

标签: java swing cardlayout jradiobutton

所以问题是:我试图制作类似向导的CardLayout。在每个卡片面板中,我放回&下一个JButton和3个JRadioButton可在3页之间切换。

现在,当我第一次选择单选按钮时,它正常工作。但是,第二次选择单选按钮时,它们不会按预期选择。例如,我想选择第2页,卡面板2确实显示,但是单选按钮2状态不显示它被选中,而是选择了单选按钮1或3。仅在我再次单击时才会选中按钮2。当我尝试选择其他的时候会发生同样的事情。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutWizardDemo extends JFrame{
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CardLayoutWizardDemo frame= new CardLayoutWizardDemo();
                frame.init();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

    private static final long serialVersionUID = 1L;

    private JPanel cardPanel, panel1, panel2, panel3, btnPanel1, btnPanel2, btnPanel3;
    private JLabel label1, label2, label3;
    private JRadioButton step1, step2, step3;
    private ButtonGroup bg;
    private CardLayout cl = new CardLayout();

    private void init(){
        setTitle("CardLayoutWizardDemo");
        cardPanel = new JPanel();
        cardPanel.setLayout(cl);

        panel1 = new JPanel(new BorderLayout());
        panel2 = new JPanel(new BorderLayout());
        panel3 = new JPanel(new BorderLayout());

        label1 = new JLabel("label 1");
        label2 = new JLabel("label 2");
        label3 = new JLabel("label 3");

        panel1.add(label1, BorderLayout.NORTH);
        panel2.add(label2, BorderLayout.NORTH);
        panel3.add(label3, BorderLayout.NORTH);

        btnPanel1 = new JPanel();
        btnPanel2 = new JPanel();
        btnPanel3 = new JPanel();

        btnPanel1.setName("panel1");
        btnPanel2.setName("panel2");
        btnPanel3.setName("panel3");

        btnPanel1 = initTutBtn(btnPanel1);
        btnPanel2 = initTutBtn(btnPanel2);
        btnPanel3 = initTutBtn(btnPanel3);

        panel1.add(btnPanel1, BorderLayout.SOUTH);
        panel2.add(btnPanel2, BorderLayout.SOUTH);
        panel3.add(btnPanel3, BorderLayout.SOUTH);

        cardPanel.add(panel1, "1");
        cardPanel.add(panel2,"2");
        cardPanel.add(panel3,"3");

        getContentPane().add(cardPanel, BorderLayout.CENTER);

        setPreferredSize(new Dimension(350,500));
        setMinimumSize(new Dimension(240,320));
        pack();
        setLocationByPlatform(true);
    }


    /**create new set of 3 step buttons
     */
    private JPanel initTutBtn(JPanel btnPanel){

        btnPanel.setLayout(new BoxLayout(btnPanel,BoxLayout.X_AXIS));

        step1 = new JRadioButton();
        step2 = new JRadioButton();
        step3 = new JRadioButton();

        step1.setActionCommand("step1");
        step2.setActionCommand("step2");
        step3.setActionCommand("step3");

        bg = new ButtonGroup();
        bg.add(step1);
        bg.add(step2);
        bg.add(step3);

        if (btnPanel.getName().equals("panel1")){
            step1.setSelected(true);
        }else if (btnPanel.getName().equals("panel2")){
            step2.setSelected(true);
        }else if (btnPanel.getName().equals("panel3")){
            step3.setSelected(true);
        }

        step1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                goToStep(e);
            }
        });
        step2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                goToStep(e);
            }
        });
        step3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                goToStep(e);
            }
        });
        btnPanel.add(step1);
        btnPanel.add(step2);
        btnPanel.add(step3);

        return btnPanel;
    }

    private void goToStep(ActionEvent evt){
        if(evt.getActionCommand().equals("step1")){
            cl.show(cardPanel, "1");
        }else if(evt.getActionCommand().equals("step2")){
            cl.show(cardPanel, "2");
        }else if(evt.getActionCommand().equals("step3")){
            cl.show(cardPanel, "3");
        }
    }
}

我想也许问题在于我在initButton()goToStep(ActionEvent evt)内创建新的单选按钮,但我无法弄清楚我做错了什么

0 个答案:

没有答案