我的程序试图保存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;
}
}
}
}
编译器:找不到变量tweet
任何想法为什么会这样?
谢谢。
最佳答案
ButtonGroup tweetCredibility = new ButtonGroup();
您正在将ButtonGroup定义为局部变量。那只是GraphicalInterface()构造函数知道这一点。
您需要将按钮组定义为实例变量,以便该类的任何方法都可以使用它。因此,在定义JRadioButton的位置定义ButtonGroup。
同样,在所有组件都添加到框架之后,应调用
surveyFrame.setVisibe(true)
语句作为构造函数的最后一条语句。