/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package buttonabinner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonABinner extends JApplet
{
public void init()
{
Container contentPane = getContentPane ();
contentPane.setLayout(new FlowLayout());
JButton btnA = new JButton("A");
JButton btnB = new JButton("B");
// Instead of passing this to the button's listener
// registration method, pass a new instance of the
//appropriate listener class
//btnA.addActionListener(this);
//btnB.addActionListener(this);
// ==>
btnA.addActionListener(new btnListenerA());
btnB.addActionListener(new btnListenerB());
contentPane.add(btnA);
contentPane.add(btnB);
}
class btnListenerA implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String title = "Message";
String message = "Hello from the Swing button A.";
JOptionPane.showMessageDialog(null, message, title,
JOptionPane.INFORMATION_MESSAGE);
}
}
class btnListenerB implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String title = "Message";
String message = "Hello from the Swing button B.";
JOptionPane.showMessageDialog(null, message, title,
JOptionPane.INFORMATION_MESSAGE);
}
}
}
Tidak ada komentar:
Posting Komentar