Results 1 to 6 of 6
  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default Class that extends JFrame help

    I am making this program for my AP Java class. The assignment is to make a GUI with 4x4 grid with 2 buttons. One button will randomize the colors of the JPanels in the grid and the other will set them to alternating black and white. I have compiled and and everything and I cannot get the buttons to work in the client code. HELP!?! Thanks.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    class RandomColorsGUI extends JFrame
    {
        private JPanel p1 = new JPanel();
        private JPanel p2 = new JPanel();
    
        private JButton randomButton = new JButton("Randomize");
        private JButton blackAndWhiteButton = new JButton("Black and White");
    
        private int grid = 16;
    
        private Random gen = new Random();
    
        private Container pane = getContentPane();
    
        public RandomColorsGUI()
        {
    
            //Container pane = getContentPane();
            pane.setLayout(new GridLayout(5, 4));
    
            while(grid > 0)
            {
                Color randomColor = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256));
    
                JPanel p1 = new JPanel();
                p1.setBackground(randomColor);
                pane.add(p1);
                grid--;
    
            }
    
            JPanel rButton = new JPanel();
            rButton.add(randomButton);
    
            JPanel bButton = new JPanel();
            bButton.add(blackAndWhiteButton);
    
            pane.add(p2);
            pane.add(randomButton);
            pane.add(blackAndWhiteButton);
    
            randomButton.addActionListener(new RandomListener());
            blackAndWhiteButton.addActionListener(new BlackListener());
    
        }
    
        private class RandomListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                //Container pane = getContentPane();
                pane.setLayout(new GridLayout(5, 4));
                while(grid > 0)
                {
                    Color randomColor = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256));
    
                    JPanel p1 = new JPanel();
                    p1.setBackground(randomColor);
                    pane.add(p1);
                    grid--;
    
                }
    
    
                JPanel rButton = new JPanel();
                rButton.add(randomButton);
    
                JPanel bButton = new JPanel();
                bButton.add(blackAndWhiteButton);
    
                pane.add(p2);
                pane.add(randomButton);
                pane.add(blackAndWhiteButton);
            }
        }
    
        private class BlackListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                //Container pane = getContentPane();
                pane.setLayout(new GridLayout(5, 4));
                while(grid > 0)
                {
                    if(grid % 2 == 0)
                    {
                        JPanel p1 = new JPanel();
                        p1.setBackground(Color.black);
                        pane.add(p1);
                        grid--;
                    }
    
                    else
                    {
                        JPanel p1 = new JPanel();
                        p1.setBackground(Color.white);
                        pane.add(p1);
                        grid--;
                    }
    
                }
    
    
                JPanel rButton = new JPanel();
                rButton.add(randomButton);
    
                JPanel bButton = new JPanel();
                bButton.add(blackAndWhiteButton);
    
                pane.add(p2);
                pane.add(randomButton);
                pane.add(blackAndWhiteButton);
            } 
        } 
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to think through your program logic before committing it to code since the code must be built on a rock-solid skeleton of logic.

    Can you explain the logic of your RandomListener for instance, in English? What is it attempting to do? Seemingly adding more JPanels to the contentPane... What is it doing with the JPanels already there? It seems to depend on the value of the grid variable for its loop to work? What do you think the value of grid is when this method starts? If you're not sure, it's easy to test with a simple System.out.println statement.

    Give this some thought, and if you're still confused, please come on back and let's try to help you out some more.

    Luck!

  3. #3
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    I saw that I needed to reset the grid value to 16. How do I clear the Container so that I can add the new JPanel's?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Myself, I'd approach it differently. I'd create a 2-dimensional array of JPanel and add them to the GUI. Then rather than create new JPanels on button press, simply iterate through the 2D array and set the colors of the existing panels. It seems simpler to me, but that's just one man's opinion.

  5. #5
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    I really do not know much about arrays...but I sifted through the API's and found a Container.removeAll() method that deletes all components out of the container. Thanks for your help!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by javaman1 View Post
    I really do not know much about arrays...but I sifted through the API's and found a Container.removeAll() method that deletes all components out of the container. Thanks for your help!
    If it works for, then have at it! Best of luck, and you're welcome!

Similar Threads

  1. JFrame Hanging When Called From Another Class
    By al_Marshy_1981 in forum AWT / Swing
    Replies: 5
    Last Post: 03-30-2010, 08:04 PM
  2. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  3. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  4. Replies: 3
    Last Post: 03-19-2009, 06:00 PM
  5. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 04:43 AM

Tags for this Thread

Posting Permissions

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