5 Replies - 805 Views - Last Post: 08 March 2013 - 11:14 PM Rate Topic: -----

#1 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Why can't I count the times it was clicked?

Posted 04 March 2013 - 04:15 AM

Here is the problem.
a. Write an application that instantiates a JFrame that contains a JButton. Disable the JButton after the user clicks it.
b. Modify the JFrameDisableButton program so that the JButton is not disabled until the user has clicked atleast 8 times. At that point, display a JLabel that indicates "That's enough!".

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JFrameDisableButton extends JFrame implements ActionListener
{
  JButton button = new JButton("Enabled");
  JLabel label = new JLabel("");
  public JFrameDisableButton()
  {
    super("Enabled-Disabled");
    setLayout(new FlowLayout());
    add(button);
    add(label);
    button.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e)
  {
  Object source = e.getSource();
  final int LIMIT = 8;
  int i;
  int times = 0;
  for(i = 0;i<8;i++)
  {
    if(source == button)
  {
    ++times;
    if(times == 8)
   {
    button.setEnabled(false);
   }
  }
  }
  
  }
  
  
  public static void main(String[] args)
  {
    JFrameDisableButton y = new JFrameDisableButton();
    final int FRAME_WIDTH = 300;
    final int FRAME_HEIGHT = 150; 
    y.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    y.setVisible(true);
    y.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}




I just can't answer the problem and if you don't mind, can you tell me if I answered letter a correctly when it comes to instantiation of JFrame? Thank you. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Why can't I count the times it was clicked?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Why can't I count the times it was clicked?

Posted 04 March 2013 - 04:26 AM

The variables important to accomplish what you're trying to do are local to the actionPerformed() method so are recreated and initialized each time the button is pressed. Declare and initialize the counter outside the actionPerformed() method.

This post has been edited by GregBrannon: 04 March 2013 - 04:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 codecsmash49  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

Re: Why can't I count the times it was clicked?

Posted 04 March 2013 - 09:22 AM

What Greg said is correct, to allow variables to be left unchanged after each new click you have to remove them from the method. The location that you initializing your variables is the issue.
Was This Post Helpful? 0
  • +
  • -

#4 ShroomiaCo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 13-January 13

Re: Why can't I count the times it was clicked?

Posted 04 March 2013 - 08:24 PM

Another issue aside from the instantation, you need to remove the for loop. What would happen is it will always set it to eight, even if it I pressed only once.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Why can't I count the times it was clicked?

Posted 05 March 2013 - 02:38 PM

View PostShroomiaCo, on 04 March 2013 - 10:24 PM, said:

Another issue aside from the instantation, you need to remove the for loop. What would happen is it will always set it to eight, even if it I pressed only once.

I think that codecsmash49 had already answered your concern

View Postcodecsmash49, on 04 March 2013 - 11:22 AM, said:

The location that you initializing your variables is the issue.

This post has been edited by pbl: 05 March 2013 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#6 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: Why can't I count the times it was clicked?

Posted 08 March 2013 - 11:14 PM

Thanks to all of you! I appreciate your effort. Sorry for the late reply, i was busy the previous days! Keep up the good work. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1