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());
   }
}

getSelection()次に、送信ボタンのアクションリスナーで、選択が行われたこと、および選択内容を確認するためにこのオブジェクトのメソッドを呼び出す必要があることをすべての関係者に通知します。

編集
ああ、あなたが何をしようとしているのかわかりました - あなたは質問ダイアログを作成しようとしています。私の提案は、実際にこれを行い、JFrameではなくダイアログを作成し、実際にモーダルダイアログにして、メソッドがユーザーの応答を待ってから次に進むようにすることです。メソッドが終了すると、実際、ButtonGroup は正しい情報を保持します。これを行う最も簡単な方法は、非常に柔軟なクリッターである JOptionPane を使用し、JRadioButton のグリッドを保持する JPanel を保持するものを使用することです。次に、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 に答える