Border Layout
A BorderLayout places components in up to five areas: top, bottom, left, right, and center.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class App extends JFrame {
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 300, 300);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setResizable(false);
frame.setVisible(true);
}
public App() {
initMenu();
}
public void initMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem menuItem1= new JMenuItem("Page1");
JMenuItem menuItem2 = new JMenuItem("Page2");
menuBar.add(menuFile);
menuFile.add(menuItem1);
menuFile.add(menuItem2);
menuBar.setPreferredSize(new Dimension(300, 25));
setJMenuBar(menuBar);
menuItem1.addActionListener(new MenuAction(new Action1()));
}
private class MenuAction implements ActionListener {
private JPanel panel;
private MenuAction(JPanel panel) {
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) {
changePanel(panel);
}
}
public void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel);
getContentPane().doLayout();
validate();
repaint();
}
}
class Action1 extends JPanel { // Look Here
public Action1() {
JPanel top = new JPanel();
top.setPreferredSize(new Dimension(300, 50));
top.setBackground(Color.decode("#CCCCCC"));
JPanel left = new JPanel();
left.setPreferredSize(new Dimension(50, 125));
left.setBackground(Color.decode("#DDDDDD"));
JPanel center = new JPanel();
center.setPreferredSize(new Dimension(175, 125));
center.setBackground(Color.decode("#AAAAAA"));
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(50, 125));
right.setBackground(Color.decode("#DDDDDD"));
JPanel bottom = new JPanel();
bottom.setPreferredSize(new Dimension(300, 50));
bottom.setBackground(Color.decode("#CCCCCC"));
add(top, BorderLayout.PAGE_START);
add(left, BorderLayout.LINE_START);
add(center, BorderLayout.CENTER);
add(right, BorderLayout.LINE_END);
add(bottom, BorderLayout.PAGE_END);
}
}
Box Layout
The BoxLayout class puts components in a single row or column. It lets you align components.
import javax.swing.*;
import java.awt.*;
public class App extends JFrame {
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 300, 300);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public App() {
JPanel top = new JPanel();
top.setPreferredSize(new Dimension(300, 46));
top.setBorder(BorderFactory.createTitledBorder("top"));
JPanel left = new JPanel();
left.setPreferredSize(new Dimension(50, 190));
left.setBorder(BorderFactory.createTitledBorder("left"));
JPanel center = new JPanel();
center.setPreferredSize(new Dimension(230, 190));
center.setBorder(BorderFactory.createTitledBorder("center"));
add(top, BorderLayout.PAGE_START);
add(left, BorderLayout.LINE_START);
add(center, BorderLayout.CENTER);
// Look Here
top.setLayout(new BoxLayout(top, BoxLayout.LINE_AXIS));
JPanel t1 = new JPanel();
t1.setMaximumSize(new Dimension(50, 5));
t1.setBackground(Color.yellow);
t1.setAlignmentY(BOTTOM_ALIGNMENT); // align bottom
top.add(t1);
JPanel t2 = new JPanel();
t2.setMaximumSize(new Dimension(70, 10));
t2.setBackground(Color.red);
t2.setAlignmentY(BOTTOM_ALIGNMENT);
top.add(t2);
JPanel t3 = new JPanel();
t3.setMaximumSize(new Dimension(90, 20));
t3.setBackground(Color.blue);
t3.setAlignmentY(BOTTOM_ALIGNMENT);
top.add(t3);
// Look Here
center.setLayout(new BoxLayout(center, BoxLayout.PAGE_AXIS));
JPanel c1 = new JPanel();
c1.setMaximumSize(new Dimension(100, 50));
c1.setBackground(Color.yellow);
center.add(c1);
JPanel c2 = new JPanel();
c2.setMaximumSize(new Dimension(150, 50));
c2.setBackground(Color.red);
center.add(c2);
JPanel c3 = new JPanel();
c3.setMaximumSize(new Dimension(200, 50));
c3.setBackground(Color.blue);
center.add(c3);
}
}
Card Layout
You can use Card Layout to switch between panels.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class App extends JFrame implements ItemListener { // Look Here
private static JPanel cardsPanel = new JPanel(new CardLayout());
public static final String CARD1 = "Card 1";
public static final String CARD2 = "Card 2";
@Override
public void itemStateChanged(ItemEvent e) { // Look Here
CardLayout c = (CardLayout) cardsPanel.getLayout();
c.show(cardsPanel, (String) e.getItem());
}
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 300, 200);
frame.setLayout(new BorderLayout()); // default, can be ommited
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
// Combo Box
JPanel topPanel = new JPanel();
String items[] = {CARD1, CARD2};
JComboBox comboBox = new JComboBox(items);
topPanel.add(comboBox);
comboBox.addItemListener(frame);// Look Here
// Cards (panels)
JPanel panel1 = new JPanel();
panel1.setBackground(Color.red);
panel1.setPreferredSize(new Dimension(100, 100));
JPanel panel2 = new JPanel();
panel2.setBackground(Color.blue);
panel2.setPreferredSize(new Dimension(100, 100));
// Add panels to Card Layout
// * CARD1 & CARD2 are the index contrains
// * CARD1 & CARD2 must be the same as in ComboBox
cardsPanel.add(panel1, CARD1);
cardsPanel.add(panel2, CARD2); // Look Here
frame.getContentPane().add(topPanel, BorderLayout.PAGE_START);
frame.getContentPane().add(cardsPanel, BorderLayout.CENTER);
frame.setVisible(true);
}
}
Grid Layout
A GridLayout object places components in a grid of cells.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class App extends JFrame {
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 400, 200);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
//frame.setLayout(new BorderLayout());
frame.setVisible(true);
}
public App() {
JPanel main = new JPanel();
main.setBorder(new EmptyBorder(10, 30, 50, 100)); // Look Here
main.setLayout(new GridLayout(0, 3)); // any rows, 3 cols
main.add(new JTextField("col 1"));
main.add(new JTextField("col 2"));
main.add(new JTextField("col 3"));
main.add(new JButton("button 1"));
main.add(new JButton("button 2"));
main.add(new JButton("button 3"));
main.getComponent(1).setPreferredSize(new Dimension(100, 20));
add(main, BorderLayout.NORTH);
}
}
Group Layout
Groups are created via createSequentialGroup() and createParallelGroup methods. (1) LEADING means left (top) (2) TRAILING means right (bottom)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class App extends JFrame {
private static JPanel c1; // Look Here
private static JPanel c2;
private static JPanel c3;
private static JPanel c4;
private static GroupLayout layout;
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 300, 200);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public App() {
initMenu();
}
public void initMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu menuOpen = new JMenu("Open");
JMenuItem menuOpenItem1 = new JMenuItem("Horizontal");
JMenuItem menuOpenItem2 = new JMenuItem("Vertical");
JMenuItem menuOpenItem3 = new JMenuItem("Alignment");
JMenuItem menuOpenItem4 = new JMenuItem("Trailing");
menuBar.add(menuOpen);
menuOpen.add(menuOpenItem1);
menuOpen.add(menuOpenItem2);
menuOpen.add(menuOpenItem3);
menuOpen.add(menuOpenItem4);
setJMenuBar(menuBar);
menuOpenItem1.addActionListener(new MenuAction(new Action1()));
menuOpenItem2.addActionListener(new MenuAction(new Action2()));
menuOpenItem3.addActionListener(new MenuAction(new Action3()));
menuOpenItem4.addActionListener(new MenuAction(new Action4()));
}
private class MenuAction implements ActionListener {
private JPanel panel;
private MenuAction(JPanel panel) {
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) {
changePanel(panel);
}
}
public void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().doLayout();
validate();
repaint();
}
public void initLayoutAndElements(JPanel panel) {
// Look Here
layout = new GroupLayout(panel);
panel.setLayout(layout);
c1 = new JPanel();
c1.setMaximumSize(new Dimension(10, 10));
c1.setBackground(Color.red);
c2 = new JPanel();
c2.setMaximumSize(new Dimension(20, 20));
c2.setBackground(Color.yellow);
c3 = new JPanel();
c3.setMaximumSize(new Dimension(30, 30));
c3.setBackground(Color.black);
c4 = new JPanel();
c4.setMaximumSize(new Dimension(40, 40));
c4.setBackground(Color.blue);
}
class Action1 extends JPanel {
public Action1() {
initLayoutAndElements(this);
layout.setHorizontalGroup(
layout.createSequentialGroup() // Look Here
.addGap(150)
.addComponent(c1)
.addComponent(c2)
.addComponent(c3)
.addComponent(c4)
);
layout.setVerticalGroup(
layout.createParallelGroup()
.addComponent(c1)
.addComponent(c2)
.addComponent(c3)
.addComponent(c4)
);
}
}
class Action2 extends JPanel {
public Action2() {
initLayoutAndElements(this);
layout.setHorizontalGroup(
layout.createParallelGroup() // Look Here
.addComponent(c1)
.addComponent(c2)
.addComponent(c3)
.addComponent(c4)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGap(50)
.addComponent(c4)
.addComponent(c3)
.addComponent(c2)
.addComponent(c1)
);
}
}
class Action3 extends JPanel {
public Action3() {
initLayoutAndElements(this);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGap(150)
.addComponent(c1) // Look Here
.addComponent(c2)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(c3)
.addComponent(c4)
)
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(c1)
.addComponent(c2)
.addGroup(layout.createSequentialGroup()
.addComponent(c3)
.addComponent(c4)
)
);
}
}
class Action4 extends JPanel {
public Action4() {
initLayoutAndElements(this);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGap(150)
.addComponent(c1)
.addComponent(c2)
.addComponent(c3)
.addComponent(c4) // Look Here
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(c1)
.addComponent(c2)
.addComponent(c3)
.addComponent(c4)
);
}
}
}
Last update: 496 days ago