java-GUI-布局
2021/4/13 12:25:08
本文主要是介绍java-GUI-布局,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
GUI-布局
流式布局
流式布局:FlowLayout
public static void main(String[] args) { // 定义窗口 Frame frame = new Frame(); // 设置窗口大小,坐标 frame.setBounds(200, 200, 400, 400); // 设置窗口颜色 frame.setBackground(Color.BLACK); // 设置流式布局 FlowLayout flowLayout = new FlowLayout(); // 窗口设置流式布局 frame.setLayout(flowLayout); // 设置按钮 Button btn_1 = new Button("btn_1"); Button btn_2 = new Button("btn_2"); // 按钮放到布局中 frame.add(btn_1); frame.add(btn_2); // 关闭窗口 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // 终止程序 System.exit(0); } }); // 设置窗口显示 frame.setVisible(true); }
边框布局
边框布局:BorderLayout,有东西南北中几个方位
public static void main(String[] args) { // 定义窗口 Frame frame = new Frame(); // 设置窗口大小,坐标 frame.setBounds(200, 200, 400, 400); // 设置窗口颜色 frame.setBackground(Color.BLACK); // 设置边框布局 BorderLayout borderLayout = new BorderLayout(); // 窗口设置边框布局 frame.setLayout(borderLayout); // 设置按钮 Button btn_1 = new Button("EAST"); Button btn_2 = new Button("WEST"); Button btn_3 = new Button("NORTH"); Button btn_4 = new Button("SOUTH"); Button btn_5 = new Button("CENTER"); // 按钮放置边框布局中 frame.add(btn_1, BorderLayout.EAST); frame.add(btn_2, BorderLayout.WEST); frame.add(btn_3, BorderLayout.NORTH); frame.add(btn_4, BorderLayout.SOUTH); frame.add(btn_5, BorderLayout.CENTER); // 关闭窗口 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // 终止程序 System.exit(0); } }); // 设置窗口显示 frame.setVisible(true); }
表格布局
表格布局:GridLayout
public static void main(String[] args) { // 定义窗口 Frame frame = new Frame(); // 设置窗口大小,坐标 frame.setBounds(200, 200, 400, 400); // 设置窗口颜色 frame.setBackground(Color.BLACK); // 设置表格布局-设置几行几列 GridLayout layout = new GridLayout(2, 2); // 窗口设置为表格布局 frame.setLayout(layout); // 设置按钮 Button btn_1 = new Button("btn_1"); Button btn_2 = new Button("btn_2"); Button btn_3 = new Button("btn_3"); Button btn_4 = new Button("btn_4"); // 按钮放到布局中 frame.add(btn_1); frame.add(btn_2); frame.add(btn_3); frame.add(btn_4); // 关闭窗口 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // 终止程序 System.exit(0); } }); // 设置窗口显示 frame.setVisible(true); }
窗口使用多个布局
窗口使用多个布局要用到面板Panel
例子:
public static void main(String[] args) { // 定义窗口 Frame frame = new Frame(); // 设置窗口大小,坐标 frame.setBounds(200, 200, 400, 400); // 设置窗口颜色 frame.setBackground(Color.BLACK); // 设置窗口大体为两行一列 frame.setLayout(new GridLayout(2, 1)); // 放置两个面板 Panel panel_1 = new Panel(); Panel panel_2 = new Panel(); frame.add(panel_1); frame.add(panel_2); // 第一个面板布局为边框布局,两边为按钮,中间为面板 // 面板设置布局 panel_1.setLayout(new BorderLayout()); // 两边设置为按钮 panel_1.add(new Button(), BorderLayout.WEST); panel_1.add(new Button(), BorderLayout.EAST); // 中间为面板,面板布局为两行一列的表格 // 设置面板,放到中间位置 Panel panel_3 = new Panel(); panel_1.add(panel_3, BorderLayout.CENTER); // 设置面板布局为两行一列的表格布局 panel_3.setLayout(new GridLayout(2, 1)); panel_3.add(new Button()); panel_3.add(new Button()); // 第二个面板布局为边框布局,两边为按钮,中间为面板 // 面板设置布局 panel_2.setLayout(new BorderLayout()); // 两边设置为按钮 panel_2.add(new Button(), BorderLayout.EAST); panel_2.add(new Button(), BorderLayout.WEST); // 中间为面板,面板布局为两行两列的表格 // 设置面板,放到中间位置 Panel panel_4 = new Panel(); panel_2.add(panel_4, BorderLayout.CENTER); // 设置面板布局为两行两列的表格布局 panel_4.setLayout(new GridLayout(2, 2)); panel_4.add(new Button()); panel_4.add(new Button()); panel_4.add(new Button()); panel_4.add(new Button()); // 关闭窗口 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // 终止程序 System.exit(0); } }); // 设置窗口显示 frame.setVisible(true); // 设置窗口自适应 frame.pack(); }
这篇关于java-GUI-布局的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性
- 2024-05-29哪些无用敏捷指标正在破坏敏捷转型?
- 2024-05-29鸿蒙原生应用再新丁!新华社 入局鸿蒙
- 2024-05-29设计模式 之 迭代器模式(Iterator)