我无法从我创建的单选按钮中返回一个简单的 int 值。
static int f5(String question)
{
int intValue = 0;
answered = false;
JFrame f = new JFrame(question);
f.setSize(300, 750);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
JPanel p = new JPanel(new GridLayout(0,1,3,3));
final ButtonGroup bg = new ButtonGroup();
JRadioButton radioButton;
p.add(radioButton = new JRadioButton("q1"));
radioButton.setActionCommand("1");
bg.add(radioButton);
p.add(radioButton = new JRadioButton("q2"));
radioButton.setActionCommand("2");
bg.add(radioButton);
p.add(radioButton = new JRadioButton("q3"));
radioButton.setActionCommand("3");
bg.add(radioButton);
p.add(radioButton = new JRadioButton("q4"));
radioButton.setActionCommand("4");
bg.add(radioButton);
JButton orderButton = new JButton("Submit");
p.add(orderButton);
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(p);
f.pack();
orderButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String ans = bg.getSelection().getActionCommand();
boolean sel;
f5Answer = Integer.parseInt(ans);
answered = true;
System.out.println("in void: " + f5Answer); //how to return the f5Answer?
}
});
f.setVisible(true);
f5Answer = intValue;
System.out.println("out of void: " + f5Answer);//this only returns 0 right away
return intValue;
}
当我按下提交时,我想在找到值后返回值和结束函数。现在,在我使用 main 中的函数后,该值立即返回 0。如何使函数仅在按下提交后返回值?
提前致谢!