私は、Java Webサービスを使用して簡単な追加をしようとしています。 JTextField
からの入力を私のint
に入力するのに問題がありますので、プログラムは追加を実行できます。私が持っているコードは以下の通りです。ユーザーが最初の値を入力すると、最初のボタン(butta
)が2番目のボタンと同じになるはずです。2番目のボタンをクリックすると結果が表示されます。整数の解析は、ボタンをクリックして値を取っていないときにはうまくいかないようです。整数解析のintが動作しない
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);
}
}
}
これは[タグ:宿題]ですか?そうでなければ、私は単一のフィールドと[ScriptEngine](http://stackoverflow.com/a/7441804/418556)を使用することをお勧めします。 –
入力を読み取っている場所にコードを貼り付けることができます(つまり、add()関数) – Ankit
Javaの基本機能が「動作していません」と主張する質問が大好きです。 – Paul