Share
Explore BrainMass

Java ButtonViewer and ClickListener classes

Enhance the ButtonViewer program so that it prints the time at which the button was clicked.
Please provide the ClickListener class in your solution.

Use the following class as your main class:

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
This program demonstrates how to install an action listener.
*/
public class ButtonViewer
{
private static final int FRAME_WIDTH = 100;
private static final int FRAME_HEIGHT = 60;

public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);

ActionListener listener = new ClickListener();
button.addActionListener(listener);

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Solution Preview

Please rename the attached 470532-ButtonViewer.java as ButtonViewer.java before compiling and executing the program.

As nothing is specified about whether the clicked-on time should be printed via a dialog box or on the console, this solution prints the clicked-on time at console.

Sample output on console would look like following.

$ java ButtonViewer
Clicked on Fri Apr 06 16:51:45 IST 2012
Clicked on ...

Solution Summary

As nothing is specified about whether the clicked-on time should be printed via a dialog box or on the console, this solution prints the clicked-on time at console.

$2.19