I Have a JPanel and a Applet inside a JFrame, and im tryng to align them like this:
Im almost loosing my hair on this as it seems so hard to align...
This is my actual snippet:
The JFrame is opening very small with only the button on it.
final JFrame f = new JFrame();
JPanel appletPanel = new JPanel();
appletPanel.setBackground(Color.RED);
JPanel menuPanel = new JPanel();
menuPanel.setBackground(Color.BLUE);
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// f.setExtendedState(JFrame.MAXIMIZED_BOTH);
// f.setResizable(false);
int w = Toolkit.getDefaultToolkit().getScreenSize().width;
int h = Toolkit.getDefaultToolkit().getScreenSize().height;
f.setSize(Toolkit.getDefaultToolkit().getScreenSize());
VNCApplet applet = new VNCApplet();
menuPanel.add(new JButton("TEST"));
appletPanel.setSize((int)(w*0.7),h);
menuPanel.setSize((int)(w*0.3),h);
c.gridx = 0;
c.gridy = 0;
f.getContentPane().add(appletPanel,c);
c.gridx = 0;
c.gridy = 1;
f.getContentPane().add(menuPanel,c);
f.pack();
applet.init();
applet.start();
f.setVisible(true);
Thanks alot for the attention !
That layout could be achieved a number of ways using a single layout (e.g. a GridBagLayout or a GroupLayout) but I'd do it as a combination of layouts. Like this:
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class AppletWithButtonsOnRight {
private JComponent ui = null;
AppletWithButtonsOnRight() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new TitledBorder("BorderLayout(4,4)"));
JPanel appletPanel = new JPanel(new GridLayout());
appletPanel.setBackground(Color.RED);
appletPanel.add(new JLabel(new ImageIcon(new BufferedImage(400, 300, BufferedImage.TYPE_INT_ARGB))));
ui.add(appletPanel);
JPanel menuPanel = new JPanel(new BorderLayout());
menuPanel.setBorder(new TitledBorder("BorderLayout()"));
ui.add(menuPanel, BorderLayout.LINE_END);
JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 10, 10));
buttonPanel.setBorder(new TitledBorder("GridLayout(0,1,10,10)"));
menuPanel.add(buttonPanel, BorderLayout.PAGE_START);
for (int i=1; i<5; i++) {
JButton b = new JButton("Button " + i);
b.setFont(b.getFont().deriveFont(24f));
buttonPanel.add(b);
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
AppletWithButtonsOnRight o = new AppletWithButtonsOnRight();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
Related
I have coded a GUI that looks like shown below. It is coded as such:
There is a main Frame managed with BorderLayout.
In the west part of it is a panel with a grid layout of 3 x 2 buttons.
In the center part is a panel.
I'd like to add a third panel as shown. How can I do this?
In the center part is a Panel
That panel might also have a BorderLayout, put the two combo boxes in a panel in the PAGE_START of it, and the 3rd panel in the CENTER.
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
public class ThreePanelLayout {
private JComponent ui = null;
private String[] buttonNames = {"Time", "Price", "Route", "Sort", "Admin", "End"};
private String[][] comboFirstNames = {{"Departing Stop"}, {"Final Stop"}};
ThreePanelLayout() {
initUI();
}
public void initUI() {
if (ui!=null) return;
// I always use this 'ui' panel as a content pane that contains
// everything else..
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
// now to create the 3 panels of the '3 panel layout'.
JPanel panel1 = new JPanel(new BorderLayout());
panel1.setBackground(Color.RED);
panel1.setBorder(new TitledBorder("Choose Option"));
JPanel panel2 = new JPanel(new BorderLayout());
panel2.setBackground(Color.GREEN);
panel2.setBorder(new TitledBorder("Choose Two Stops"));
JPanel panel3 = new JPanel(new BorderLayout());
panel3.setBackground(Color.ORANGE);
panel3.setBorder(new TitledBorder("Third Panel Here"));
// add the buttons to 1st panel
panel1.add(addButtonsToPanel(buttonNames), BorderLayout.LINE_START);
// add the combos to the top of 2nd panel
panel2.add(addCombosToPanel(comboFirstNames), BorderLayout.PAGE_START);
// give the 3rd panel some size
panel3.add(new JLabel(new ImageIcon(new BufferedImage(400,200,BufferedImage.TYPE_INT_ARGB))));
// now assemble them all together
panel2.add(panel3, BorderLayout.CENTER);
panel1.add(panel2, BorderLayout.CENTER);
ui.add(panel1, BorderLayout.CENTER);
}
private JPanel addButtonsToPanel(String[] ids) {
JPanel p = new JPanel(new GridLayout(0, 2));
for (String id : ids) {
p.add(new JButton(id));
}
return p;
}
private JPanel addCombosToPanel(String[][] ids) {
JPanel p = new JPanel(new FlowLayout());
for (String[] id : ids) {
p.add(new JComboBox<String>(id));
}
return p;
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ThreePanelLayout o = new ThreePanelLayout();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
When i add an scroll to my textarea then the textarea isnt visible in the application:
Code:
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
final JTextArea textArea = new JTextArea();
textArea.setBounds(15, 112, 689, 310);
JScrollPane scrollPane = new JScrollPane( textArea );
panel.add( scrollPane );
Here is how to do what the code seems to be trying to do, using sizing hints for the text area, layouts and padding. Adjust numbers to need.
See further comments in code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ScrollingTextArea {
private JComponent panel = null;
ScrollingTextArea() {
initUI();
}
public void initUI() {
if (panel != null) {
return;
}
panel = new JPanel(new BorderLayout());
// adjust numbers to need
panel.setBorder(new EmptyBorder(32, 15, 32, 15));
// adjust rows & cols to need
final JTextArea textArea = new JTextArea(20,80);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane);
}
public JComponent getUI() {
return panel;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ScrollingTextArea o = new ScrollingTextArea();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
So what I am trying to do is create this:
I am using a gridbag layout and here is what I have so far:
public class board {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel leftTop = new JPanel();
leftTop.setPreferredSize(new Dimension(251,300));
leftTop.setBackground(Color.black);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(leftTop, c);
JPanel middleTop = new JPanel();
middleTop.setPreferredSize(new Dimension(251,200));
middleTop.setBackground(Color.green);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
pane.add(middleTop, c);
JPanel rightTop = new JPanel();
rightTop.setPreferredSize(new Dimension(251,600));
rightTop.setBackground(Color.blue);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
pane.add(rightTop, c);
JPanel leftBottom = new JPanel();
leftBottom.setPreferredSize(new Dimension(251,300));
leftBottom.setBackground(Color.red);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
pane.add(leftBottom, c);
JPanel middleBottom = new JPanel();
middleBottom.setPreferredSize(new Dimension(251,400));
middleBottom.setBackground(Color.yellow);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
pane.add(middleBottom, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
It creates something like:
How would I push up the panels so they are touching each other like in my first picture. I looked through the GridBagConstraints but I could not find anything that looked like it would work. Thanks!
Instead of trying to solve the complete layout problem with one layout manager, it's often simpler to nest layouts. For example, your example code could be modified to use a horizontal grid layout (to keep the columns equal width - I don't actually know if you want to force that. If not, then FlowLayout or BoxLayout would be better), and the columns use a BoxLayout each:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class board {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridLayout(1, 0));
JPanel left = new JPanel();
pane.add(left);
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
JPanel leftTop = new JPanel();
leftTop.setPreferredSize(new Dimension(125, 150));
leftTop.setBackground(Color.black);
left.add(leftTop);
JPanel leftBottom = new JPanel();
leftBottom.setPreferredSize(new Dimension(125, 150));
leftBottom.setBackground(Color.red);
left.add(leftBottom);
JPanel middle = new JPanel();
pane.add(middle);
middle.setLayout(new BoxLayout(middle, BoxLayout.Y_AXIS));
JPanel middleTop = new JPanel();
middleTop.setPreferredSize(new Dimension(125, 100));
middleTop.setBackground(Color.green);
middle.add(middleTop);
JPanel middleBottom = new JPanel();
middleBottom.setPreferredSize(new Dimension(125, 200));
middleBottom.setBackground(Color.yellow);
middle.add(middleBottom);
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(125, 300));
right.setBackground(Color.blue);
pane.add(right);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
Results in:
(I modified the preferred sizes a bit to make the image smaller. As a further note it's usually better to override getPreferredSize() rather than use setPreferredSize(); setPreferredSize() is convenient for the quick example though)
As practice, I've been trying to make a simple tic tac toe game to see how layouts work in java. Now that I have the base code, with all of the rules and variable checks, I cannot find out how to get the buttons to line up the way I want. I wanted to make a 3x3 grid of buttons, but whenever I try a tutorial online or find someone with a similar problem, it always leads to the buttons not showing up at all. The following code gets the buttons on the screen, but doesn't arrange them.
package game;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame{
//JPanel
JPanel pnlMainBoard = new JPanel();
//Buttons
JButton btnTest = new JButton("Test");
JButton btnAI = new JButton("A1");
JButton btnBI = new JButton("B1");
JButton btnCI = new JButton("C1");
JButton btnAII = new JButton("A2");
JButton btnBII = new JButton("B2");
JButton btnCII = new JButton("C2");
JButton btnAIII = new JButton("A3");
JButton btnBIII = new JButton("B3");
JButton btnCIII = new JButton("C3");
public Main(){
//Layout
//pnlMainBoard.setLayout(null);
//Game set bounds
btnTest.setBounds(60,400,220,30);
//JPanel bounds
pnlMainBoard.setBounds(800,800,200,100);
//Add buttons to frame
pnlMainBoard.add(btnTest);
pnlMainBoard.add(btnAI);
pnlMainBoard.add(btnBI);
pnlMainBoard.add(btnCI);
pnlMainBoard.add(btnAII);
pnlMainBoard.add(btnBII);
pnlMainBoard.add(btnCII);
pnlMainBoard.add(btnAIII);
pnlMainBoard.add(btnBIII);
pnlMainBoard.add(btnCIII);
add(pnlMainBoard);
//JFrame Properties
setSize(400,400);
setTitle("Ultimate Tic Tac Toe");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
So far this is the only code I've created that successfully puts the buttons on the screen, and when I change it the buttons disappear. How do I make it so I can set the location of the buttons where I want them on the window?
For a 3x3 grid check out the swing grid layout that you can set the JPanel to use like this:
GridLayout grid = new GridLayout(3,3);
JPanel.setLayout(grid);
Where jpanel is the name of your jpanel in your program...
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ThreeByThreeWithButtonLayout {
private JComponent ui = null;
ThreeByThreeWithButtonLayout() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
int gap = 10;
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
JButton testButton = new JButton("Test");
JPanel buttonConstrain = new JPanel(
new FlowLayout(FlowLayout.CENTER, gap, gap));
buttonConstrain.add(testButton);
ui.add(buttonConstrain, BorderLayout.PAGE_START);
JPanel gridPanel = new JPanel(new GridLayout(0, 3, 5, 5));
gridPanel.setBorder(new EmptyBorder(gap, gap, gap, gap));
ui.add(gridPanel, BorderLayout.CENTER);
String[] buttonRows = {"A", "B", "C"};
for (int ii = 1; ii < 4; ii++) {
for (String buttonRow : buttonRows) {
JButton b = new JButton(buttonRow + ii);
b.setFont(b.getFont().deriveFont(32f));
gridPanel.add(b);
}
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ThreeByThreeWithButtonLayout o = new ThreeByThreeWithButtonLayout();
JFrame f = new JFrame("3x3 + Button");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
GridLayout makes some rows and columns to your frame/panel like this :
GridLayout grid = new GridLayout(3,3);
Jpanel.setLayout(grid);
I am doing a little test of a demo Swing GUI. In this demo, the JFrame is composed of 3 "master" JPanels. If you will, the first (jp1) is composed of JLabels, and the other two are composed of several other JPanels. I am using MigLayout.
Here is my sample code:
// All the jPanels
JFrame frame = new JFrame();
frame.setLayout(new MigLayout());
JPanel jp1 = new JPanel();
jp1.setLayout(new MigLayout());
jp1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp2 = new JPanel();
jp2.setLayout(new MigLayout());
jp2.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp3 = new JPanel();
jp3.setLayout(new MigLayout());
jp3.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp4 = new JPanel();
jp4.setLayout(new MigLayout());
jp4.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp5 = new JPanel();
jp5.setLayout(new MigLayout());
jp5.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp6 = new JPanel();
jp6.setLayout(new MigLayout());
jp6.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel jp7 = new JPanel();
jp7.setLayout(new MigLayout());
jp7.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel bigPanel1 = new JPanel();
bigPanel1.setLayout(new MigLayout());
bigPanel1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
JPanel bigPanel2 = new JPanel();
bigPanel2.setLayout(new MigLayout());
bigPanel2.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
//All the labels to be added to JPanel jp1
JLabel label1 = new JLabel();
label1.setText("LABEL1");
JLabel label2 = new JLabel();
label2.setText("LABEL2");
JLabel label3 = new JLabel();
label3.setText("LABEL3");
JLabel label4 = new JLabel();
label4.setText("LABEL4");
jp1.add(label1);
jp1.add(label2);
jp1.add(label3);
jp1.add(label4,"wrap");
bigPanel1.add(jp2);
bigPanel1.add(jp6);
bigPanel1.add(jp3,"grow,wrap");
bigPanel2.add(jp4);
bigPanel2.add(jp7);
bigPanel2.add(jp5,"grow,wrap");
frame.getContentPane().add(jp1,"dock north, wrap");
frame.getContentPane().add(bigPanel1,"span,grow,wrap");
frame.getContentPane().add(bigPanel2,"span,grow,wrap");
frame.pack();
frame.setVisible(true);
Which results in this output:GUI OUTPUT
What I want to achieve is being able to add labels into the 1st JPanel (jp1) without messing with the remainder JPanels width.
Additionally, I want to make the several JPanels inside a bigPanel to occupy its full width, as well as in jp2,jp6 and jp3 to fill bigPanel1.
How should I do this? Thanks in advance.
I have never used MigLayout, and personally dont see the reason if it can be done using default java LayoutManager.
Okay so I used a combination FlowLayout and GridBagLayout to achieve this, along with gc.fill=GridBagConstraints.NONE and gc.anchor=GridBagConstraints.WEST for those panels which we dont want to fill the contentpane width, also updated as per your comment to stop the JPanel/JFrame from growing larger than the given max width when more JLabels are added this was done using a JScrollPane:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class Test {
public Test() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new GridBagLayout());
final JPanel labelPanel = new JPanel();
labelPanel.setBorder(new LineBorder(Color.black));
for (int i = 0; i < 5; i++) {
labelPanel.add(new JLabel("Label" + (i + 1)));
}
final int maxWidth = 200;
final JScrollPane jsp = new JScrollPane(labelPanel) {
#Override
public Dimension getPreferredSize() {
//we set the height by checking if we exceeed the wanted ith thus a scrollbar will appear an we must incoprate that or labels wont be shpwn nicely
return new Dimension(maxWidth, labelPanel.getPreferredSize().width < maxWidth ? (labelPanel.getPreferredSize().height + 5) : ((labelPanel.getPreferredSize().height + getHorizontalScrollBar().getPreferredSize().height) + 5));
}
};
JPanel otherPanel = new JPanel();
otherPanel.add(new JLabel("label"));
otherPanel.setBorder(new LineBorder(Color.black));
JPanel otherPanel2 = new JPanel();
otherPanel2.add(new JLabel("label 1"));
otherPanel2.add(new JLabel("label 2"));
otherPanel2.setBorder(new LineBorder(Color.black));
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 1.0;
gc.weighty = 1.0;
gc.gridx = 0;
gc.gridy = 0;
frame.add(jsp, gc);
gc.fill = GridBagConstraints.NONE;
gc.anchor = GridBagConstraints.WEST;
gc.gridy = 1;
frame.add(otherPanel, gc);
gc.anchor = GridBagConstraints.WEST;
gc.gridy = 2;
frame.add(otherPanel2, gc);
frame.pack();
frame.setVisible(true);
frame.revalidate();
frame.repaint();
}
public static void main(String[] args) {
//Create Swing components on EDT
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test();
}
});
}
}
I have use BorderLayout and FlowLayout to manage the layouts. The frame has two JPanel's and one JPanel in it has two more JPanel's. All the internal panels use FlowLayout to align the JLabels. To arrange these panels on the JFrame I have used BorderLayout.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class LayoutTest {
public LayoutTest() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new GridBagLayout());
JPanel motherPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new BorderLayout());
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
motherPanel.add(topPanel, BorderLayout.NORTH);
motherPanel.add(bottomPanel, BorderLayout.CENTER);
JPanel topUpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel topBottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
topUpperPanel.setBorder(new LineBorder(Color.BLACK));
topBottomPanel.setBorder(new LineBorder(Color.BLACK));
bottomPanel.setBorder(new LineBorder(Color.BLACK));
topPanel.add(topUpperPanel, BorderLayout.PAGE_START);
topPanel.add(topBottomPanel, BorderLayout.CENTER);
for(int i = 0; i < 3; i++) {
JLabel label = new JLabel("Label-" + String.valueOf(i));
label.setBorder(new LineBorder(Color.BLACK));
topUpperPanel.add(label);
}
for(int i = 0; i < 2; i++) {
JLabel label = new JLabel("Label-" + String.valueOf(i));
label.setBorder(new LineBorder(Color.BLACK));
topBottomPanel.add(label);
}
for(int i = 0; i < 5; i++) {
JLabel label = new JLabel("Label-" + String.valueOf(i));
label.setBorder(new LineBorder(Color.BLACK));
bottomPanel.add(label);
}
frame.add(motherPanel);
frame.setTitle("Layout Manager");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutTest();
}
});
}
}
P.S: I would suggest you to separate the panels such that there will be "whithout no interference with remaining JPanels."