Problems in dealing with event handlers
Hi everybody,
I was trying to program a simple calculator. I assigned the buttons the addActionListener method through
piu.addActionListener(this);
per.addActionListener(this);
div.addActionListener(this);
meno.addActionListener(this);
radice.addActionListener(this);
quadrato.addActionListener(this);
but it seems that when I press these buttons the program doesn't call tha actionPerformed event and so it doesn't execute the code in it. What is the mistake?
this is my code:
package array;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author Val
*/
public class ProvaGui extends JFrame implements ActionListener {
private JLabel result, lab1, lab2;
private static JFrame frame;
private JPanel pan, pan2;
private JButton per, div, piu, meno, radice, quadrato, uguale, clear;
private JTextField campo1, campo2, ris;
public ProvaGui(String par) throws MalformedURLException{
super(par);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cont = getContentPane();
pan = new JPanel();
this.pack();
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d= tk.getScreenSize();
setSize((int)d.getWidth()/2,(int)d.getHeight()/6);
setLocation((int)d.getWidth()/4,(int)d.getHeight()/4);
setResizable(false);
JButton clear = new JButton("C");
clear.addActionListener(this);
pan.add(clear);
cont.add(pan);
JButton per = new JButton("*");
JButton div = new JButton(":");
JButton piu = new JButton("+");
JButton meno = new JButton("-");
JButton radice = new JButton("x^(1/2)");
JButton quadrato = new JButton("x^2");
JButton uguale = new JButton("=");
pan.add(per);
pan.add(div);
pan.add(piu);
pan.add(meno);
pan.add(radice);
pan.add(quadrato);
pan.add(uguale);
JTextField campo1 = new JTextField("",8);
JTextField campo2 = new JTextField("",8);
JLabel lab1 = new JLabel("Operando 1");
JLabel lab2 = new JLabel("Operando 2");
JLabel result = new JLabel("Risultato: ");
JTextField ris = new JTextField("",8);
JPanel pan2 = new JPanel();
piu.addActionListener(this);
per.addActionListener(this);
div.addActionListener(this);
meno.addActionListener(this);
radice.addActionListener(this);
quadrato.addActionListener(this);
uguale.addActionListener(this);
pan2.add(lab1);
pan2.add(campo2);
pan2.add(lab2);
pan2.add(campo1);
cont.add(pan2,"South");
JPanel pan3 = new JPanel();
pan3.add(result);
pan3.add(ris);
cont.add(pan3,"East");
}
public void actionPerformed(ActionEvent evt) {
if(evt.getSource().equals(clear)){
campo1.setText("ggg");
campo2.setText("ggg");
System.out.println("AQUI!");
} else { if(evt.getSource().equals(uguale)){
ris.setText("");
}
}
}
public static void main (String[] args) throws MalformedURLException{
ProvaGui frame = new ProvaGui("Calcolatrice");
frame.setVisible(true);
}
}
Thank u for any help!