如何从 ButtonGroup 中保存选定的 JRadioButton?

How to save the selected JRadioButton from a ButtonGroup?

我的程序正在尝试保存 ButtonGroup 的状态,以便在程序结束时将其写入文件,或者在用户选择离开时恢复到按钮 'Back'。

我以前发现过这个问题:

How do I get which JRadioButton is selected from a ButtonGroup

但是我的下面的代码在编译时出错,因为在 ActionPerformed 方法中使用时无法识别 ButtonGroup。

    import javax.swing.*;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Enumeration;

    public class GraphicalInterface implements ActionListener
    {
        private static String[] tweetList =
        {"/Users/Chris/Desktop/Code/Tweets/One.jpg", "/Users/Chris/Desktop/Code/Tweets/Two.jpg", "/Users/Chris/Desktop/Code/Tweets/Three.jpg",
        "/Users/Chris/Desktop/Code/Tweets/Four.jpg", "/Users/Chris/Desktop/Code/Tweets/Five.jpg", "/Users/Chris/Desktop/Code/Tweets/Six.jpg",
        "/Users/Chris/Desktop/Code/Tweets/Seven.jpg", "/Users/Chris/Desktop/Code/Tweets/Eight.jpg"};

        private int[] answers = {4, 4, 4, 4, 4, 4, 4, 4};
        private int currentTweet = 0;

        JFrame surveyFrame = new JFrame("User Survey");
        JPanel surveyPanel = new JPanel(new FlowLayout());

        JButton buttonBack = new JButton("Back");
        JButton buttonNext = new JButton("Next");
        JButton buttonFinish = new JButton("Finish");

        JRadioButton cred1 = new JRadioButton("Extrememly Credible");
        JRadioButton cred2 = new JRadioButton("Credible");
        JRadioButton cred3 = new JRadioButton("Somewhat Credible");
        JRadioButton cred4 = new JRadioButton("Undecided");
        JRadioButton cred5 = new JRadioButton("Somewhat Incredible");
        JRadioButton cred6 = new JRadioButton("Incredible");
        JRadioButton cred7 = new JRadioButton("Extrememly Incredible");

        JLabel tweetLabel = new JLabel(new ImageIcon(tweetList[currentTweet]));

        public void Interface()
        {
            surveyFrame.setVisible(true);
            surveyFrame.setSize(500, 350);
            surveyFrame.add(surveyPanel);
            surveyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            surveyPanel.add(tweetLabel);
            surveyPanel.add(buttonNext);
            surveyPanel.add(buttonBack);
            surveyPanel.add(buttonFinish);
            surveyPanel.add(cred1);
            surveyPanel.add(cred2);
            surveyPanel.add(cred3);
            surveyPanel.add(cred4);
            surveyPanel.add(cred5);
            surveyPanel.add(cred6);
            surveyPanel.add(cred7);
            cred4.setSelected(true);

            ButtonGroup tweetCredibility = new ButtonGroup();
            tweetCredibility.add(cred1);
            tweetCredibility.add(cred2);
            tweetCredibility.add(cred3);
            tweetCredibility.add(cred4);
            tweetCredibility.add(cred5);
            tweetCredibility.add(cred6);
            tweetCredibility.add(cred7);

            buttonNext.addActionListener(this);
            buttonBack.addActionListener(this);
            buttonFinish.addActionListener(this);
        }

        public void actionPerformed(ActionEvent input)
        {
            Object source = input.getSource();

            if (source == buttonNext)
            {
                if (currentTweet < tweetList.length-1)
                {
                    answers[currentTweet] = getCredRating(tweetCredibility);
                    currentTweet++;
                    ImageIcon nextTweet = new ImageIcon(tweetList[currentTweet]);
                    tweetLabel.setIcon(nextTweet);
                    // restore button from next image
                }
            }

            if (source == buttonBack)
            {
                if (currentTweet > 0)
                {
                    answers[currentTweet] = getCredRating(tweetCredibility);
                    currentTweet--;
                    ImageIcon nextTweet = new ImageIcon(tweetList[currentTweet]);
                    tweetLabel.setIcon(nextTweet);
                    // restore button from previous image
                }
            }

            if (source == buttonFinish)
            {
                // save answers to file
            }
        }

        public int getCredRating(ButtonGroup input)
        {
            int n = 1;
            for (Enumeration<AbstractButton> buttons = input.getElements(); buttons.hasMoreElements(); n++)
            {
                AbstractButton button = buttons.nextElement();
                if (button.isSelected())
                {
                    return n;
                }
            }
        }
    }

编译器:找不到变量 tweetCredibility

知道为什么会这样吗?

谢谢。

ButtonGroup tweetCredibility = new ButtonGroup();

您正在将 ButtonGroup 定义为局部变量。只有 GraphicalInterface() 构造函数知道它。

您需要将按钮组定义为实例变量,以便class的任何方法都可以使用它。因此,在定义 JRadioButtons 的位置定义 ButtonGroup。

此外,surveyFrame.setVisibe(true) 语句应在所有组件都添加到框架后作为构造函数的最后一条语句调用。