当选择使用JPanel 和顶层容器的content pane 时,需要考虑布局管理
JPanel 缺省是初始化一个FlowLayout,而content pane 缺省是初始化一个BorderLayout
下面将分别介绍几种最常用的布局管理器:FlowLayout、BorderLayout、BoxLayout、CardLayout、GridLayout 和GridBagLayout
代码演示 每一个布局管理器都会有一个代码演示,xxxLayoutDemo
java(见附件)
这些文件主要有三个方法组成: addComponentsToPane()提供布局逻辑(依据不同的布局管理器和UI 内容)
Java 代码 public static void addComponentsToPane(Container pane) {
} /** *createAndShowGUI()实例化一个JFrame,通过它的ContentPane 加载布局逻辑内容
*/ private static void createAndShowGUI() { // Create and set up the window
JFrame frame = new JFrame("FlowLayoutDemo"); frame
setDefaultCloseOperation(JFrame
EXIT_ON_CLOSE); // Set up the content pane
addComponentsToPane(frame
getContentPane()); // Display the window
frame
pack(); frame
setVisible(true); } //main()程序入口,单独起一个