Results 1 to 4 of 4

Thread: GUI Button Help

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

    Default GUI Button Help

    I am writing this GUI program for my AP Java class. We have to create a grid that is 4x4 and randomize 16 colored JPanels in the JFrame. There also has to be 3 button, one that will randomize all the colors, one that will set all of the JPanels to black and white, and a final one that will "clear" the JFrame. This will only set the JPanels back to their default color. I can't get the "clear" button to work. The only time it will work is the beginning of the program. You can press is once, then never again. If you press one of the other two buttons, you can't go to the "clear" button. Any help? Thanks.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    //************
    //Adam Brewer*
    //Lab 12     *
    //************
    
    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 \n\nand White");
        private JButton clearButton = new JButton("Clear");
    
        private int grid = 16;
    
        private Random gen = new Random();
    
        private Container pane = getContentPane();
    
        public RandomColorsGUI()
        {
            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);
    
            JPanel cButton = new JPanel();
            cButton.add(clearButton);
    
            pane.add(p2);
            pane.add(randomButton);
            pane.add(blackAndWhiteButton);
            pane.add(cButton);
    
            randomButton.addActionListener(new RandomListener());
            blackAndWhiteButton.addActionListener(new BlackListener());
            clearButton.addActionListener(new ClearListener());
        }
    
        private class RandomListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                pane.removeAll();
                grid = 16;
                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--;
    
                }
    
                pane.validate();
                JPanel rButton = new JPanel();
                rButton.add(randomButton);
    
                JPanel bButton = new JPanel();
                bButton.add(blackAndWhiteButton);
    
                JPanel cButton = new JPanel();
                cButton.add(clearButton);
    
                pane.add(p2);
                pane.add(randomButton);
                pane.add(blackAndWhiteButton);
                pane.add(cButton);
            }
        }
    
        private class BlackListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                pane.removeAll();
                grid = 16;
                pane.setLayout(new GridLayout(5, 4));
                while(grid > 0)
                {
                    if(grid == 1 || grid == 3 || grid == 6 || grid == 8 || grid == 9 || grid == 11 || grid == 14 || grid == 16)
                    {
                        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--;
                    }
    
                }
    
                pane.validate();
                JPanel rButton = new JPanel();
                rButton.add(randomButton);
    
                JPanel bButton = new JPanel();
                bButton.add(blackAndWhiteButton);
    
                JPanel cButton = new JPanel();
                cButton.add(clearButton);
    
                pane.add(p2);
                pane.add(randomButton);
                pane.add(blackAndWhiteButton);
                pane.add(cButton);
            }
        }
    
        private class ClearListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                pane.removeAll();
                grid = 16;
                pane.setLayout(new GridLayout(5, 4));
    
                while(grid > 0)
                {
                    //Color c1 = new Color(Color.white);
                    JPanel p1 = new JPanel();
                    //p1.setBackground(Color.white);
                    pane.add(p1);
                    grid--;
                }
    
                pane.validate();
                JPanel rButton = new JPanel();
                rButton.add(randomButton);
    
                JPanel bButton = new JPanel();
                bButton.add(blackAndWhiteButton);
    
                JPanel cButton = new JPanel();
                cButton.add(clearButton);
    
                pane.add(p2);
                pane.add(randomButton);
                pane.add(blackAndWhiteButton);
                pane.add(cButton);
            }
        }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    the code of RandomColorsGUI is a waste of resources. each time a button is pushed you remove all panels, recreate it and build the whole frame from scratch. this is not necessery. look at the following code: is stores the jpanels holding the colors in an array of jpanels so each time you push a button you can retrieve the jpanel and update it with an other color. this result is a much shorter code wasting less resources and a better maintenance. further i used only two main panels in my frame, one called center (which holds all jpanel in the array) and one called south with the buttons.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    //************
    //Adam Brewer*
    //Lab 12     *
    //************
    
    import java.util.Random;
    
    class RandomColorsGUI extends JFrame {
    	// private JPanel p1 = new JPanel();
    	JPanel center;
    	JPanel south;
    
    	private JButton randomButton = new JButton("Randomize");
    	private JButton blackAndWhiteButton = new JButton("Black \n\nand White");
    	private JButton clearButton = new JButton("Clear");
    
    	private int grid = 16;
    
    	private JPanel[] panelArr;
    
    	private Random gen = new Random();
    
    	private Container pane = getContentPane();
    
    	public static void main(String[] args) {
    		RandomColorsGUI frame = new RandomColorsGUI();
    		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		frame.setBounds(100, 100, 400, 180);
    		frame.setVisible(true);
    
    	public RandomColorsGUI() {
    		center = new JPanel();
    
    		center.setLayout(new GridLayout(0, 4));
    
    		panelArr = new JPanel[grid];
    
    		while (grid > 0) {
    			Color randomColor = new Color(gen.nextInt(256), gen.nextInt(256),
    					gen.nextInt(256));
    
    			grid--;
    			panelArr[grid] = new JPanel();
    			panelArr[grid].setBackground(randomColor);
    			center.add(panelArr[grid]);
    		}
    
    		pane.add(center, BorderLayout.CENTER);
    
    		JPanel south = new JPanel();
    		south.add(randomButton);
    		south.add(blackAndWhiteButton);
    		south.add(clearButton);
    
    		randomButton.addActionListener(new RandomListener());
    		blackAndWhiteButton.addActionListener(new BlackListener());
    		clearButton.addActionListener(new ClearListener());
    
    		pane.add(south, BorderLayout.SOUTH);
    
    		randomButton.addActionListener(new RandomListener());
    		blackAndWhiteButton.addActionListener(new BlackListener());
    		clearButton.addActionListener(new ClearListener());
    	}
    
    	private class RandomListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			grid = 16;
    			while (grid > 0) {
    				grid--;
    				Color randomColor = new Color(gen.nextInt(256),
    						gen.nextInt(256), gen.nextInt(256));
    				panelArr[grid].setBackground(randomColor);
    			}
    		}
    	}
    
    	private class BlackListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			grid = 16;
    			while (grid > 0) {
    				grid--;
    				// because the index of arrays is zero-based you must use grid - 1
    				if (grid == 0 || grid == 2 || grid == 5 || grid == 7
    						|| grid == 8 || grid == 10 || grid == 13 || grid == 15) {
    					panelArr[grid].setBackground(Color.black);
    				}
    				else {
    					panelArr[grid].setBackground(Color.white);
    				}
    			}
    		}
    	}
    
    	private class ClearListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    
    			grid = 16;
    
    			while (grid > 0) {
    				// Color c1 = new Color(Color.white);
    				grid--;
    				panelArr[grid].setBackground(Color.white);
    			}
    		}
    	}
    }
    Last edited by j2me64; 11-15-2010 at 05:38 PM.

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

    Default

    I've suggested he use an array of JPanel in two previous threads but without success. We may be wasting our breath on this suggestion.

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

    Default

    Sorry guys but I have not learned arrays yet. I guess I will look into them on my own. Thanks for your help!

Similar Threads

  1. Tab button
    By Zack in forum Suggestions & Feedback
    Replies: 0
    Last Post: 07-28-2010, 08:40 AM
  2. SWT - X Button
    By dorgin in forum SWT / JFace
    Replies: 0
    Last Post: 06-16-2010, 04:05 PM
  3. Button
    By Tb0h in forum New To Java
    Replies: 6
    Last Post: 07-22-2009, 01:28 AM
  4. Using SWT Button
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:05 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
  •