JScrollPane scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(23, 11, 603, 123);
contentPane.add(scrollPane);
JPanel panel = new JPanel();
JLabel[] labels=new JLabel[callout.getRedCount()]; //callout.getRedCount() = 8
panel.setLayout(null);
int x = 50;
int y = 50;
for (int i=0;i<callout.getRedCount();i++){ //callout.getRedCount() = 8
labels[i]=new JLabel("Red" + i);
labels[i].setBounds(x, y, 32, 32);
panel.add(labels[i]);
x += 150;
}
scrollPane.setViewportView(panel);
I'm trying to add these labels to the panel that is embedded in a scrollPane. The labels are display correctly except for when the last few are over the size of the panel. The scrollPane does not scroll. I have tried .setLayout to various different layouts and still no results. Am I missing something on how to do this?
Okay I got this working with GridBagLayout.
public static void main(String args[]){
JFrame contentPane = new JFrame();
JScrollPane scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(23, 11, 50, 50);
contentPane.add(scrollPane);
JPanel panel = new JPanel();
JLabel[] labels=new JLabel[8]; //callout.getRedCount() = 8
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0,10,0,0);
int x = 50;
int y = 50;
for (int i=0;i<8;i++){ //callout.getRedCount() = 8
labels[i]=new JLabel("Red" + i);
gbc.gridx = i;
panel.add(labels[i], gbc);
}
scrollPane.setViewportView(panel);
contentPane.setSize(50,50);
contentPane.setVisible(true);
}
You can change around the values, this is very rough. Hope this helps!
Related
I am aware of the NORTH, CENTER and SOUTH field details for setting a component (e.g. BorderLayout) to either the top, middle or bottom respectively, however, I was wondering if it's possible to have more than three components and if so, how would I go about doing this with additional field details.
Currently, I have the following.
North - BorderLayout
Center - GridLayout
South - GridBagLayout
I am wanting to add an additional GridBagLayout to the bottom of the program (either below or above existing GridBagLayout). How would I go about adding an additional layout so that it does not interfere with and merge with the current SOUTH area?
Code:
public class boggleView extends JFrame {
public boggleView(){
super("Boggle");
setResizable(false);
setLocationRelativeTo(null);
setMinimumSize(new Dimension(400, 400));
JPanel northPanel = new JPanel();
JPanel middlePanel = new JPanel();
JPanel southPanel = new JPanel();
JPanel southPanelBottom = new JPanel();
// North Panel
getContentPane().add(northPanel, BorderLayout.NORTH);
northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel gameBoardTitle = new JLabel("<html><font size = 5>Game Board</font></html>");
northPanel.add(gameBoardTitle);
// Middle Panel
getContentPane().add(middlePanel, BorderLayout.CENTER);
middlePanel.setLayout(new GridLayout(4,4));
for(int button=0 ; button<16 ; button++){
JButton diceButton = new JButton(String.valueOf("<html><font size = 10>E</font></html>"));
diceButton.setBackground(Color.WHITE);
diceButton.setBorder(new LineBorder(Color.BLACK));
diceButton.setPreferredSize(new Dimension(100, 100));
middlePanel.add(diceButton);
}
// South Panel
southPanel.setLayout (new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,0,10);
c.fill = GridBagConstraints.HORIZONTAL;
Border wordBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
Border textFieldBorder = BorderFactory.createLineBorder(Color.BLACK, 2);
JButton button;
JLabel textField = new JLabel("");
textField.setBorder(textFieldBorder);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 8;
textField.setPreferredSize(new Dimension(0,35));
southPanel.add(textField, c);
button = new JButton("Submit");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
button.setBackground(Color.GREEN);
button.setPreferredSize(new Dimension(80, 35));
southPanel.add(button, c);
button = new JButton("Cancel");
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
button.setBackground(Color.RED);
button.setPreferredSize(new Dimension(80, 35));
southPanel.add(button, c);
button = new JButton("Restart");
c.gridx = 4;
c.gridy = 1;
c.gridwidth = 1;
button.setBackground(Color.CYAN);
button.setPreferredSize(new Dimension(80, 35));
southPanel.add(button, c);
button = new JButton("Info");
c.gridx = 6;
c.gridy = 1;
c.gridwidth = 1;
button.setBackground(Color.CYAN);
button.setPreferredSize(new Dimension(80, 35));
southPanel.add(button, c);
JLabel wordListTitle = new JLabel("<html><font size = 5>Recent Words</font></html>");
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0,5,0,0);
southPanel.add(wordListTitle, c);
// Bottom South Panel
southPanelBottom.setLayout (new GridBagLayout());
GridBagConstraints d = new GridBagConstraints();
d.fill = GridBagConstraints.HORIZONTAL;
d.insets = new Insets(0,0,0,0);
JLabel word = new JLabel("<html><font size = 5>HAT</font></html>");
word.setBorder(wordBorder);
d.gridx = 0;
d.gridy = 3;
d.gridwidth = 2;
southPanelBottom.add(word, d);
JLabel word2 = new JLabel("<html><font size = 5>1</font></html>");
word2.setBorder(wordBorder);
d.gridx = 2;
d.gridy = 3;
d.gridwidth = 1;
southPanelBottom.add(word2, d);
getContentPane().add(southPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
use a new helper Panel with layout Borderlayout.
put your new GridBagLayout panel to this new helper Panel's South and put the existing GridBagLayout panel (currently at SOUTH) this panel's North.
then put this new helper Panel to SOUTH replacing old GridBagLayout panel.
JPanel southHelperPanel = new JPanel(new BorderLayout());
southHelperPanel.add(southPanel, BorderLayout.NORTH);
southHelperPanel.add(southPanelBottom, BorderLayout.SOUTH);
getContentPane().add(southHelperPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
You can just combine layouts as you need them. Please have a look at the below example.. In this example, the main panel has a BorderLayout. In the different directions of this panel I'm adding new panels with other layouts.
public class Test extends JFrame {
public static void main(String[] args) {
Test test = new Test();
test.setVisible(true);
}
public Test() {
JPanel main = new JPanel(new BorderLayout());
JPanel top = new JPanel(new FlowLayout());
JPanel center = new JPanel(new GridBagLayout());
JPanel bottom = new JPanel(new BorderLayout());
main.add(top, BorderLayout.PAGE_START);
main.add(center, BorderLayout.CENTER);
main.add(bottom, BorderLayout.PAGE_END);
this.getContentPane().add(main);
this.setSize(800, 500);
}
}
You could also give your main panel a different layout (like GridBagLayout) if you need more "directions" in the beginning.
BorderLayout cannot itself do this; it can only lay out 3 things vertically. Switch the container (window.getContentPane(), presumably) to a different layout.
Something similar-ish to BorderLayout is to use BoxLayout; I'd need to know a little more about exactly what's in those subcomponents to be sure if it's the right replacement here.
Apologies for my code being so messy. I've added and changed and commented out a lot of code and haven't gotten round to cleaning any of it up yet.
My main problem is being confused about how to get a 5x5 set of images. I need to be able to update it regularly as it is a map. These updates are going to happen in a separate method from the main and it will be difficult to know what image is in a particular square - hence why I've made an ArrayList called previous map.
This is so I can remove the elements of the previous map, then add the new ones. This is really ugly though so I'm trying to change the way I'm doing this by using a JList. I don't fully understand it, but it's cropped up in a lot of the examples I've seen.
What I think is happening is that I can add all my 25 images (as JLabel components) to this list, then add this list to the panel I want it to show up in.
I'm also trying to use GridLayout as that's also shown up in a lot of examples. What I think is happening with grid layout is that I set up a 5x5 grid layout, then use setLayout(<GridLayout name>) to the panel I want the grid to be in. I then add the images I want to be in the map to this grid layout.
Is my understanding correct? I've looked at lots of examples and explanations but I'm still unsure. I'm also struggling to understand how I can combine these two ideas - do I have to? If so am I doing it in the right way? If not how would I do this?
So far, I have managed to get this:
However, for some reason,the images are showing up as a column and not in a 5x5 grid.
Here's the relevant code I've been working on:
public class GameGUI3 extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JFrame f = new JFrame("Dungeons of Doom");
JPanel textPanel = new JPanel();
JPanel leftPanel = new JPanel(new GridBagLayout());
JPanel moveButtonPanel = new JPanel(new GridBagLayout());
JPanel extraButtonPanel = new JPanel(new GridBagLayout());
JPanel mapPanel = new JPanel(new GridBagLayout());
GridLayout gl = new GridLayout(5, 5);
JTextArea textArea = new JTextArea(20, 50);
JScrollPane scrollPane = new JScrollPane(textArea);
DefaultListModel listModel = new DefaultListModel();
JList list = new JList(listModel);
String action;
ArrayList<String> previousMap = new ArrayList<String>();
GridBagConstraints mapC = new GridBagConstraints();
private static GUIClient3 client = new GUIClient3();
JButton n = new JButton("N");
JButton e = new JButton("E");
JButton s = new JButton("S");
JButton w = new JButton("W");
JButton look = new JButton("LOOK");
JButton pickup = new JButton("PICKUP");
JButton hello = new JButton("HELLO");
JButton quit = new JButton("QUIT");
public GameGUI3()
{
GridBagConstraints northC = new GridBagConstraints();
GridBagConstraints eastC = new GridBagConstraints();
GridBagConstraints southC = new GridBagConstraints();
GridBagConstraints westC = new GridBagConstraints();
GridBagConstraints leftTopC = new GridBagConstraints();
GridBagConstraints leftBottomC = new GridBagConstraints(GridBagConstraints extraC = new GridBagConstraints();
northC.insets = new Insets(10, 65, 10, 10);
eastC.insets = new Insets(10, 65, 10, 10);
southC.insets = new Insets(10, 65, 10, 10);
westC.insets = new Insets(10, 0, 10, 10);
leftTopC.insets = new Insets(10, 10, 30, 10);
mapC.insets = new Insets(30, 30, 30, 30);
extraC.insets = new Insets(10, 10, 10, 10);
f.setBounds(100, 100, 1000, 600); // (start on x-axis, start on y, width, height)
textPanel.setBounds(1000, 250, 500, 200);
textPanel.add(new JScrollPane(textArea));
f.getContentPane().add(leftPanel, BorderLayout.WEST);
leftTopC.gridx = 0;
leftTopC.gridy = 0;
leftTopC.anchor = GridBagConstraints.FIRST_LINE_START;
leftPanel.add(moveButtonPanel, leftTopC);
//mapC.gridx = 0;
//mapC.gridy = 1;
//mapC.anchor = GridBagConstraints.LINE_START;
//leftPanel.add(mapPanel, mapC);
//mapPanel.setLayout(gl);
leftPanel.add(mapPanel);
leftBottomC.gridx = 0;
leftBottomC.gridy = 2;
leftBottomC.anchor = GridBagConstraints.LAST_LINE_START;
leftPanel.add(extraButtonPanel, leftBottomC);
f.getContentPane().add(textPanel, BorderLayout.EAST);
textArea.setEditable(false);
this.setVisible(false);
northC.gridx = 0;
northC.gridy = 0;
northC.gridwidth = 3;
northC.anchor = GridBagConstraints.NORTH;
moveButtonPanel.add(n, northC);
westC.gridx = 0;
westC.gridy = 1;
westC.gridwidth = 1;
westC.anchor = GridBagConstraints.WEST;
moveButtonPanel.add(w, westC);
eastC.gridx = 2;
eastC.gridy = 1;
eastC.gridwidth = 3;
eastC.anchor = GridBagConstraints.EAST;
moveButtonPanel.add(e, eastC);
southC.gridx = 0;
southC.gridy = 2;
southC.gridwidth = 3;
southC.anchor = GridBagConstraints.SOUTH;
moveButtonPanel.add(s, southC);
mapC.gridx = 0;
mapC.gridy = 2;
//mapC.gridwidth = 10;
//mapC.anchor = GridBagConstraints.LINE_START;
//mapPanel.setLayout(gl);
leftPanel.add(mapPanel, mapC);
ImageIcon HereBeDragonsIcon = new ImageIcon("HereBeDragons.jpg", "Image");
JLabel HereBeDragonsLabel = new JLabel(HereBeDragonsIcon);
//mapPanel.add(HereBeDragonsLabel);
int count=0;
for(int i=0; i<4; i++)
{
listModel.add(count++, HereBeDragonsIcon);
//listModel.addElement(HereBeDragonsIcon);
previousMap.add("HereBeDragonsLabel");
//mapPanel.add(HereBeDragonsLabel);
}
//JList list = new JList(listModel);
//mapPanel.add(list);
mapPanel.add(list);
extraButtonPanel.add(look, extraC);
extraButtonPanel.add(pickup, extraC);
extraButtonPanel.add(hello, extraC);
extraButtonPanel.add(quit, extraC);
n.addActionListener(this);
e.addActionListener(this);
s.addActionListener(this);
w.addActionListener(this);
look.addActionListener(this);
pickup.addActionListener(this);
hello.addActionListener(this);
quit.addActionListener(this);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
Many thanks - if anything is unclear please let me know and I will try and re-phrase my question or explain my code better.
i have added the scroll bar but it does not scroll up and down the frame and i do not see the upper label i have created .
kindly give me any suggestion so i can scroll up and down in my frame.
i have a question also that when my frame size is fixed and i want to create
many labels and text fields that i must have to scroll down so in that case i have to increase its frame size or not
public static void main(String[] args)
{
JFrame frame = new JFrame("jframe");
JLabel[] labels=new JLabel[50];
for (int i=0;i<labels.length;i++)
{
labels[i]=new JLabel("Column" + i);
}
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cst = new GridBagConstraints();
JScrollBar vbar=new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 500);
for(int i =0 ; i<50 ;i++)
{
cst.fill = GridBagConstraints.HORIZONTAL;
cst.gridx = 0;
cst.gridy = i;//
cst.gridwidth = 2;
panel.add(labels[i],cst);
}
frame.getContentPane().add(vbar, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1300,700);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
why don't you use a JScrollPane and put your panel on it?
take a look at: https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
public static void main(String[] args)
{
JFrame frame = new JFrame("jframe");
JLabel[] labels = new JLabel[50];
for(int i = 0; i < labels.length; i++)
{
labels[i] = new JLabel("Column" + i);
}
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cst = new GridBagConstraints();
// JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL,30,40,0,500);
for(int i = 0; i < 50; i++)
{
cst.fill = GridBagConstraints.HORIZONTAL;
cst.gridx = 0;
cst.gridy = i;//
cst.gridwidth = 2;
panel.add(labels[i],cst);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1300,700);
frame.setVisible(true);
}
I have written the following code to add a JLabel to a JPanel but it displays it in the center, while I expected it to be placed at the top of the JPanel.
Here is the piece of code I'm referring to:
JPanel pnlProjects = new JPanel();
pnlProjects.setMinimumSize(new Dimension(10, 300));
GridBagLayout gridBagLayout = new GridBagLayout();
pnlProjects.setLayout(gridBagLayout);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
// add multiple label dynamically;
for (int count = 0; count < project.length; count++) {
lblProjects[count] = new JLabel("Project"+count );
lblProjects[count].setHorizontalAlignment(SwingConstants.LEFT);
lblProjects[count].setHorizontalTextPosition(SwingConstants.LEFT);
lblProjects[count].setBorder(BorderFactory.createBevelBorder(0));
lblProjects[count].setPreferredSize(new Dimension(100, 20));
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = count;
pnlProjects.add(lblProjects[count], gridBagConstraints);
}
// Add project panel in to the scorllPan
JScrollPane jspProjectlist = new JScrollPane(pnlProjects);
Would anyone be ablt to explain to me how to change it as per my requirement?
You can use next trick: add next code after your loop:
gridBagConstraints.weighty=1;
gridBagConstraints.gridy++;
pnlProjects.add(new JLabel(" "), gridBagConstraints);
That dummy JLabel will grab all space under your project JLabel's.
Im having a hard time identifying what layout to be used. help any suggestions.
JPanel mainpanel = new JPanel();
public void addPanel(JPanel panel){
mainpanel.add(panel);
}
addPanel(A);
addPanel(B);
addPanel(C);
addPanel(D);
addPanel(E);
....
Panels A,B,C,D... are not of fixed sized.
How can I make this possible?
Seems like by using GridBagLayout, you can achieve this. Hopefully you can change the JPanel with MAGENTA colour as per your liking :-)
Here is the output :
And here is the code :
import java.awt.*;
import javax.swing.*; //swing package
public class GridBagLayoutTest
{
//defining the constructor
public GridBagLayoutTest()
{
JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
maFrame.setLocationByPlatform(true); //centering frame
JPanel headPanel = new JPanel(); //creating the header panel
Container container = maFrame.getContentPane();
container.setLayout(new GridBagLayout()); //setting layout of main frame
GridBagConstraints cns = new GridBagConstraints(); //creating constraint
cns.gridx = 0;
cns.gridy = 0;
//cns.gridwidth = 3;
//cns.gridheight = 4;
cns.weightx = 0.5;
cns.weighty = 0.2;
cns.anchor = GridBagConstraints.FIRST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
headPanel.setBackground(Color.BLUE);
container.add(headPanel, cns);
JPanel panel = new JPanel();
panel.setBackground(Color.CYAN);
cns = new GridBagConstraints();
cns.gridx = 1;
cns.gridy = 0;
//cns.gridwidth = 7;
//cns.gridheight = 4;
cns.weightx = 0.5;
cns.weighty = 0.2;
cns.anchor = GridBagConstraints.FIRST_LINE_END;
cns.fill = GridBagConstraints.BOTH;
container.add(panel, cns);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.RED);
cns = new GridBagConstraints();
cns.gridx = 0;
cns.gridy = 1;
//cns.gridwidth = 2;
cns.gridheight = 2;
cns.weightx = 0.5;
cns.weighty = 0.3;
cns.anchor = GridBagConstraints.LINE_START;
cns.fill = GridBagConstraints.BOTH;
container.add(panel1, cns);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.PINK);
cns = new GridBagConstraints();
cns.gridx = 1;
cns.gridy = 1;
//cns.gridwidth = 2;
//cns.gridheight = 2;
cns.weightx = 0.5;
cns.weighty = 0.2;
cns.anchor = GridBagConstraints.LINE_END;
cns.fill = GridBagConstraints.BOTH;
container.add(panel2, cns);
JPanel panel4 = new JPanel();
panel4.setBackground(Color.ORANGE);
cns = new GridBagConstraints();
cns.gridx = 1;
cns.gridy = 2;
//cns.gridwidth = 2;
//cns.gridheight = 2;
cns.weightx = 0.5;
cns.weighty = 0.2;
cns.anchor = GridBagConstraints.LINE_END;
cns.fill = GridBagConstraints.BOTH;
container.add(panel4, cns);
JPanel mainPanel = new JPanel();
mainPanel.setBackground(Color.WHITE);
mainPanel.setLayout(new GridBagLayout());
cns = new GridBagConstraints();
cns.gridx = 0;
cns.gridy = 4;
cns.gridwidth = 2;
cns.gridheight = 2;
cns.weightx = 1.0;
cns.weighty = 0.3;
cns.anchor = GridBagConstraints.LAST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
container.add(mainPanel, cns);
JPanel panel3 = new JPanel();
panel3.setBackground(Color.MAGENTA);
cns = new GridBagConstraints();
cns.gridx = 0;
cns.gridy = 0;
//cns.gridwidth = 2;
//cns.gridheight = 2;
cns.weightx = 0.5;
cns.weighty = 0.2;
cns.anchor = GridBagConstraints.FIRST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
mainPanel.add(panel3, cns);
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.WHITE);
cns = new GridBagConstraints();
cns.gridx = 0;
cns.gridy = 1;
//cns.gridwidth = 2;
//cns.gridheight = 2;
cns.weightx = 1.0;
cns.weighty = 0.2;
cns.anchor = GridBagConstraints.LAST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
mainPanel.add(bottomPanel, cns);
//JButton button = new JButton("BUTTON");
//headPanel.add(button);
maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
maFrame.pack();
maFrame.setVisible(true); //making the frame visible
}
//defining the main method
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
public void run()
{
new GridBagLayoutTest(); //instantiating the class
}
};
SwingUtilities.invokeLater(runnable);
}
}
Consider using JGoodies Forms layout. it is highly flexible and easy to use. Any sort of layouting of widgets are possible, vertical stacking , horizontal stacking etc.
http://www.jgoodies.com/freeware/forms/index.html
/* B1 Space B2 Space B3 Space B4 */
String col1 = "10dlu, 3dlu, 10dlu, 3dlu 10dlu, 3dlu, 10ldu";
/*Width of button */
String row1 = "5dlu";
FormLayout layout = new FormLayout( col1, row1);
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
/* X - stands for column position :: Y - stand for row */
panel.add(new JButton("B1"), cc.xy(1, 1));
panel.add(new JButton("B2"), cc.xy(2, 1));
.
.
.
there is one more cc.xyh(1,1,2) where 'h' stands for vertical span or width.
Yes, you read more about JGoodies forms layout, 'DLU' means dialog units, rather than me explaining all these, kindly visit JGoodies site, download the library and documentation. Trust be, this layout manager is much better, in terms of usability , maintainability and readability.
I would say, create a temporary panel for ACF, then BDE, then create one to combine ACF and BDE, then add the last temp panel and your main panel onto the frame with BorderLayout NORTH and SOUTH
Grid layout might be fit into this shape. Just look to the java swing documentation.
Depends on what the behaviour should be when the window is resized. But you won't avoid building a tree structure with JPanels.
The top component can have BorderLayout, with one panel in the North and main panel in Center.
The North panel will have X-axis BoxLayout and contain two panels.
Both these panels should have Y-axis BoxLayout and the first one will contain A, C, F and the second one B, D, E.
You should also set preferred sizes for A B C D E and F so that they render with appropriate size.
EDIT:
Here, I created an example:
public class GladysPanel extends JPanel
{
public GladysPanel(JComponent A, JComponent B, JComponent C, JComponent D, JComponent E, JComponent F, JComponent main){
super(new BorderLayout());
JPanel abcdef = new JPanel(new BorderLayout());
Box ab = new Box(BoxLayout.X_AXIS);
ab.add(A);
ab.add(B);
abcdef.add(ab, BorderLayout.NORTH);
Box cdef = new Box(BoxLayout.X_AXIS);
Box cf = new Box(BoxLayout.Y_AXIS);
cf.add(C);
cf.add(F);
cf.add(Box.createVerticalGlue());
Box de = new Box(BoxLayout.Y_AXIS);
de.add(D);
de.add(E);
de.add(Box.createVerticalGlue());
cdef.add(cf, BorderLayout.WEST);
cdef.add(de, BorderLayout.EAST);
abcdef.add(cdef);
add(abcdef, BorderLayout.NORTH);
add(main);
}
public static void main(String[] args){
JPanel A = new JPanel();
A.setOpaque(true);
A.setBackground(Color.BLUE);
A.add(new JLabel("A"));
JPanel B = new JPanel();
B.setOpaque(true);
B.setBackground(Color.LIGHT_GRAY);
B.add(new JLabel("B"));
JPanel C = new JPanel();
C.setPreferredSize(new Dimension(0, 100));
C.setOpaque(true);
C.setBackground(Color.RED);
C.add(new JLabel("C"));
JPanel D = new JPanel();
D.setOpaque(true);
D.setBackground(Color.PINK);
D.add(new JLabel("D"));
JPanel E = new JPanel();
E.setOpaque(true);
E.setBackground(Color.YELLOW);
E.add(new JLabel("E"));
E.setPreferredSize(new Dimension(0, 60));
JPanel F = new JPanel();
F.setOpaque(true);
F.setBackground(Color.MAGENTA);
F.add(new JLabel("F"));
JPanel main = new JPanel();
main.setOpaque(true);
main.setBackground(Color.WHITE);
main.add(new JLabel("main"));
GladysPanel panel = new GladysPanel(A, B, C, D, E, F, main);
JFrame example = new JFrame("Gladys example");
example.setContentPane(panel);
example.setSize(300, 300);
example.setVisible(true);
}
}
you can omit the setPreferredSize(), I added it only to demonstate behaviour. You can also try to resize the window. The code is much much shorter that when using GridBagLayout.