import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /** a GUI application playing with BorderLayout...! @author Sharon Tuttle @version 2021-10-05 */ public class BorderLayoutPlay { /** creates a simple frame with a panel that includes some buttons laid out using BorderLayout @param args not used here */ public static void main(String[] args) { EventQueue.invokeLater( () -> { BorderLayoutPlayFrame mainFrame = new BorderLayoutPlayFrame(); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setVisible(true); } ); } } /** A frame with a panel that includes several buttons laid out using BorderLayout */ class BorderLayoutPlayFrame extends JFrame { // data fields private static final int DEFAULT_WIDTH = 1000; private static final int DEFAULT_HEIGHT = 450; /** construct a BorderLayoutPlayFrame instance */ public BorderLayoutPlayFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); setTitle("A Little BorderLayout Play"); BorderLayoutPlayPanel bPanel = new BorderLayoutPlayPanel(); add(bPanel); } } /** A panel containing several elements and a little playing with BorderLayout */ class BorderLayoutPlayPanel extends JPanel { // data fields! private static final Font BIG_FONT = new Font("SanSerif", Font.PLAIN, 30); private static final Font HUGE_FONT = new Font("SanSerif", Font.PLAIN, 60); /** constructs a BorderLayoutPlayPanel instance */ public BorderLayoutPlayPanel() { // set the layout manager for this panel to a new BorderLayout // instance setLayout(new BorderLayout()); // putting a JLabel in the north region of this panel JLabel topLabel = new JLabel("BorderLayout North Label"); topLabel.setFont(HUGE_FONT); topLabel.setBorder(new EtchedBorder()); add(topLabel, BorderLayout.NORTH); // putting a JButton in the south region of this panel JButton southButton = new JButton("South button"); southButton.setFont(BIG_FONT); add(southButton, BorderLayout.SOUTH); // putting a JButton in the east region of this panel JButton eastButton = new JButton("East button"); eastButton.setFont(BIG_FONT); add(eastButton, BorderLayout.EAST); // putting a JButton in the west region of this panel JButton westButton = new JButton("West button"); westButton.setFont(HUGE_FONT); add(westButton, BorderLayout.WEST); // putting a JButton in the center region of this panel JButton centerButton = new JButton("Center button"); centerButton.setFont(BIG_FONT); add(centerButton, BorderLayout.CENTER); // be careful, if you add more than one component to a region, // you'll ONLY see the latest-added component: // (uncomment to see this) // //add(new JButton("Looky")); //add(new JButton("EEP"), BorderLayout.SOUTH); //add(new JButton("Huh")); } }