-
JLayeredPane/GridBagLayout
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
-
i could fake the whole thing without using a single layered pane or panel. for more info, read the gridbag tutorial in my sig
if the images dont show, my webhost is being lame, wait a while and try again
-
Hi JCard
Using a GridbagLayout for the screen is a great idea. I think I never thought of that approach because it seems hard.
Hopefully I will be able to make it work.
Thanks.
Ronnie
-
if you get stuck, post to the gridbag thread (on a different board - registration required)
-
Thanks
Is the fourm the same one where I found your tutroial?
Thanks...(VB was a breeze...)
Ronnie
-
Thanks
Thanks Steve. I think I need the Array List becuase I am not certain of the number of variables in my array.
Ronnie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks