Results 1 to 4 of 4
Thread: GUI Button Help
- 11-15-2010, 04:06 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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); } } }
- 11-15-2010, 05:26 PM #2
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.
-
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.
- 11-15-2010, 06:02 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
Tab button
By Zack in forum Suggestions & FeedbackReplies: 0Last Post: 07-28-2010, 08:40 AM -
SWT - X Button
By dorgin in forum SWT / JFaceReplies: 0Last Post: 06-16-2010, 04:05 PM -
Button
By Tb0h in forum New To JavaReplies: 6Last Post: 07-22-2009, 01:28 AM -
Using SWT Button
By Java Tip in forum Java TipReplies: 0Last Post: 01-08-2008, 09:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks