鱼C论坛

 找回密码
 立即注册
查看: 3627|回复: 0

[学习笔记] GUI-lesson2

[复制链接]
发表于 2021-8-25 17:12:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 愷龍 于 2021-9-19 15:47 编辑

Panel 面板:
Panel 必须存在与容器中显示 不能独立显示

Panel 面板和 Frame 窗口用法基本一致

通过 add 来进行添加一些组件

四种布局模式:

  1. frame.setLayout(LayoutManager mgr)  //参数为LayoutManage对象  LayouMannager是一个接口
复制代码

BorderLayout 类 东南西北中布局模式
  1. import java.awt.*;
  2. public class TestBorderLayout {
  3.     public static void main(String[] args) {
  4.         Frame frame = new Frame("东西南北中");
  5.         Button button1 = new Button("East");
  6.         Button button2 = new Button("west");
  7.         Button button3 = new Button("south");
  8.         Button button4 = new Button("north");
  9.         Button button5 = new Button("center");
  10.         //添加东西南北中效果
  11.         frame.add(button1,BorderLayout.EAST);
  12.         frame.add(button2,BorderLayout.WEST);
  13.         frame.add(button3,BorderLayout.SOUTH);
  14.         frame.add(button4,BorderLayout.NORTH);
  15.         frame.add(button5,BorderLayout.CENTER);
  16.         frame.setSize(526,526);
  17.         frame.setVisible(true);
  18.     }
  19. }
复制代码


                               
登录/注册后可看大图


FlowLayout 类 流式布局模式 从左到右 从上到下
  1. import java.awt.*;
  2. public class TestFlowLayout {
  3.     public static void main(String[] args) {
  4.         Frame frame = new Frame();
  5.         //设置按钮
  6.         Button button = new Button("yuyan1");
  7.         Button button1 = new Button("yuyan2");
  8.         Button button2 = new Button("yuyan3");
  9.         //设置为流式布局
  10.         frame.setLayout(new FlowLayout());    //默认居中
  11.        // frame.setLayout(new FlowLayout(FlowLayout.LEFT));  //左
  12.         //添加按钮
  13.         frame.add(button);
  14.         frame.add(button1);
  15.         frame.add(button2);
  16.         //显示
  17.         frame.setVisible(true);
  18.     }
  19. }
复制代码


                               
登录/注册后可看大图


GridLayout 类 栅格式布局模式 表格模式 几行几列
  1. import java.awt.*;

  2. public class TestGridLayout {
  3.     public static void main(String[] args) {
  4.         Frame frame = new Frame("表格布局");
  5.         Button button1 = new Button("1");
  6.         Button button2 = new Button("2");
  7.         Button button3 = new Button("3");
  8.         Button button4 = new Button("4");
  9.         Button button5 = new Button("5");
  10.         Button button6 = new Button("6");
  11.         //设置行列
  12.         frame.setLayout(new GridLayout(3,2));
  13.         frame.add(button1);
  14.         frame.add(button2);
  15.         frame.add(button3);
  16.         frame.add(button4);
  17.         frame.add(button5);
  18.         frame.add(button6);
  19.         //Java的函数,自动布局
  20.         frame.pack();
  21.         frame.setVisible(true);
  22.     }
  23. }
复制代码


                               
登录/注册后可看大图


这三个类都是 LayouManager 接口的实现类
  1. frame.setLayout(new FlowLayout());  //流式布局  组件呈流失分布 从左向右 从上到下
  2. frame.setLayout(new BorderLayout(int hgap, int vgap))  //东南西北中布局  组件呈特定方向排布  默认为中 center
  3. frame.setLayout(new GridLayout(int rows, int cols, int hgap, int vgap))  //将窗口分成几行几列的表单 每个组件占一格分布                    //hgap vgap 为容器内行 列 间隔   可以不填默认无间隔
复制代码

体用法:
  1. Button b=new Button("我是按钮组件");
  2. frame.add(b);                      //自动流式布局
  3. frame.add(b,BoderLayout.WEST);   //将按钮向西排列布局 BoderLayout类中已经设置好了布局位置的静态常量可以直接调用
  4.                                 //BoderLayout.EAST : 东   BoderLayout.CENTER  : 中
  5. frame.setLayout(new GridLayout(2,2))  //栅格布局 将窗口分为两行两列
  6. frame.add(b)    //按钮位置在窗口左上方 占窗口四分之一大小
复制代码

上面是三种布局模式

绝对定位布局:

根据组件设置的坐标 自动相对于窗口定位

设置坐标: 1. setLocation (int : x , int : y) 2. setBounds (int : x , int : y ,int : width , int : height)
  1. frame.setLayout(null)              //绝对定位
  2. Button b=new Button("按钮")
  3. b.setLocation(100,100);            //设置按钮在窗口内的位置坐标
  4. frame.add(b)                    //将按钮添加到窗口上  按钮在 x 为100  Y 为100的位置上
复制代码

面板:
  1. import java.awt.*;
  2. import java.awt.event.WindowAdapter;
  3. import java.awt.event.WindowEvent;
  4. import java.awt.event.WindowListener;
  5. public class TestPanel {
  6.     public static void main(String[] args) {
  7.         Frame frame = new Frame();
  8.         Panel panel = new Panel();
  9.         //设置布局
  10.         frame.setLayout(null);
  11.         //坐标
  12.         frame.setBounds(300, 300, 500, 500);
  13.         frame.setBackground(Color.GREEN);
  14.         //panel设置左边,相对于frame
  15.         panel.setBounds(50, 50, 400, 400);
  16.         panel.setBackground(Color.RED);
  17.         //frame.add(panel)
  18.         frame.add(panel);
  19.         frame.setVisible(true);
  20.         //监听事件,监听窗口关闭事件system.exit(0)
  21.         //适配器模式
  22.         frame.addWindowListener(new WindowAdapter(){
  23.             //窗口点击关闭时需要做的事情
  24.             public void windowClosing(WindowEvent e){
  25.                 //结束程序
  26.                 System.exit(0);
  27.             }
  28.         });
  29.     }
  30.    
  31. }
复制代码


                               
登录/注册后可看大图

一个小实践

  1. import java.awt.*;
  2. import java.awt.event.WindowAdapter;
  3. import java.awt.event.WindowEvent;
  4. public class Test {
  5.     public static void main(String[] args) {
  6.         Frame frame = new Frame("作业");
  7.         frame.setSize(526,526);
  8.         Panel panel = new Panel();
  9.         Panel panel1 = new Panel();
  10.         Panel panel2 = new Panel();
  11.         Panel panel3 = new Panel();
  12.         panel.setBounds(0,0,50,526);
  13.         panel1.setBounds(50,0,426,263);
  14.         panel2.setBounds(50,263,426,263);
  15.         panel3.setBounds(476,0,50,526);
  16.         //便于区分
  17.         panel.setBackground(new Color(58, 65, 219));
  18.         panel1.setBackground(new Color(21, 220, 151));
  19.         panel2.setBackground(new Color(224, 208, 63));
  20.         panel3.setBackground(new Color(231, 125, 98));
  21.         frame.add(panel);
  22.         frame.add(panel1);
  23.         frame.add(panel2);
  24.         frame.add(panel3);
  25.         Button button1 = new Button("1");
  26.         Button button2 = new Button("2");
  27.         Button button3 = new Button("3");
  28.         Button button4 = new Button("4");
  29.         Button button5 = new Button("5");
  30.         Button button6 = new Button("6");
  31.         Button button7 = new Button("7");
  32.         Button button8 = new Button("8");
  33.         Button button9 = new Button("9");
  34.         Button button10 = new Button("10");
  35.         panel.setLayout(new GridLayout(2,1));
  36.         panel1.setLayout(new GridLayout(2,1));
  37.         panel2.setLayout(new GridLayout(2,2));
  38.         panel3.setLayout(new GridLayout(2,1));
  39.         panel.add(button1);
  40.         panel.add(button2);
  41.         panel1.add(button3);
  42.         panel1.add(button4);
  43.         panel2.add(button5);
  44.         panel2.add(button6);
  45.         panel2.add(button7);
  46.         panel2.add(button8);
  47.         panel3.add(button9);
  48.         panel3.add(button10);
  49.         //监听事件,监听窗口关闭事件  System.exit(0)
  50.         //适配器模式:
  51.         frame.addWindowListener(new WindowAdapter() {
  52.             @Override
  53.             public void windowClosing(WindowEvent e) {
  54.                 //结束程序
  55.                 System.exit(0);
  56.             }
  57.         });
  58.         frame.pack();
  59.         //frame.setResizable(false);
  60.         frame.setVisible(true);
  61.     }
  62. }
复制代码


                               
登录/注册后可看大图

                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 08:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表