Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with Code

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help with Code

    Ok so I have to use two buttons, one increment and one decrement. Also I must display a label with a number in it. When one of the buttons are pushed, the number in the label will either decrease or increase. I can't figure out how to change the number in the label once the button is pushed. I can get everything displayed right, but can't get the buttons to work. Here is my code:

    HTML Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    public class Number extends JPanel
    {
        private JLabel figure;
        private JButton Increment;
        private JButton Decrement;
        private int value;
        
        public Number (){
            
            Increment = new JButton ("Increment");
            Decrement = new JButton ("Decrement");
            
            value = 50;
            figure = new JLabel ("Value of Number: " + value );
            
            Increment.addActionListener (new ButtonListener1());
            //Decrement.addActionListener (new ButtonListener2());
            
            add (Increment);
            add (Decrement);
            add (figure);
            
            setPreferredSize (new Dimension(300, 100));
            setBackground (Color.green);
        }
        
        private class ButtonListener1 implements ActionListener{
            
            public void actionPerformed (ActionEvent event){
                
                int incrementNumber;
                
                Random generator = new Random();
                
                incrementNumber = generator.nextInt(40) +10;
                value = value + incrementNumber;
                
                
                
            }
            
        }
    }
    I know I only have one of the buttonlisteners set-up...just trying to get the 1st working first. Also here is the other class, but probably not needed:

    HTML Code:
    import javax.swing.JFrame;
    
    public class NumberPanel{
    
        public static void main (String[] args){
    
            JFrame frame = new JFrame ("Number Program");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        
            frame.getContentPane().add(new Number());
        
            Number panel = new Number();
        
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setVisible(true);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Code

    can't get the buttons to work
    What doesn't work? Is the action listener called? Add a print out to show if it is.

    BTW Naming conventions for variables states they should begin with lowercase letters. Class names with uppercase.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Bradshjo (September 26th, 2010)

  4. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Code

    Ok when I did the print out, pushing the increment button worked, it keep adding on top of the set value of 50. But what I want to do is to have the JLabel figure that is at 50 currently to increase within the JFrame. I can't get the 50 to change value when the increment button is pushed inside the JFrame.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Code

    have the JLabel figure that is at 50 currently to increase
    Where in your code do you try to change the text that is shown in the JLabel?
    You set it to an initial value and never do anything more to change it.
    Read the API doc for the JLabel class to see how to change its text.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Bradshjo (September 26th, 2010)

  7. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help with Code

    ok i found out how to do it...thanks