Java 整数解析int不工作
我正在尝试使用JavaWeb服务进行一个简单的添加。我无法将Java 整数解析int不工作,java,swing,jbutton,parseint,Java,Swing,Jbutton,Parseint,我正在尝试使用JavaWeb服务进行一个简单的添加。我无法将JTextField中的输入输入到我的ints中,以便程序可以执行加法。下面是我的代码。当用户输入第一个值时,他们应单击第一个按钮(butta),与第二个按钮相同,单击第二个按钮后,应在某处显示结果。当我点击按钮时,整数解析似乎不起作用,因为它没有取值 public class Wscalcclient { static int aa=0, bb=0, cc=0; String str = new
JTextField
中的输入输入到我的int
s中,以便程序可以执行加法。下面是我的代码。当用户输入第一个值时,他们应单击第一个按钮(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);
}
}
}
您正在
ActionListener
s中设置bb
和aa
。您再也不会检查(“读取”)这些值。那么你怎么知道他们得到了错误的值呢?您没有任何实际使用它们的代码。是这样吗?如果没有,我建议使用单个字段和.Hey你能把代码粘贴到你正在阅读输入的地方吗(即add()函数)我喜欢那些断言Java的一些基本函数“不起作用”的问题,只需发布问题的实际原因代码段。我需要使用什么代码才能将aa的值分配到a,将bb的值分配到b。我已经尝试了很多东西,但是我似乎没有把它做好。请帮忙,谢谢