ButtonExample.java Uses the following

/./././././././././
# ButtonExample.java Uses the following classes:

    * CloseableFrame.java
    * FgReporter.java
    * BgReporter.java
    * SizeReporter.java
******************
ButtonExample.java 
******************
import java.awt.*;
import java.awt.event.*;

/././././././././././

public class ButtonExample extends CloseableFrame {
  public static void main(String[] args) {
    new ButtonExample();
  }

  public ButtonExample() {
    super("Using ActionListeners");
    setLayout(new FlowLayout());
    Button b1 = new Button("Button 1");
    Button b2 = new Button("Button 2");
    Button b3 = new Button("Button 3");
    b1.setBackground(Color.lightGray);
    b2.setBackground(Color.gray);
    b3.setBackground(Color.darkGray);
    FgReporter fgReporter = new FgReporter();
    BgReporter bgReporter = new BgReporter();
    SizeReporter sizeReporter = new SizeReporter();
    b1.addActionListener(fgReporter);
    b2.addActionListener(fgReporter);
    b2.addActionListener(bgReporter);
    b3.addActionListener(fgReporter);
    b3.addActionListener(bgReporter);
    b3.addActionListener(sizeReporter);
    add(b1);
    add(b2);
    add(b3);
    setSize(350, 100);
    setVisible(true);
  }
}
/./././././././././
FgReporter.java
***************
import java.awt.event.*;
import java.awt.*;

/././././././././././

public class FgReporter implements ActionListener {
  public void actionPerformed(ActionEvent event) {
    Component c = (Component)event.getSource();
    System.out.println("Foreground: " + c.getForeground());
  }
}
****************
BgReporter.java
****************
/././././././././
import java.awt.event.*;
import java.awt.*;

/././././././././././././

public class BgReporter implements ActionListener {
  public void actionPerformed(ActionEvent event) {
    Component c = (Component)event.getSource();
    System.out.println("Background: " + c.getBackground());
  }
}
/././././././././././
SizeReporter.java
***********************

import java.awt.event.*;
import java.awt.*;

/././././././././

public class SizeReporter implements ActionListener {
  public void actionPerformed(ActionEvent event) {
    Component c = (Component)event.getSource();
    Dimension d = c.getSize();
    System.out.println("Size: " + d.width + "x" + d.height);
  }
}
********************

Permanent link to this article: http://bangla.sitestree.com/buttonexample-java-uses-the-following/

Leave a Reply