Java GUI 图形用户界面

前言

1、图形界面

  • GUI(Graphic User Interface)图形用户界面。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import javax.swing.JButton;
    import javax.swing.JFrame;

    JFrame frame = new JFrame("LoL"); // 主窗体
    frame.setSize(400, 300); // 主窗体设置大小
    frame.setLocation(200, 200); // 主窗体设置位置
    frame.setLayout(null); // 主窗体中的组件设置为绝对定位

    JButton button = new JButton("一键秒对方基地挂"); // 按钮组件
    button.setBounds(50, 50, 280, 30); // 同时设置组件的大小和位置

    frame.add(button); // 把按钮加入到主窗体中
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗体的时候,退出程序
    frame.setVisible(true); // 让窗体变得可见

2、事件监听

关键字 简介
ActionListener 按钮监听
KeyListener 键盘监听
MouseListener 鼠标监听
Adapter 适配器
  • 按钮监听

    • 当按钮被点击时,就会触发 ActionEvent 事件,actionPerformed 方法就会被执行。

      1
      2
      3
      4
      5
      6
      // 给按钮 增加 监听
      button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      lable.setVisible(false);
      }
      });
  • 键盘监听

    • keyPressed 代表 键被按下
    • keyReleased 代表 键被弹起
    • keyTyped 代表 一个按下弹起的组合动作
    • KeyEvent.getKeyCode() 可以获取当前点下了哪个键

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      frame.addKeyListener(new KeyListener() {

      public void keyReleased(KeyEvent e) { // 键被弹起

      if (e.getKeyCode() == 39) { // 39 代表按下了 “右键”
      label.setLocation(label.getX() + 10, label.getY()); // 图片向右移动 (y 坐标不变,x 坐标增加)
      }
      }

      public void keyPressed(KeyEvent e) { // 键被按下
      // TODO Auto-generated method stub
      }

      public void keyTyped(KeyEvent e) { // 一个按下弹起的组合动作

      }
      });
  • 鼠标监听

    • mouseReleased 鼠标释放
    • mousePressed 鼠标按下
    • mouseExited 鼠标退出
    • mouseEntered 鼠标进入
    • mouseClicked 鼠标点击

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      label.addMouseListener(new MouseListener() {

      public void mouseReleased(MouseEvent e) { // 释放鼠标
      // TODO Auto-generated method stub
      }

      public void mousePressed(MouseEvent e) { // 按下鼠标
      // TODO Auto-generated method stub
      }

      public void mouseExited(MouseEvent e) { // 鼠标退出
      // TODO Auto-generated method stub
      }

      public void mouseEntered(MouseEvent e) { // 鼠标进入
      Random random = new Random();
      int x = random.nextInt(frame.getWidth() - label.getWidth());
      int y = random.nextInt(frame.getHeight() - label.getHeight());
      label.setLocation(x, y);
      }

      public void mouseClicked(MouseEvent e) { // 按下释放组合动作为点击鼠标
      // TODO Auto-generated method stub
      }
      });
  • 鼠标监听适配器

    • 一般说来在写监听器的时候,会实现 MouseListener。
    • 但是 MouseListener 里面有很多方法实际上都没有用到,比如 mouseReleased ,mousePressed,mouseExited 等等。
    • 这个时候就可以使用 鼠标监听适配器,MouseAdapter 只需要重写必要的方法即可。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      label.addMouseListener(new MouseAdapter() {

      public void mouseEntered(MouseEvent e) {
      Random random = new Random();
      int x = random.nextInt(frame.getWidth() - label.getWidth());
      int y = random.nextInt(frame.getHeight() - label.getHeight());
      label.setLocation(x, y);
      }
      });

3、布局器

  • 布局器是用在容器上的。用来决定容器上的组件摆放的位置和大小。

  • 绝对定位就是指不使用布局器,组件的位置和大小需要单独指定。

  • FlowLayout 顺序布局器

    • 设置布局器为 FlowLayout
    • 容器上的组件水平摆放
    • 加入到容器即可,无需单独指定大小和位置
  • BorderLayout

    • 设置布局器为 BorderLayout
    • 容器上的组件按照上北 下南 左西 右东 中的顺序摆放
  • GridLayout 网格布局器

    • GridLayout
  • setPreferredSize

    • 即便 使用 布局器,也可以 通过 setPreferredSize,向布局器建议该组件显示的大小.
    • 只对部分布局器起作用,比如 FlowLayout 可以起作用。比如 GridLayout 就不起作用,因为网格布局器必须对齐。
  • CardLayout

