2

Java Web サービスを使用して簡単に追加しようとしています。プログラムが加算を実行できるように、 からJTextField自分のに入力を取得する際に問題があります。int私が持っているコードは以下です。ユーザーが最初の値を入力するとき、2 番目のボタンと同じように最初のボタン ( butta) をクリックする必要があります。値を取得していないボタンをクリックすると、整数の解析が機能しないようです。

 public class Wscalcclient {
           static int aa=0, bb=0, cc=0;
        String str = new String(); 
        JTextField fielda, fieldb;
        JTextField fieldc;

        Wscalcclient()
        {  JButton butta, buttb;
           JLabel result;
           JPanel panel;

           JFrame frame  = new JFrame();
           frame.setBackground(Color.white);
           frame.setForeground(Color.black);
           frame.setSize(500,250);

           panel = new JPanel();
           panel.setBackground(Color.cyan);
           panel.setForeground(Color.black);
           panel.setLayout(new GridLayout(0,2));

           butta = new JButton("enter number a");
           buttb = new JButton("enter number b");
           result = new JLabel("result a+b=");
           fielda = new JTextField();
           fieldb = new JTextField();
           fieldc = new JTextField();

           panel.add(butta);
           panel.add(fielda);
           panel.add(buttb);
           panel.add(fieldb);
           panel.add(result);
           panel.add(fieldc);

           butta.addActionListener(new GetA());
           buttb.addActionListener(new GetB());

           frame.getContentPane().add(panel);
           frame.pack();
           frame.setVisible(true);

        }

        class GetA implements ActionListener
        { public void actionPerformed(ActionEvent t1e)
          { aa = Integer.parseInt(fielda.getText()); }
        }

        class GetB implements ActionListener
        { public void actionPerformed(ActionEvent t1e)
          { bb = Integer.parseInt(fieldb.getText()); }
        }

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            try
          { new Wscalcclient();
              calc.Calculator_Service service = new calc.Calculator_Service();
            calc.Calculator port = service.getCalculatorPort();


            BufferedReader keybrd = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("enter number a: ");
            int a = Integer.parseInt(keybrd.readLine());
            System.out.println("enter number b: ");
            int b= Integer.parseInt(keybrd.readLine());

            int cc = port.add(a,b);
            System.out.println("sum = " + cc);
          }
          catch (Exception ex)
          { System.out.println("exception = " + ex);
          }
        }
    }
4

1 に答える 1

5

あなたはあなたの中で設定bbaaていますActionListener。これらの値を再度調べる (「読み取る」) ことはありません。では、それらが間違った値を取得していることをどうやって知るのでしょうか? それらを実際に使用するコードはありません。

于 2011-12-23T04:40:55.047 に答える