Java Tutorial/Swing/BorderLayout — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:31, 31 мая 2010

A BorderLayout divides the space into five regions: North, West, South, East and Centre.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class BorderExample {
  public static void main(String[] args) {
    JPanel panel = new JPanel(new BorderLayout());
    JPanel top = new JPanel();
    top.setBackground(Color.gray);
    top.setPreferredSize(new Dimension(250, 150));
    panel.add(top);
    panel.setBorder(new EmptyBorder(new Insets(10, 20, 30, 40)));
    JFrame f = new JFrame();
    f.add(panel);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}





A typical usage of a border layout manager.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class BorderLayoutExample {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");
    menubar.add(file);
    f.setJMenuBar(menubar);
    JToolBar toolbar = new JToolBar();
    toolbar.setFloatable(false);
    JButton bexit = new JButton(new ImageIcon("exit.png"));
    bexit.setBorder(new EmptyBorder(0, 0, 0, 0));
    toolbar.add(bexit);
    f.add(toolbar, BorderLayout.NORTH);
    JToolBar vertical = new JToolBar(JToolBar.VERTICAL);
    vertical.setFloatable(false);
    vertical.setMargin(new Insets(10, 5, 5, 5));
    JButton selectb = new JButton(new ImageIcon("a.png"));
    selectb.setBorder(new EmptyBorder(3, 0, 3, 0));
    JButton freehandb = new JButton(new ImageIcon("b.png"));
    freehandb.setBorder(new EmptyBorder(3, 0, 3, 0));
    JButton shapeedb = new JButton(new ImageIcon("c.png"));
    shapeedb.setBorder(new EmptyBorder(3, 0, 3, 0));
    vertical.add(selectb);
    vertical.add(freehandb);
    vertical.add(shapeedb);
    f.add(vertical, BorderLayout.WEST);
    f.add(new JTextArea(), BorderLayout.CENTER);
    JLabel statusbar = new JLabel(" Statusbar");
    statusbar.setPreferredSize(new Dimension(-1, 22));
    statusbar.setBorder(LineBorder.createGrayLineBorder());
    f.add(statusbar, BorderLayout.SOUTH);
    f.setSize(350, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}





Combining BorderLayout and GridLayout Managers

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class CombinedLayoutManager extends JFrame {
  public static void main(String[] args) {
    CombinedLayoutManager e = new CombinedLayoutManager();
    e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    e.setSize(400, 300);
    e.setVisible(true);
  }
  public CombinedLayoutManager() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    pane.add(getHeader(), BorderLayout.NORTH);
    pane.add(getTextArea(), BorderLayout.CENTER);
    pane.add(getButtonPanel(), BorderLayout.SOUTH);
  }
  protected JComponent getHeader() {
    JLabel label = new JLabel("Embedded Layout Manager Test", JLabel.CENTER);
    label.setFont(new Font("Courier", Font.BOLD, 24));
    return label;
  }
  protected JComponent getTextArea() {
    return new JTextArea(10, 10);
  }
  protected JComponent getButtonPanel() {
    JPanel inner = new JPanel();
    inner.setLayout(new GridLayout(1, 2, 10, 0));
    inner.add(new JButton("Ok"));
    inner.add(new JButton("Cancel"));
    return inner;
  }
}





Place multiple components into one of the regions of a BorderLayout-managed container

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainFrameBorderLayout {
  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel outerPanel = new JPanel(new BorderLayout());
    JPanel topPanel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name:");
    JTextField text = new JTextField();
    topPanel.add(label, BorderLayout.BEFORE_LINE_BEGINS);
    topPanel.add(text, BorderLayout.CENTER);
    outerPanel.add(topPanel, BorderLayout.BEFORE_FIRST_LINE);
    frame.add(outerPanel);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Using a BorderLayout Manager

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.EtchedBorder;
public class TryBorderLayout {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Border Layout");
    aWindow.setBounds(30, 30, 300, 300); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BorderLayout border = new BorderLayout(); // Create a layout manager
    Container content = aWindow.getContentPane(); // Get the content pane
    content.setLayout(border); // Set the container layout mgr
    EtchedBorder edge = new EtchedBorder(EtchedBorder.RAISED); // Button border
    // Now add five JButton components and set their borders
    JButton button;
    content.add(button = new JButton("EAST"), BorderLayout.EAST);
    button.setBorder(edge);
    content.add(button = new JButton("WEST"), BorderLayout.WEST);
    button.setBorder(edge);
    content.add(button = new JButton("NORTH"), BorderLayout.NORTH);
    button.setBorder(edge);
    content.add(button = new JButton("SOUTH"), BorderLayout.SOUTH);
    button.setBorder(edge);
    content.add(button = new JButton("CENTER"), BorderLayout.CENTER);
    button.setBorder(edge);
    aWindow.setVisible(true); // Display the window
  }
}





What is the BorderLayout

BorderLayout is the default layout manager for the content pane of a JFrame, JWindow, JDialog, JInternalFrame, and JApplet.

  1. Place components against any of the four borders of the container and in the center.
  2. The component in the center fills the available space.
  3. You do not need specify all five areas of the container.
  4. The component in the north region takes up the entire width of the container along its top.
  5. South does the same along the bottom.
  6. The heights of north and south will be the preferred heights of the added component.
  7. The east and west areas are given the widths of the component each contains, where the height is whatever is left in the container after satisfying north"s and south"s height requirements.
  8. Any remaining space is given to the component in the center region.
  9. Constants used to specify areas: CENTER, EAST, NORTH, SOUTH, WEST

Its constructors:



public BorderLayout()
public BorderLayout(int hgap, int vgap)