Results 1 to 6 of 6
Thread: Class that extends JFrame help
- 11-10-2010, 01:45 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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); } } }
-
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!
- 11-10-2010, 02:02 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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?
-
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.
- 11-10-2010, 02:27 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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!
-
Similar Threads
-
JFrame Hanging When Called From Another Class
By al_Marshy_1981 in forum AWT / SwingReplies: 5Last Post: 03-30-2010, 07:04 PM -
add object to ArrayList (object is from extends other class)
By mBull in forum Java AppletsReplies: 3Last Post: 03-15-2010, 08:44 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
[SOLVED] how to print a class that extends another class
By alpdog14 in forum New To JavaReplies: 3Last Post: 03-19-2009, 05:00 PM -
Implements MyClass extends JFrame
By coco in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks