关于SWING的几种布局管理的实例

发布时间:2011-6-16浏览:

核心提示:关于SWING的几种布局管理的实例

去年项目组要做关于swing的项目,前期都是学习的阶段。自己就将swing的布局管理以及控件的使用都练习了一遍。

下面是有关swing布局管理的几个实例,希望对初学者有用。

BorderLayout:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
 * @author closewubq
 */

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();
   }
  });
 }
}

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;
/**
 * @author closewubq
 */
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();
   }
  });
 
 }
}

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;
/**
 * @author closewubq
 */
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();
            }
        });
    }
}

FlowLayout:

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
 * @author closewubq
 */
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();
   }
  });
 }
}

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;
/**
 * @author closewubq
 */
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();
   }
  });
 }
}

GridLayout:

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
 * @author closewubq
 */
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();
   }
  });
 }
}

 

 


前一篇:定做程序设计
后一篇:在Home版中安装IIS
分享到: