Swing的FlowLayout示例

FlowLayout类以从左到右的流程排列组件。

示例

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingTester {
   public static void main(String[] args) {
      createWindow();
   }

   private static void createWindow() {    
      JFrame frame = new JFrame("Swing的FlowLayout示例");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      createUI(frame);
      frame.setSize(560, 200);      
      frame.setLocationRelativeTo(null);  
      frame.setVisible(true);
   }

   private static void createUI(final JFrame frame){  
      JPanel panel = new JPanel();
      FlowLayout layout = new FlowLayout();

      panel.setLayout(layout);        
      panel.add(new JButton("按钮 1"));
      panel.add(new JButton("按钮 2")); 
      panel.add(new JButton("按钮 3")); 
      panel.add(new JButton("按钮 4")); 
      panel.add(new JButton("按钮 5"));    
      frame.getContentPane().add(panel, BorderLayout.CENTER);    
   }
}

执行上面示例代码,得到以下结果:

FlowLayout示例


上一篇: Swing布局示例 下一篇: Swing菜单示例