我正在尝试使用Java Web服务进行简单的添加。我在将输入从JTextField输入到我的int时遇到问题,以便程序可以执行添加操作。我的代码如下。当用户输入第一个值时,他们应单击与第二个相同的第一个按钮(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);
          }
        }
    }

最佳答案

您正在bb中设置aaActionListener。您再也不必检查(“读取”)这些值。那么您怎么知道他们得到了错误的价值呢?您没有任何实际使用它们的代码。

07-27 13:13