Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GUI - JLayerPane and GridBagLayout Please Help

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
I'm new to Java and am working on an GUI. I created a screen with JLayeredPane and want to populate the JPanes with JCombo, and JLabel components.

I want the group of JPanes/JLabels ect. to be in the center and stay there when the screen is enlarged. I tried to use the GrideBagLayout with the JlayeredPane, but it would not work.

Here is an example of what I want the screen to look like:

import javax.swing.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;


public class SimpleLayers extends JFrame{

private GridBagLayout gbl;
private GridBagConstraints gbc;

public SimpleLayers (String caption) {
super (caption);
setBackground(Color.lightGray);

gbl = new GridBagLayout();
gbc = new GridBagConstraints();

// setLayout(gbl);

getContentPane().setLayout(gbl);

JLabel lblName = null;
JFrame f = new JFrame();
JLayeredPane lp = f.getLayeredPane();


JPanel top = new JPanel();
top.setBackground(Color.white);
top.setBounds(50, 70, 150, 200);

JPanel mid1 = new JPanel ();
mid1.setBackground(Color.gray);
mid1.setBounds(60,100,130,40);

JPanel mid2 = new JPanel ();
mid2.setLayout(gbl);
mid2.setBackground(Color.red);
mid2.setBounds(60,150,130,40);

JPanel mid3 = new JPanel ();
mid3.setBackground(Color.gray);
mid3.setBounds(60,200,130,40);

JPanel bottom = new JPanel();
bottom.setBackground(Color.black);
bottom.setBounds(60,120,150,170);

lblName = new JLabel("Test");
lblName.setFont(new Font("Arial", Font.BOLD, 14));
lblName.setForeground(Color.red);

mid1.add(lblName, gbc);
set_gbc(lblName, 0,0,5,1);

// mid1.add(lblName);

//Place the buttons in different layers
lp.add(bottom, new Integer(1));
lp.add(top, new Integer(2));
lp.add(mid1, new Integer(3));
lp.add(mid2, new Integer(4));
lp.add(mid3, new Integer(5));

// Show the Frame
f.setSize(600,500);
f.setVisible(true);

}
public static void main(String[] args)
{
new SimpleLayers("Java Test");
}

private void set_gbc(Component c, int row, int column, int width, int height) {
gbc.gridy = row;
gbc.gridx = column;
gbc.gridwidth = width;
gbc.gridheight = height;
}
}

Would someone please help me understand what is wrong. That is How to offset the shadow, have the large pane and three other planes with their components offset the shadow and increse correctly when the screen is enlarged.

Thanks...Ronnie

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top