import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Demo02 extends JFrame
implements ActionListener{
private JButton btn1, btn2;
private JPanel northPanel;
public Demo02(){
btn1 = new JButton("Button 1");
btn2 = new JButton("Button 2");
northPanel = new JPanel();
northPanel.add(btn1);
northPanel.add(btn2);
getContentPane().add(northPanel,
BorderLayout.NORTH);
// Step 4:
btn1.addActionListener(this);
btn2.addActionListener(this);
}//end constructor
// Step 3: add this method
public void actionPerformed(ActionEvent ev){
if (ev.getSource() == btn1)
JOptionPane.showMessageDialog(null,
"Hi,you clicked button 1");
else if (ev.getSource() == btn2){
JOptionPane.showMessageDialog(null,
"Hi,you clicked button 2");
}
}//end actionPerformed
}//end class
Friday, November 24, 2006
Demo02 : Event Handling
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment