/**
* A Simple stand-alone GUI program based on Swing.
*
* This example uses a class that extends JFrame
* instead of creating a JFrame object!
* The main creates a window with a bunch of buttons
* and a textfield.
* Uses a JPanel to hold the buttons (container component).
*
* Whenever the user does something with the mouse
* (related to a button), the textfield changes to
* display what is happening.
*
* This sample is based (in part) on code supplied in
* an example found at java.sun.com (how to write a mouse listener
* tutorial).
* @author Dave Hollinger
* @version 1.0
*/
import javax.swing.*;
import java.awt.*; // needed for Container
import java.awt.event.*; // needed for event types (listeners)
class MouseEvents extends JFrame {
// The textfield must not be anonymouse, since we want to
// be able to modify it from event handlers
JTextArea jt;
// main just creates a new MouseEvent Object
// (which is a special kind of JFrame).
public static void main(String [] args) {
MouseEvents me = new MouseEvents();
}
// Constructor sets up the window and gets things rolling.
MouseEvents() {
Container c = getContentPane();
// set up layout manager
c.setLayout(new BorderLayout(10,10));
// add a button panel
c.add( createButtonPanel(), BorderLayout.NORTH );
// add single text Area
jt = new JTextArea(5,20);
jt.setSize(100,20);
jt.setFont(new Font("Arial",Font.BOLD,16));
jt.setBackground(Color.WHITE);
jt.setForeground(Color.BLUE);
c.add(jt,BorderLayout.CENTER);
// establish what happens when the window is closed
// (without this the program would keep running!)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set the size
setSize(200,200);
// turn it on
setVisible(true);
}
JPanel createButtonPanel() {
// create a JPanel with a bunch of buttons in it arranged in
// a grid.
JPanel jp = new JPanel();
// establish the layout manager
// 2 rows, 3 cols everything 5 pixels apart
jp.setLayout( new GridLayout(2,3,5,5));
// Create a mouse event listener object (code is below)
// that will handle events
MouseHandler mh = new MouseHandler();
// add a bunch of buttons
for (int i=0;i<6;i++) {
JButton j = new JButton(new Integer(i).toString());
// change the font
j.setFont(new Font("Courier",Font.BOLD,20));
// change the colors
j.setBackground(Color.WHITE);
j.setForeground(Color.BLACK);
// set up the listener for this button
j.addMouseListener(mh);
// add the button to the panel
jp.add(j);
}
return(jp);
}
// here is a class that will implement the MouseListener
// interface. This allows us to capture mouse related events
// and update the textfield.
class MouseHandler implements MouseListener {
// each of these methods updates the text area field
// to describe what has happend and what button it
// happened to.
public void mousePressed(MouseEvent e) {
updateText(e,"Pressed");
}
public void mouseReleased(MouseEvent e) {
updateText(e,"Released");
}
public void mouseEntered(MouseEvent e) {
updateText(e,"Entered");
}
public void mouseExited(MouseEvent e) {
updateText(e,"Exited");
}
public void mouseClicked(MouseEvent e) {
updateText(e,"Clicked");
}
public void updateText(MouseEvent e, String what) {
// get the button associated with the event
JButton j = (JButton) e.getComponent();
// get the button text
String butname = j.getText();
// update the text area
jt.setText("Button " + butname + " " + what);
}
}
}