Here is what I have for my Button Viewer. I don't know if I am doing this right. Would I have to do a new class actionlistener for each button? Also, how would I do the counter? It would be something like system.out.println("I was clicked" + n + "times!"); ??
Here is my code:
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
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 = 100;
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button1 = new JButton("Click me!");
panel.add(button1);
JButton button2 = new JButton("Click me!");
panel.add(button2);
class AddActionListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
frame.add(panel);
ActionListener listener = new ClickListener();
button1.addActionListener(listener);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

New Topic/Question
Reply



MultiQuote



|