Home arrow static arrow Java Programming [Archive] - JPanel and Layout Managers
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - JPanel and Layout Managers
This topic has 3 replies on 1 page.

Posts:49
Registered: 3/31/04
JPanel and Layout Managers  
Aug 1, 2004 7:01 PM



 
Hello, I have written a very basic JApplet program. The output is supposed to be 4 JPanels in a Grid layout, with 4 buttons within each of the 4 JPanels. I believe I have achieved that output, but am not 100% sure, as I am just learning about Layout Managers and the JPanel class. If anyone could just take a look at my code and check the output, it would be greatly appreciated. Thank you.

import javax.swing.*;import java.awt.*;import java.awt.event.*;public class TwoByTwo extends JApplet{	private JButton a = new JButton("A");	private JButton b = new JButton("B");	private JButton c = new JButton("C");	private JButton d = new JButton("D");	private JButton e = new JButton("E");	private JButton f = new JButton("F");	private JButton g = new JButton("G");	private JButton h = new JButton("H");	private JButton i = new JButton("I");	private JButton j = new JButton("J");	private JButton k = new JButton("K");	private JButton l = new JButton("L");	private JButton m = new JButton("M");	private JButton n = new JButton("N");	private JButton o = new JButton("O");	private JButton p = new JButton("P"); 	public void init()	{		Container container = this.getContentPane();		container.setLayout(new GridLayout(2,2,2,2));		JPanel p1 = new JPanel();		JPanel p2 = new JPanel();		JPanel p3 = new JPanel();		JPanel p4 = new JPanel();		p1.add(a);		p1.add(b);		p1.add(c);		p1.add(d);		p2.add(e);		p2.add(f);		p2.add(g);		p2.add(h);		p3.add(i);		p3.add(j);		p3.add(k);		p3.add(l);		p4.add(m);		p4.add(n);		p4.add(o);		p4.add(p);		container.add(p1);		container.add(p2);		container.add(p3);		container.add(p4);	}} 
 

Posts:8,813
Registered: 10/4/00
Re: JPanel and Layout Managers  
Aug 1, 2004 7:25 PM (reply 1 of 3)



 
You've achieved '4 JPanels in a Grid layout, with 4 buttons within each of the 4 JPanels'. As long as that was the assignment, you did it. However, every time you find yourself writing the same line of code over and over a little voice should pop up.
There must be a better way. Here it is.
import javax.swing.*;import java.awt.*;import java.awt.event.*;public class TwoByTwo extends JApplet {  public void init() {    Container content = getContentPane();    content.setLayout(new GridLayout(2, 2, 2, 2));    char[] c = {'A'};    for (int i=0; i<4; i++) {      JPanel subPanel = new JPanel();      content.add(subPanel);      for (int j=0; j<4; j++) {        subPanel.add(new JButton(new String(c)));        c[0]++;      }    }  }}
This uses some advanced techniques that your instructor will probably recognize as being above your level, so you might think twice about turning it in as your own. Rember thsi advices.
 

Posts:49
Registered: 3/31/04
Re: JPanel and Layout Managers  
Aug 1, 2004 7:37 PM (reply 2 of 3)



 
Point well taken bbritta. I mainly posted this to the forum for clarification - I make it a personal rule to NEVER expect someone else to redo my work for me (although you have kindly done so here). I find this forum an excellent resource, and at times, I like to submit questions for others' perspectives. Thank you for your prompt and helpful reply.

Regards,

Randy
 

Posts:8,813
Registered: 10/4/00
Re: JPanel and Layout Managers  
Aug 1, 2004 7:42 PM (reply 3 of 3)



 
Good rule. I kind of figured you had it or I wouldn't have posted that code. Hope your find the 'little voice' helpful.
 
This topic has 3 replies on 1 page.