Hello,
Here is the code which can explain you properly
Code:
Button class Hello extends JFrame implements ActionListener (
JButton b1;
JButton b2;
Hello public Button () (
this.setDefaultCloseOperation (DISPOSE_ON_CLOSE);
mkBut ();
arele ();
Buttons register ();
)
private void mkBut () (
b1 = new JButton ("button 1");
b2 = new JButton ("button 2");
)
private void arele () (
this.setLayout (new Flow layout ());
this.getContentPane (). add (b1);
this.getContentPane (). add (b2);
)
Buttons private void register () (
b1.addActionListener (this);
b2.addActionListener (this);
)
public static void main (String [] args) (
Hello h = new Button app Button ();
app.setSize (100,100);
app.setVisible (true);
)
public void actionPerformed (ActionEvent exc) (
if (exc.getSource () == b1)
System.out.println ("Button1 pressured");
if (exc.getSource () == b2)
System.out.println ("Button2 pressured");
System.out.println ("An event occurred");
)
)
Bookmarks