jjh08's Profile User Rating: -----

Reputation: 43 Craftsman
Group:
Active Members
Active Posts:
135 (0.43 per day)
Joined:
13-July 12
Profile Views:
1,776
Last Active:
User is offline May 22 2013 04:47 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Who Cares
Favorite Browser:
FireFox
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Playstation
Your Car:
Who Cares
Dream Kudos:
0

Latest Visitors

Icon   jjh08 says I see you jQuery :)

Posts I've Made

  1. In Topic: Creating a Counter With "+" "-" Buttons

    Posted 22 May 2013

    @Cevo
    This time, all your code is placed in main() but in the other case, the code logic was inside a constructor which would have created the infinite recursion GregBrannon was talking about. As I said before, from a practical standpoint, you don't want main() to have any code logic. You want the logic to be inside constructors and methods and have main() call those instead. Hope that helps :)

    As far as the static keyword is concerned, you should ask yourself "Does it make sense to use this field or invoke this method without instantiating an object first?" In your case, it is unnecessary to use static because you do not have to put the code logic in main() but you chose to do so.
    More resources : here and here
  2. In Topic: Creating a Counter With "+" "-" Buttons

    Posted 22 May 2013

    View PostCevo, on 22 May 2013 - 03:48 PM, said:

    i am not aware of all the methods you used there so i don't think i would be able to understand them fully at this moment.I used static because the variable was dropping to zero and starting another circle with the other button, so what i thought of making it static would bind the buttons to work together without the variable dropping.

    That sample code g00se gave will create the GUI on the event dispatch thread. From the Java Tutorials:

    Quote

    In Swing programs, the initial threads don't have a lot to do. Their most essential job is to create a Runnable object that initializes the GUI and schedule that object for execution on the event dispatch thread. Once the GUI is created, the program is primarily driven by GUI events, each of which causes the execution of a short task on the event dispatch thread. Application code can schedule additionals tasks on the event dispatch thread (if they complete quickly, so as not to interfere with event processing) or a worker thread (for long-running tasks).

    An initial thread schedules the GUI creation task by invoking javax.swing.SwingUtilities.invokeLater or javax.swing.SwingUtilities.invokeAndWait . Both of these methods take a single argument: the Runnable that defines the new task. Their only difference is indicated by their names: invokeLater simply schedules the task and returns; invokeAndWait waits for the task to finish before returning.

    Hope that helps :)

    View PostCevo, on 22 May 2013 - 03:59 PM, said:

    @GregBrannon @jjh08

    Click ClickCounter = new Click();
       stuff.add(lol);
       //+ button
       button.addActionListener(ClickCounter);
       stuff.add(button);
    //- button
       stuff.add(button2);
       button2.addActionListener(ClickCounter);
    

    So i think this will be right?

    No, definitely use this like Greg said.
  3. In Topic: Creating a Counter With "+" "-" Buttons

    Posted 22 May 2013

    View PostGregBrannon, on 22 May 2013 - 03:52 PM, said:

    @jjh08:

    Your code is a design improvement, but you have two BIG mistakes that will cause the system to crash. The two errors are calling the constructor from the constructor. Instead of

    // infinite recursion:
    button.addActionListener( new Click() );

    and

    // infinite recursion:
    button2.addActionListener( new Click() );

    You do this:

    button.addActionListener( this );
    button2.addActionListener( this );

    Thanks, I didn't even notice. My mistake :oops:
  4. In Topic: Creating a Counter With "+" "-" Buttons

    Posted 22 May 2013

    Quote

    If you declare them as class variables:

    Great catch, Greg. I didn't see that at first but I did something like this:
    public class Click implements ActionListener{
    int sayac=0;
    static JLabel lol=new JLabel("0 kere tıklandı");
    
    JButton button = new JButton("+");
    JButton button2 =new JButton("-");
    
    public Click()
    {
       JFrame frame=new JFrame("Counter");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(500,500);
       frame.setVisible(true);
       frame.setLayout(new FlowLayout());
       
     Container stuff=frame.getContentPane();
     
       stuff.add(lol);
       //+ button
       
       button.addActionListener(new Click());
       stuff.add(button);
    //- button
       
       stuff.add(button2);
       button2.addActionListener(new Click());
    }
    
    public static void main(String[] args) {
            // TODO code application logic here
       new Click();
    }
       @Override
     public void actionPerformed(ActionEvent e)
     {
       if(e.getSource().equals(button))       //problem is here &
         sayac++;
     
       else if(e.getSource().equals(button2)) //problem is here
             sayac--;
         
       lol.setText(sayac +" kere tıklandı");
     } 
    }
    

    Just took the code from main() and placed it inside a constructor for Click and had main() call it. I kept those two JButtons as instance variables. I didn't execute but no compile time errors.
  5. In Topic: Creating a Counter With "+" "-" Buttons

    Posted 22 May 2013

    I just compiled the code in Eclipse. It says that "button" and "button2" cannot be resolved to a variable.
    Basically, you declared button and button2 of type JButton in main() but you are trying to use them in another method called actionPerfomed(). You can't do this because those variables are local to main(). What you can do is take the two button variables out of main()'s scope and make them instance variables. Hope that helps :)

    Also, you should make a constructor for Click and place all of the necessary code inside of that and then call it with main(). From a practical standpoint, main() should only have maybe 1 or 2 lines of code.

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
Houston, Texas
Interests:
Programming
Web Development
Music Theory
Full Name:
Jason H.
Years Programming:
2
Programming Languages:
Java SE
HTML5/CSS3

Contact Information

E-mail:
Private

Comments

Page 1 of 1
  1. Photo

    ladyJava Icon

    05 Feb 2013 - 10:05
    Thanks So much ! I actually finished not too long ago. Your help was very useful. Thanks Again !
Page 1 of 1