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

标签 java swing compiler-errors jradiobutton buttongroup

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

我之前发现过这个问题:

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() 构造函数知道这一点。

您需要将按钮组定义为实例变量,以便该类的任何方法都可以使用它。因此,请在定义 JRadioButton 的地方定义 ButtonGroup。

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

关于java - 如何从 ButtonGroup 中保存选定的 JRadioButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29543711/

相关文章:

java - 如何拥有Jlabel的唯一标识符?

java - 在java中从线程执行方法

Java Eclipse - 将字段命名为表列名以外的其他名称

java - null、0 和 nothing 之间有什么区别?

java - 提交分数排行榜 Google Play 时应用程序崩溃

java - 如何根据内容找出未显示的 JPanel 的首选大小?

java - 在 Eclipse 中使用 Swing 实用程序时获取 "Access restriction error"

c++ - 解决链接器错误MySQL Connector/C++

python - 如何创建包含所有相关文本的单个列?

java 解析异常 : Unparseable date