【JAVA入门】Day17 - GUI

【JAVA入门】Day17 - GUI



        GUI 即图形化界面。

一、组件

        一个 Java 的图形化界面项目主要用到了下面几种组件。

  • Java 中最外层的窗体叫做 JFrame。
  • Java 中最上层的菜单叫做 JMenuBar。
  • Java 中管理文字和图片的容器叫做 JLabel。

【练习1】创建主界面1。

public class App {
    public static void main(String[] args) {
        //表示程序的启动入口
        //想要启动哪个界面,就创建它的对象
        new LoginJFrame();      //调用默认空参构造方法
    }
}
public class GameJFrame extends JFrame {
    //1.创建一个游戏的主界面
    //属性(宽 高)   行为 setSize setVisible
    //规定:GameJFrame 这个界面表示的是游戏的主界面
    public GameJFrame() {
        this.setSize(603, 680);
        this.setVisible(true);
    }
}
public class RegisterJFrame extends JFrame {
    //注册界面
    public RegisterJFrame() {
        this.setSize(488, 430);
        this.setVisible(true);
    }
}
public class LoginJFrame extends JFrame {
    //登录界面
    public LoginJFrame() {
        this.setSize(488, 430);
        this.setVisible(true);
    }
}

【练习2】设置 GameJFrame的相关方法。

 public GameJFrame() {
        //设置界面的宽高
        this.setSize(603, 680);
        //设置界面的标题
        this.setTitle("拼图单机版 v1.0");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置游戏的关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //让窗体显示出来,建议写在最后
        this.setVisible(true);
    }

【练习3】在 GameJFrame 窗体上方添加菜单。
在这里插入图片描述

 public GameJFrame() {
        //设置界面的宽高
        this.setSize(603, 680);
        //设置界面的标题
        this.setTitle("拼图单机版 v1.0");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置游戏的关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        //初始化菜单
        //创建整个的菜单对象
        JMenuBar jMenuBar = new JMenuBar();

        //创建菜单上的两个选项的对象(功能   关于我们)
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");

        //创建选项下面的条目对象
        JMenuItem replayItem = new JMenuItem("重新游戏");
        JMenuItem reloginItem = new JMenuItem("重新登录");
        JMenuItem closeItem = new JMenuItem("关闭游戏");

        JMenuItem accountItem = new JMenuItem("公众号");

        //将每一个选项下的条目添加到选项当中
        functionJMenu.add(replayItem);
        functionJMenu.add(reloginItem);
        functionJMenu.add(closeItem);

        aboutJMenu.add(accountItem);

        //将菜单里的两个选项添加到菜单当中
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);

        //给整个界面设置菜单
        this.setJMenuBar(jMenuBar);

        //让窗体显示出来,建议写在最后
        this.setVisible(true);
    }

        将构造方法中的代码进行抽取,抽取到不同的方法中。

 public GameJFrame() {
        //初始化界面
        initJFrame();

        //初始化菜单
        initJMenuBar();

        //让窗体显示出来,建议写在最后
        this.setVisible(true);
    }

    private void initJMenuBar() {
        //初始化菜单
        //创建整个的菜单对象
        JMenuBar jMenuBar = new JMenuBar();

        //创建菜单上的两个选项的对象(功能   关于我们)
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");

        //创建选项下面的条目对象
        JMenuItem replayItem = new JMenuItem("重新游戏");
        JMenuItem reloginItem = new JMenuItem("重新登录");
        JMenuItem closeItem = new JMenuItem("关闭游戏");

        JMenuItem accountItem = new JMenuItem("公众号");

        //将每一个选项下的条目添加到选项当中
        functionJMenu.add(replayItem);
        functionJMenu.add(reloginItem);
        functionJMenu.add(closeItem);

        aboutJMenu.add(accountItem);

        //将菜单里的两个选项添加到菜单当中
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);

        //给整个界面设置菜单
        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        //设置界面的宽高
        this.setSize(603, 680);
        //设置界面的标题
        this.setTitle("拼图单机版 v1.0");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置游戏的关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

【练习4】添加图片。

 private void initImage() {
        //创建一个图片ImageIcon的对象
        ImageIcon icon = new ImageIcon("D:\\IdeaProjects\\PuzzleGame\\image\\daxiaoqiao\\1.jpg");
        //创建一个JLabel的对象(管理容器)
        JLabel jLabel = new JLabel(icon);
        //把管理容器添加到界面中
        this.add(jLabel);
    }

        注意:窗体和图片之间其实还有一个隐藏的容器(布局方式容器),这个容器可以用 getContentPane() 来获取,我们添加的图片,实质上是添加进了这个容器当中,如果没有设定这个容器的特殊要求,它会默认把图片放到居中位置。如果不想将图片放到正中央,就需要把这个默认机制取消掉,我们可以使用 setLayout(null) 来实现。

  //在JFrame的初始化方法中
  //取消掉默认居中布局
  this.setLayout(null);
    private void initImage() {
        //创建一个图片ImageIcon的对象
        ImageIcon icon = new ImageIcon("D:\\IdeaProjects\\PuzzleGame\\image\\daxiaoqiao\\1.jpg");
        //创建一个JLabel的对象(管理容器)
        JLabel jLabel = new JLabel(icon);
        //指定图片位置
        jLabel.setBounds(0, 0, 105, 105);
        //把管理容器添加到布局容器中
        this.getContentPane().add(jLabel);
    }

【练习5】打乱一维数组中的数据。
需求:把一个一维数组中的数据:0~15打乱顺序,然后再以4个一组的方式添加到二维数组中。

package test;

import java.util.Random;

