head first java ( 13 章 )

- 创建 GUI 四个步骤

  1. 创建window(JFrame)

     JFrame frame = new JFrame();

  2. 创建组件

     JButton button = new JButton(“click me”);

  3. 把组件加到 frame 上

     frame.getContentPane().add(BorderLayout.EAST, button);

  4. 显示出来

     frame.setSize(300, 300);

     frame.setVisible(true);

- 有 panel 的情况

  panel 将作为一个逻辑单位, 就组件先存放在它的上边, 然后再将 panel 添加到框架上

  JFrame frame = new JFrame();

  JPanel panel = new JPanel();

  panel.setBackground(Color.darkGray);

  frame.getContentPane().add(BorderLayout.EAST, panel);

  frame.setSize(200, 200);

  frame.setVisible(true);

- 三大布局管理器

  BorderLayout: 把背景分成5个部分, 每个部分只能放一个组件

  FlowLayout: 跟文书处理程序版式差不多(从左到右, 从上到下, 有需要时全换行)

  BoxLayout: 由上到下, 依次排列

  imageimageimage

  image

  先南本, 后东西, 最后中间

原文地址:https://www.cnblogs.com/moveofgod/p/3025543.html