2

我无法从我创建的单选按钮中返回一个简单的 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。如何使函数仅在按下提交后返回值?

提前致谢!

4

1 回答 1

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,该 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);
   }
于 2012-03-21T14:33:04.397 回答