我有问题从我创建的radiobutton中返回一个简单的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。如何使函数仅在按下提交后返回值?
提前致谢!
答案 0 :(得分:5)
我想我有点理解你的问题。你应该做的是让你的ButtonGroup对象成为一个私有类字段,然后给你的类一个公共方法,比如说......
public int getSelection() {
ButtonModel btnModel = bg.getSelection();
if (bg == null) {
// TODO: throw an exception -- no radio button has been selected
} else {
return Integer.parseInt(btnModel.getActionCommand());
}
}
然后在提交按钮的actionlistener中,通知任何和所有方已做出选择,并且他们应该调用此对象上的getSelection()
方法以找出选择内容。
修改强>
啊,现在我看到你正在尝试做什么 - 你正试图创建一个问题对话框。我的建议是你实际上就是这样做,创建一个 对话框 而不是一个JFrame,实际上它是一个模态对话框,这样你的方法将等待用户在继续之前做出响应,因此当方法结束时,ButtonGroup实际上将保存正确的信息。最简单的方法是使用JOptionPane,它们是非常灵活的生物,并使用一个持有JPanel的工具,该JPanel包含JRadioButtons的网格。然后找出用户在 JOptionPane返回后选择的内容。在这里,静态方法可以正常工作,因为您没有更改对象的状态。例如:
public static String myQuestion(String question, String[] answers) {
JPanel panel = new JPanel(new GridLayout(0, 1));
final ButtonGroup btnGroup = new ButtonGroup();
for (String answer : answers) {
JRadioButton radioBtn = new JRadioButton(answer);
radioBtn.setActionCommand(answer);
btnGroup.add(radioBtn);
panel.add(radioBtn);
}
String[] options = { "Submit", "Cancel" };
String initialValue = "Submit";
int optionResult = JOptionPane.showOptionDialog(null, panel, question,
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, initialValue);
// *** code will pause here and wait for the user to handle the modal dialog
if (optionResult != 0) {
return ""; // did not select "Submit"
} else if (optionResult == 0) {
ButtonModel btnModel = btnGroup.getSelection();
if (btnModel == null) {
return ""; // error! nothing selected
} else {
return btnModel.getActionCommand();
}
}
return "";
}
public static void main(String[] args) {
String question = "Where is Ulysses S. Grant buried?";
String[] answers = { "In my back yard", "At the white house",
"red my lord! No, blue!", "In Grant's tomb", "Who the hell is Ulysses S. Grant?" };
String selectedString = myQuestion(question, answers);
System.out.println("Answer selected: " + selectedString);
}