4、组件

  • Java 的图形界面下有两组控件,一组是 awt,一组是 swing

  • 一般都是使用 swing

关键字 简介
JLabel 标签
ImageIcon setIcon 使用 JLabel 显示图片
JButton 按钮
JCheckBox 复选框
JRadioButton 单选框
ButtonGroup 按钮组
JComboBox 下拉框
JOptionPane 对话框
JTextField 文本框
JPasswordField 密码框
JTextArea 文本域
JProgressBar 进度条
JFileChooser 文件选择器

5、容器

  • 容器是用来存放按钮,输入框等组件的。

  • 窗体型容器有两个

    • JFrame
    • JDialog
  • JFrame

    • 是最常用的窗体型容器,默认情况下,在右上角有最大化最小化按钮。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      import javax.swing.JButton;
      import javax.swing.JFrame;

      JFrame frame = new JFrame("LoL"); // 普通的窗体,带最大和最小化按钮
      frame.setSize(400, 300);
      frame.setLocation(200, 200);
      frame.setLayout(null);

      JButton button = new JButton("一键秒对方基地挂");
      button.setBounds(50, 50, 280, 30);

      frame.add(button);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
  • JDialog

    • 也是窗体型容器,右上角没有最大和最小化按钮。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      import javax.swing.JButton;
      import javax.swing.JFrame;

      JDialog dialog = new JDialog(); // 普通的窗体,带最大和最小化按钮,而对话框却不带
      dialog.setTitle("LOL");
      dialog.setSize(400, 300);
      dialog.setLocation(200, 200);
      dialog.setLayout(null);

      JButton button = new JButton("一键秒对方基地挂");
      button.setBounds(50, 50, 280, 30);

      dialog.add(button);
      dialog.setVisible(true);
  • 模态 JDialog

    • 当一个对话框被设置为模态的时候,其背后的父窗体,是不能被激活的,除非该对话框被关闭。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JFrame;

      JFrame frame = new JFrame("外部窗体");
      frame.setSize(800, 600);
      frame.setLocation(100, 100);

      JDialog dialog = new JDialog(frame); // 根据外部窗体实例化 JDialog
      dialog.setModal(true); // 设置为模态

      dialog.setTitle("模态的对话框");
      dialog.setSize(400, 300);
      dialog.setLocation(200, 200);
      dialog.setLayout(null);

      JButton button = new JButton("一键秒对方基地挂");
      button.setBounds(50, 50, 280, 30);
      dialog.add(button);

      frame.setVisible(true);
      dialog.setVisible(true);
  • 窗体大小不可变化

    • 通过调用方法 setResizable(false); 做到窗体大小不可变化。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      import javax.swing.JButton;
      import javax.swing.JFrame;

      JFrame frame = new JFrame("LoL");
      frame.setSize(400, 300);
      frame.setLocation(200, 200);
      frame.setLayout(null);

      JButton button = new JButton("一键秒对方基地挂");
      button.setBounds(50, 50, 280, 30);
      frame.add(button);

      frame.setResizable(false); // 窗体大小不可变化
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);

6、面板

  • 面板JFrame 一样都是容器,不过面板一般用来充当中间容器,把组件放在面板上,然后再把面板放在窗体上。

    • JPanel:基本面板
    • ContentPane:JFrame 上一层面板
    • SplitPanel:分割面板
    • JScrollPanel:带滚动条的面板
    • TabbedPanel:标签栏面板
    • CardLayerout:卡片面板

7、菜单

  • GUI 的菜单分为

    • JMenuBar:菜单栏
    • JMenu:菜单
    • JMenuItem:菜单项

8、工具栏

  • 工具栏用于存放常用的按钮。

  • 当鼠标放在按钮上的时候会出现提示。

  • 默认情况下 工具栏可以通过鼠标拖动。

  • 可以禁止鼠标拖动功能。

9、表格

10、日期控件

  • Swing 没有自带的日期控件,需要第三方的类。

11、Swing 中的线程

12、皮肤

  • Java 提供了非常便捷的方式切换界面风格

  • 只需要提供一句代码就可以把所有的组件切换成不同的风格。

    1
    javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
文章目录
  1. 1. 前言
  2. 2. 1、图形界面
  3. 3. 2、事件监听
  4. 4. 3、布局器
  5. 5. 4、组件
  6. 6. 5、容器
  7. 7. 6、面板
  8. 8. 7、菜单
  9. 9. 8、工具栏
  10. 10. 9、表格
  11. 11. 10、日期控件
  12. 12. 11、Swing 中的线程
  13. 13. 12、皮肤
隐藏目录