Java初学——阶段项目

一 JFrame类创建界面  initJFrame();

1 创建JFrame类的窗口对象;

2 初始化界面:大小;标题;置顶;界面居中;关闭模式;取消组件居中以启用坐标轴;显示窗口;等等。

private void initJFrame() {
        // 设置界面的大小
        this.setSize(420,500);

        // 设置界面标题
        this.setTitle("拼图单机版 v1.0");

        // 设置界面置顶
        this.setAlwaysOnTop(true);

        // 设置界面居中
        this.setLocationRelativeTo(null);

        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //取消图片的默认居中放置,只有取消了才会按照xy轴的形式添加组件
        this.setLayout(null);
    }

二 初始化菜单  initJMenuBar();

1 创建菜单对象;

2 创建选项对象;

3 创建条目对象;

4 将条目添加到选项中,选项添加到菜单中;

5 在窗口对象中设置JMenuBar。

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);
    }

三 添加图片

 涉及类:ImageIcon; Jlabel

概括说,创建一个ImageIcon类的图片对象,并指定图片在电脑中的位置;再创建一个Jabel对象,把图片交给Jlabel;再把整体放到界面中。

 图片文件夹添加到当前模块下;设计打乱图片的方法;打乱图片;在界面的不同位置放置这些图片。

1 创建ImageIcon的对象;

2 创建JLabel的对象(管理容器);

3 指定图片位置

4 添加管理容器到界面中,getContentPane()可以理解为获取界面的隐藏容器。

 int[][] data = new int[4][4];
private void initData() {
        int[] temp =  {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 < 16; i++) {
            int index = r.nextInt(temp.length);
            int value = temp[i];
            temp[i] = temp[index];
            temp[index] = value;
        }
        for (int i = 0; i < 16; i++) {
            data[i/4][i%4]=  temp[i];
        }

    }

    private void initImage() {
        int number=1;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                // 创建一个图片ImageIcon的对象
                ImageIcon Icon = new ImageIcon("D:\\JavaLearning\\JavaLearning_1\\puzzlegame\\image\\girl\\girl1\\"+data[i][j]+".jpg");
                number++;

                //创建一个JLabel的对象(管理容器)
                JLabel label = new JLabel(Icon);

                //指定图片位置
                label.setBounds(105*i,105*j,105,105);

                //把管理容器添加到界面中,getContentPane()可以理解为获取界面的隐藏容器
                this.getContentPane().add(label);
            }
        }
    }

四 事件

事件是可以被组件识别的操作

1 事件源:按钮,图片,窗体等。

2 事件: 对事件源的操作,如鼠标单击,鼠标划入等。

3 绑定监听:当事件源上发生了某个事件,则执行某段代码。

(1)KeyListener:键盘监听,

细节:

a addKeyListener()方法的调用者是窗口对象,参数时接口KeyListener的实现类。

b 如果按下一个键不松开,会重复调用Keypressed方法;

c e.getKeyCode()表示获取键盘上每一个按键的编号

(2)MouseListener:鼠标监听;

(3)ActionListener:动作监听

单击或者空格触发。

4 流程

(1)创建一个按钮对象;

(2)并设置位置和宽高;

(3)给按钮对象设置监听并以参数的形式传递事件触发后执行的代码;

(4)把按钮添加到界面当中。

5 两种在界面中添加事件的方式

(1)以匿名内部类的方式将ActionListener的实现类作为参数传递给绑定监听方法(如ActionListener())。

public class test {
    public static void main(String[] args) {
        JFrame jf = new JFrame();
        jf.setSize(500, 500);
        jf.setTitle("添加事件的方式一");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭即退出
        jf.setLocationRelativeTo(null);//居中
        jf.setLayout(null);//取消组件的默认居中
        jf.setAlwaysOnTop(true);//置顶显示

        //设置按钮的范围,绑定监听
        JButton b1 = new JButton("点我!");
        b1.setBounds(0, 0, 100, 100);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("不要点我!!");
            }
        });
        jf.getContentPane().add(b1);//在窗口界面添加按钮
        jf.setVisible(true);
    }
}

不要点我!!

(2)将本类(JFrame)对象实现ActionListener接口后作为实现类传递给绑定监听方法(如ActionListener())。

public class MyJFrame extends JFrame implements ActionListener {
    JButton button1;
    JButton button2;

    public MyJFrame() {
        //初始化窗口
        this.setSize(500, 500);
        this.setTitle("添加事件的方式二");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭即退出
        this.setLocationRelativeTo(null);//居中
        this.setLayout(null);//取消组件的默认居中
        this.setAlwaysOnTop(true);//置顶显示

        //新建两个按钮对象
        button1 = new JButton("你点我就变大");
        button2 = new JButton("你点我就跳");

        //设置按钮大小
        button1.setBounds(0,0,150,150);
        button2.setBounds(250,250,100,100);

        //由于本类实现了ActionListener接口,所以本类对象可以作为addActionListener()的参数,
        // 在事件触发时执行本类中的actionPerformed()方法。
        button1.addActionListener(this);
        button2.addActionListener(this);

        //在我的窗口界面中添加这两个按钮
        this.getContentPane().add(button1);
        this.getContentPane().add(button2);

        //显示窗口
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        //getSource()获取当前被操作的按钮对象,e为事件,返回发生事件的对象
        Object source = e.getSource();
        if (source == button1) {
            button1.setSize(200,200);
        }
        else if (source == button2) {
            Random r = new Random();
            button2.setLocation(r.nextInt(500),r.nextInt(500));
        }
    }
}
public class MyTest {
    public static void main(String[] args) {
        new MyJFrame();
    }
}

五 移动图片

重要步骤:

1 在界面初始化函数中写好事件接口

2 事件:松开键盘的上下左右键;

触发代码(keyReleased):以向上移动为例,空白格位置变成它下面的图片;

3 在每次利用ImageIcon和JLabel添加图片之前,要清空之前的图片,否则无法更新图片;并且每次更新完图片要进行刷新。

4 对于超出范围的情况要做处理。

六 查看完整图片

七 作弊码

八 判断胜利

九 计步功能

十 菜单业务实现

1 重新游戏

2 重新登录

3 关闭游戏

4 公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值