Results 1 to 3 of 3

Thread: Number of Button CLicks{resolved}

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class ButtonClick extends JFrame   implements ActionListener {
    int numClicks = 0;
    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: ";
    numClicks++;
    lblNumClicks.setLabelFor(btnClick);
    lblNumClicks.setText("Number of Clicks: " + numClicks);
    }
    
    public static void main(String[] arguments) {
    ButtonClick btn = new ButtonClick();
    }
    }

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    You have to move the integer variable numClicks to the class itself to make it a member variable. The way you have it now(as a local method variable) when an event is fired off numClicks is initialized to 0 then incremented by 1 with the same process repeated with subsequent method invocations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width