Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从按钮组保存选定的JRadioButton?_Java_Swing_Compiler Errors_Jradiobutton_Buttongroup - Fatal编程技术网

Java 如何从按钮组保存选定的JRadioButton?

Java 如何从按钮组保存选定的JRadioButton?,java,swing,compiler-errors,jradiobutton,buttongroup,Java,Swing,Compiler Errors,Jradiobutton,Buttongroup,我的程序试图保存按钮组的状态,以便在程序结束时将其写入文件,或者在用户选择“返回”时恢复到按钮 我以前发现过这个问题: 但是,我下面的代码在编译时出错,因为在ActionPerformed方法中使用ButtonGroup时无法识别 import javax.swing.*; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionE

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

我以前发现过这个问题:

但是,我下面的代码在编译时出错,因为在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;
                }
            }
        }
    }
import javax.swing.*;
导入java.awt.FlowLayout;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入java.util.Enumeration;
公共类GraphicalInterface实现ActionListener
{
私有静态字符串[]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”};
私有int[]答案={4,4,4,4,4,4,4};
私有int currentTweet=0;
JFrame surveyFrame=新JFrame(“用户调查”);
JPanel surveyPanel=新的JPanel(新的FlowLayout());
JButton buttonBack=新JButton(“Back”);
JButton buttonNext=新JButton(“下一步”);
JButton buttonFinish=新JButton(“Finish”);
JRadioButton cred1=新JRadioButton(“非常可信”);
JRadioButton cred2=新的JRadioButton(“可信”);
JRadioButton cred3=新的JRadioButton(“有点可信”);
JRadioButton cred4=新的JRadioButton(“未决定”);
JRadioButton cred5=新的JRadioButton(“有点不可思议”);
JRadioButton cred6=新JRadioButton(“难以置信”);
JRadioButton cred7=新的JRadioButton(“极其不可思议”);
JLabel tweetLabel=新的JLabel(新的图像图标(tweetList[currentTweet]);
公共void接口()
{
surveyFrame.setVisible(真);
测量框架设置尺寸(500350);
surveyFrame.add(surveyPanel);
surveyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
surveyPanel.add(推特标签);
surveyPanel.add(按钮下一步);
surveyPanel.add(按钮背面);
surveyPanel.add(按钮完成);
调查面板添加(cred1);
调查面板添加(cred2);
调查面板添加(cred3);
调查面板添加(cred4);
调查面板添加(第5条);
调查面板添加(cred6);
调查面板添加(第7条);
cred4.选择的设置(正确);
ButtonGroup=新建ButtonGroup();
tweet.add(cred1);
tweet.add(cred2);
tweet.add(cred3);
tweet.add(cred4);
添加(cred5);
tweet.add(cred6);
tweet.add(cred7);
buttonNext.addActionListener(此);
addActionListener(这个);
buttonFinish.addActionListener(这个);
}
已执行的公共无效操作(操作事件输入)
{
Object source=input.getSource();
如果(源==按钮下一步)
{
如果(currentTweet0)
{
answers[currentTweet]=getCredRating(TweetCredition);
当前推特--;
ImageIcon nextTweet=新的ImageIcon(tweetList[currentTweet]);
tweetlab.setIcon(nextTweet);
//从上一个图像还原按钮
}
}
如果(源==按钮完成)
{
//将答案保存到文件
}
}
public int getCredRating(按钮组输入)
{
int n=1;
对于(枚举按钮=input.getElements();buttons.hasMoreElements();n++)
{
AbstractButton=buttons.nextElement();
if(button.isSelected())
{
返回n;
}
}
}
}
编译器:找不到变量

知道为什么会这样吗

谢谢

ButtonGroup tweetCredibility = new ButtonGroup();
您正在将ButtonGroup定义为局部变量。这是只有GraphicalInterface()构造函数知道的

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


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

tweet
被定义为一个局部变量,在
接口()内
方法,您试图在外部以另一种方法访问该变量,在这种情况下,是
actionPerformed(…)
方法导致了这个问题。