有关swing布局管理的几个实例
1.BorderLayout:
BorderLayout只管里容器中的5个组件的排列方式,这五个组件的位置分别位于 东、南、西、北、中 方向。
import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; public class BorderLayoutDemo { public static void addComponentsToPane(Container pane) { JButton button = new JButton("[PAGE_START]"); pane.add(button, BorderLayout.PAGE_START); button = new JButton("[CENTER]"); button.setPreferredSize(new Dimension(200, 100)); pane.add(button, BorderLayout.CENTER); button = new JButton("[LINE_START]"); pane.add(button, BorderLayout.LINE_START); button = new JButton("Long Name Button [PAGE_END]"); pane.add(button, BorderLayout.PAGE_END); button = new JButton("[LINE_END]"); pane.add(button, BorderLayout.LINE_END); } private static void createAndShowGUI() { JFrame frame = new JFrame("BorderLayoutManage"); 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(); } }); } }2.BoxLayout:
BoxLayout 对容器中的组件进行同一方向上的平均排列,纵向或者横向。
import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class BoxLayoutDemo { public static void addComponentsToPane(Container pane) { JPanel xPanel = new JPanel(); xPanel.setLayout(new BoxLayout(xPanel, BoxLayout.X_AXIS)); addButtons(xPanel); JPanel yPanel = new JPanel(); yPanel.setLayout(new BoxLayout(yPanel, BoxLayout.Y_AXIS)); addButtons(yPanel); pane.add(yPanel, BorderLayout.PAGE_START); pane.add(xPanel, BorderLayout.PAGE_END); } private static void addAButton(String text, Container container) { JButton button = new JButton(text); button.setAlignmentX(Component.CENTER_ALIGNMENT); container.add(button); } private static void addButtons(Container container) { addAButton("Button 1", container); addAButton("Button 2", container); addAButton("Button 3", container); addAButton("Long Button 4", container); addAButton("Button 5", container); } private static void createAndShowGUI() { JFrame frame = new JFrame("BoxLayoutManage"); 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(); } }); } }3.CardLayout:
import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class CardLayoutDemo { public void addComponentToPane(Container pane) { final JPanel contentPanel = new JPanel(); JPanel controlPanel = new JPanel(); final CardLayout cardLayout=new CardLayout();; pane.setLayout(new BorderLayout()); pane.add(contentPanel, BorderLayout.CENTER); pane.add(controlPanel, BorderLayout.PAGE_END); controlPanel.setLayout(new FlowLayout()); JButton[] b = new JButton[10]; for (int i = 0; i < 10; i++) { b[i] = new JButton("No." + i); contentPanel.add(b[i]); } contentPanel.setLayout(cardLayout); JButton nextButton = new JButton("Next"); nextButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { cardLayout.next(contentPanel); }}); controlPanel.add(nextButton); } private static void createAndShowGUI() { JFrame frame = new JFrame("CardLayoutManage"); frame.setBounds(400, 200, 600, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CardLayoutDemo demo = new CardLayoutDemo(); demo.addComponentToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }4.FlowLayout:
FlowLayout 是一种比较简单的布局方式,它会将所有组件排列成一行,以组件的preferredSize显示,一般情况下,如果一行显示不了所有的组件,会自动换到下 一行显示。
import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; public class FlowLayoutDemo { public static void addComponentsToPane(Container pane) { pane.setLayout(new FlowLayout()); pane.add(new JButton("Button 1")); pane.add(new JButton("Button 2")); pane.add(new JButton("Button 3")); pane.add(new JButton("Long Button 4")); pane.add(new JButton("Button 5")); } private static void createAndShowGUI() { JFrame frame = new JFrame("FlowLayoutManage"); 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(); } }); } }5.GridBagLayout:
import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; public class GridBagLayoutDemo { public static void addComponentsToPane(Container pane) { JButton button; pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); button = new JButton("Button 1"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; pane.add(button, c); button = new JButton("Button 2"); button.setSize(100,100); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 0; pane.add(button, c); button = new JButton("Button 3"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 2; c.gridy = 0; pane.add(button, c); button = new JButton("Long-Named Button 4"); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 20; c.gridwidth = 3; c.gridx = 0; c.gridy = 1; pane.add(button, c); button = new JButton("Long-Named Button Long-Named Button 5"); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 0; c.weighty = 1.0; c.anchor = GridBagConstraints.PAGE_END; c.insets = new Insets(10, 0, 0, 0); c.gridx = 1; c.gridwidth = 3; c.gridy = 2; pane.add(button, c); } private static void createAndShowGUI() { JFrame frame = new JFrame("GridBagLayoutManage"); 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(); } }); } }6.GridLayout:
import java.awt.Container; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; public class GridLayoutDemo { public static void addComponentsToPane(Container pane) { JButton[] buttons = new JButton[9]; pane.setLayout(new GridLayout(3, 3)); for (int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(i + ""); pane.add(buttons[i]); } } private static void createAndShowGUI() { JFrame frame = new JFrame("GridLayoutManage"); frame.setBounds(80, 60, 600, 300); 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(); } }); } }