this is my code which is about postfix expression evaluator, and it works great, but it just work when I click evaluate button.
I want the code works also when I click 'Enter' key
public void init(){
setLayout(new BorderLayout());
label1 = new JLabel("Enter a postfix arithmetic expression, then press Enter or click Evaluate. Expression");
add("North",label1);
Panel p = new Panel();
p.setLayout(new FlowLayout());
input = new JTextField(50);
input.setSize(300, 20);
p.add(input);
label2 = new JLabel("Result : ");
p.add(label2);
label3 = new JLabel("");
label3.setSize(30,20);
p.add(label3);
add("Center", p);
Panel p2 = new Panel();
p2.setLayout(new FlowLayout());
b1 = new JButton("Evaluate");
p2.add(b1);
b2 = new JButton("Clear");
p2.add(b2);
add("South", p2);
b1.addActionListener(this);
b2.addActionListener(this);
setSize(800,150);
}
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand()=="Evaluate"){
String post = input.getText();
String ans = compute(post);
label3.setText(ans);
System.out.println(label3.getText());
System.out.println(ans);
}
else if(ae.getActionCommand()=="Clear"){
input.setText("");
label3.setText("");
}
}
}
Thanks