|
-
Jun 5th, 2004, 09:17 AM
#1
Thread Starter
Frenzied Member
Number of Button CLicks{resolved}
I made a program that tells how many times a buttons has been clicked in a label. It will work for the first click but after that it doesnt. I think I know whats wrong but I dont know how to fix it.
Here is the code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ButtonClick extends JFrame implements ActionListener {
JButton btnClick = new JButton("Press");
JLabel lblNumClicks = new JLabel("0");
public ButtonClick() {
super("Number of Clicks");
setSize(150,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnClick.addActionListener(this);
JPanel row1Button = new JPanel();
row1Button.add(btnClick);
Container pane = getContentPane();
pane.add(row1Button);
JPanel row2Label = new JPanel();
row2Label.add(lblNumClicks);
pane.add(row2Label);
FlowLayout flo = new FlowLayout();
pane.setLayout(flo);
setContentPane(pane);
setVisible(true);
lblNumClicks.setLabelFor(btnClick);
}
public void actionPerformed(ActionEvent event) {
String labelPrefix = "Number of Button Clicks: ";
int numClicks = 0;
numClicks++;
lblNumClicks.setLabelFor(btnClick);
lblNumClicks.setText("Number of Clicks: " + numClicks);
}
public static void main(String[] arguments) {
ButtonClick btn = new ButtonClick();
}
}
Last edited by System_Error; Jun 10th, 2004 at 10:06 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|