public class RandomTest {
    public static void main(String[] args) {
        int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

        Random r = new Random();
        for(int i = 0; i < tempArr.length; i++) {
            //获取随机索引
            int index = r.nextInt(tempArr.length);
            int temp = 0;
            //交换两个索引上的数据
            temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = temp;
            //遍历数组
            System.out.print(tempArr[i]+" ");
        }
        System.out.println();


        //创建一个二维数组
        int[][] data = new int[4][4];
        //给二维数组添加数据
        //遍历tempArr每一个元素,把每一个元素依次添加到二维数组中
        for (int i = 0; i < tempArr.length; i++) {
            data[i / 4][i % 4] = tempArr[i];
        }
        //遍历二维数组
        for(int i = 0 ;i < 4; i++) {
            for(int j = 0; j < 4; j++) {
                System.out.print(data[i][j] + " ");
            }
            System.out.println();
        }
    }
}

【练习6】打乱图片。

 private void initData() {
        int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

        Random r = new Random();
        for(int i = 0; i < tempArr.length; i++) {
            //获取随机索引
            int index = r.nextInt(tempArr.length);
            //交换两个索引上的数据
            int temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = temp;

        }
        //给二维数组添加数据
        //遍历tempArr每一个元素,把每一个元素依次添加到二维数组中
        for (int i = 0; i < tempArr.length; i++) {
            data[i / 4][i % 4] = tempArr[i];        //巧妙运用数学知识,一行完成二维数组元素的添加
        }
    }
  private void initImage() {
        for(int i = 0 ; i < 4; i++){
            for (int j = 0; j < 4; j++) {
                //创建一个图片ImageIcon的对象
                ImageIcon icon = new ImageIcon("D:\\IdeaProjects\\PuzzleGame\\image\\daxiaoqiao\\" + data[i][j] + ".jpg");
                //创建一个JLabel的对象(管理容器)
                JLabel jLabel = new JLabel(icon);
                //指定图片位置
                jLabel.setBounds(105 * j, 105 * i + 4, 105, 105);
                //把管理容器添加到界面中
                this.getContentPane().add(jLabel);
            }
        }

在这里插入图片描述

二、事件

        事件,就是可以被组件识别的操作。当你对组件做了某种事情后,就会执行相应的代码。
        GUI 中的事件源可以是按钮、图片、窗体等等控件。
        鼠标点击、鼠标划入等等操作都统称为事件
        当事件源上发生了某个事件,则执行某段代码,这个行为叫做绑定监听

  • KeyListner                键盘监听
  • MouseListener         鼠标监听
  • ActionListener          动作监听

【练习7】写一个按钮的事件监听。
        按钮用 JButton 类创建。事件监听需要导包,导入 ActionEvent 和 ActionListener。

package test;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EventTest {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        //设置界面宽高
        jFrame.setSize(603, 680);
        //设置界面标题
        jFrame.setTitle("事件演示");
        //设置界面置顶
        jFrame.setAlwaysOnTop(true);
        //设置界面居中
        jFrame.setLocationRelativeTo(null);
        //设置关闭模式
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消组件默认居中放置
        jFrame.setLayout(null);


        //创建一个按钮对象
        JButton jbt = new JButton("点我");
        //设置按钮位置和宽高
        jbt.setBounds(0,0,100,50);
        //给按钮添加动作监听
        //jbt:组件对象,表示要给哪个组件添加事件
        //addActionListener:表示我要给组件添加哪个事件监听(动作监听包括两种:鼠标左键点击,空格按下)
        //()中应该传入一个接口的实现类对象,该对象是ActionListener的对象,可以用匿名内部类作为参数
        //该参数表示:事件被触发后要执行的代码
        jbt.addActionListener(new ActionListener() {
        	@Override
            public void actionPerformed(ActionEvent e) {
            	System.out.println("按钮被点击了");
            }
        });
        //把按钮添加入窗体
        jFrame.getContentPane().add(jbt);
        //显示窗体
        jFrame.setVisible(true);
    }
}

        事件监听还有另一种写法,可以让当前界面去实现 ActionListener 接口。
        写一个窗体实现 ActionListener 接口。

package test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class MyJFrame extends JFrame implements ActionListener {

    //创建2个按钮,必须在成员位置
    JButton jbt1 = new JButton("点我!");
    JButton jbt2 = new JButton("点我。");
        //构造方法
    MyJFrame() {
        this.setSize(600,600);
        //设置界面的标题
        this.setTitle("登录");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置窗口的关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //设置窗口的Layout
        this.setLayout(null);

        //设置按钮位置大小
        jbt1.setBounds(0,0,100,50);
        jbt2.setBounds(202,0,100,50);

        //给按钮添加事件
        //因为当前类实现了ActionListener接口,所以该类本身对象即可作为参数反传递给addActionListener()方法
        jbt1.addActionListener(this);
        jbt2.addActionListener(this);

        //把按钮添加到窗口中
        this.getContentPane().add(jbt1);
        this.getContentPane().add(jbt2);

        //显示窗体
        this.setVisible(true);

    }
    //实现ActionListener接口,重写actionPerformed方法
    @Override
    public void actionPerformed(ActionEvent e) {
        //对当前的按钮进行判断
        //获取当前被操作的那个按钮对象
        Object source = e.getSource();
        if(source == jbt1) {
            jbt1.setSize(200,200);
            System.out.println("按钮1被按下");
        }else{
            Random r = new Random();
            jbt2.setLocation(r.nextInt(500),r.nextInt(500));
            System.out.println("按钮2被按下");
        }
    }
}

        测试类用来创建窗口。

package test;

public class EventTest2 {
    public static void main(String[] args) {
        MyJFrame myJFrame = new MyJFrame();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值