首页 > 编程语言 >JAVA- 动漫美女拼图(给按钮添加事件)

JAVA- 动漫美女拼图(给按钮添加事件)

时间:2022-11-22 22:12:02浏览次数:43  
标签:JAVA 拼图 JButton int itheima 按钮 new public datas

代码一

package com.itheima_05;

public class App{
    public static void main(String[] args) {
        PictureFrame pf=new PictureFrame();

    }
}

代码二

package com.itheima_05;

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

public class PictureFrame extends JFrame {

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

    //定义两个int类型的变量,用于记录0号图片的位置
    private int x0;
    private int y0;

    //定义上左下右,求助,重置按钮
    private JButton shangButton;
    private JButton zuoButton;
    private JButton xiaButton;
    private JButton youButton;
    private JButton qiuzhuButton;
    private JButton chongzhiButton;





    public PictureFrame() {
        //用于窗体的基本设置
        initFrame();



        //二维数组元素打乱
        randomData();

        //窗体上组件的绘制
        paintView();

        //给按钮添加事件
        addButtonEvent();

        //设置窗体可见
        this.setVisible(true);

    }

    //给按钮添加事件
    public void addButtonEvent(){
        shangButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("上");
            }
        });
        zuoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("左");
            }
        });
        xiaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("下");
            }
        });
        youButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("右");
            }
        });
        qiuzhuButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("求助");
            }
        });
        chongzhiButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("重置");
            }
        });
    }
    //二维数组元素打乱
    public  void randomData(){
        Random r =new Random();
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                int x=r.nextInt(datas.length);
                int y=r.nextInt(datas[i].length);


                int temp =datas[i][j];
                datas[i][j]=datas[x][y];
                datas[x][y]=temp;


            }
        }
        //记录0号图片的位置
        wc:for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if(datas[i][j]==0){
                    x0=i;
                    y0=j;
                    break wc;
                }
            }
        }
        System.out.println(x0+","+y0);
    }

    //窗体上组件的绘制
    public void paintView() {
        //标题图片
        JLabel titleLabel = new JLabel(new ImageIcon("itheima_picture_puzzle\\images\\title.png"));
        titleLabel.setBounds(354, 27, 232, 57);
        this.add(titleLabel);

        //定义一个二维数组用来存储图片的编号
//        int[][] datas={
//                {1,2,3,4},
//                {5,6,7,8},
//                {9,10,11,12},
//                {13,14,15,16}
//        };
        //创建面板
        JPanel imagePanel = new JPanel();
        imagePanel.setBounds(150, 114, 360, 360);
        imagePanel.setLayout(null);//取消面板的默认布局
        //遍历二维数组得到图片编号
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                //创建JLabel对象加载图片资源
                JLabel imageLabel = new JLabel(new ImageIcon("itheima_picture_puzzle\\images\\" + datas[i][j] + ".png"));
                //调整图片的位置
                imageLabel.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imageLabel);
            }
        }
//        把面板添加到窗体上
        this.add(imagePanel);

        //动漫参照图
        JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima_picture_puzzle\\images\\canzhaotu.png"));
        canZhaoTuLabel.setBounds(574, 114, 122, 121);
        this.add(canZhaoTuLabel);

        //上下左右按钮以及求助重置按钮
         shangButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\shang.png"));
        shangButton.setBounds(732, 265, 57, 57);
        this.add(shangButton);


         zuoButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\zuo.png"));
        zuoButton.setBounds(650, 347, 57, 57);
        this.add(zuoButton);


         xiaButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\xia.png"));
        xiaButton.setBounds(732, 347, 57, 57);
        this.add(xiaButton);


         youButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\you.png"));
        youButton.setBounds(813, 347, 57, 57);
        this.add(youButton);


         qiuzhuButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\qiuzhu.png"));
        qiuzhuButton.setBounds(626, 444, 108, 45);
        this.add(qiuzhuButton);


        chongzhiButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\chongzhi.png"));
        chongzhiButton.setBounds(786, 444, 108, 45);
        this.add(chongzhiButton);


        //展示背景图
        JLabel backgroundLabel = new JLabel(new ImageIcon("itheima_picture_puzzle\\images\\background.png"));
        backgroundLabel.setBounds(0, 0, 960, 530);
        this.add(backgroundLabel);
    }


    //用于窗体的基本设置
    public void initFrame() {
        //窗体大小
        this.setSize(960, 565);

        //窗体标题
        this.setTitle("动漫拼图");

        //窗体居中
        this.setLocationRelativeTo(null);

        //窗体关闭时退出程序
        this.setDefaultCloseOperation(3);

        //窗体要位于其他窗口之上
        this.setAlwaysOnTop(true);

        //取消窗体默认布局
        this.setLayout(null);
    }
}

执行结果
image

标签:JAVA,拼图,JButton,int,itheima,按钮,new,public,datas
From: https://www.cnblogs.com/cy-xt/p/16916679.html

相关文章

  • JAVA- 动漫美女拼图(记录0号图片位置)
    代码一packagecom.itheima_02;publicclassApp{publicstaticvoidmain(String[]args){PictureFramepf=newPictureFrame();}}代码二pa......
  • Java多线程 线程池的生命周期及运行状态
    (目录)一、说明线程池的生命周期线程池的状态runState和工作线程数量workerCount共同保存在AtomicInteger类型的控制变量ctl中ctl高三位保存运行状态(2^3^=8>5),低2......
  • java爬虫
    创建Maven工程加入依赖<!--https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient--><dependency><groupId>org.apache.httpcomponents</gr......
  • 【JAVA笔记】jJAVA入门基础02
     一.符号及类型1.1添加注释comment注释:就是对代码的解释和说明。其目的是让人们能够更加轻松地了解代码。为代码添加注释,是十分必须要的,它不影响程序的编译和运行......
  • Java基础题
    Java基础题1.Java面向对象有哪些特征继承、封装、多态三大特征相互相乘封装:封装内部的一些实现机制,就跟方法封装一些实现的机制一样继承:就是从已有的类派生出新的类,新......
  • Java基础
    java基础一.java注释1.单行注释:////这是单行注释2.多行注释:/*注释内容*//*这是多行注释这是多行注释*/3.文档注释(JavaDoc):/**注释内容*//***@......
  • java变量 运算符包机制 doc
    java基础2一.变量java变量是程序中最基本存储单元,要素包含变量名变量类型作用域数据类型变量类型变量名=值;例:Stringa='shiftnm';变量作用域类......
  • javascript-代码随想录训练营day7
    454.四数相加Ⅱ题目链接:https://leetcode.cn/problems/4sum-ii/题目描述:给你四个整数数组nums1、nums2、nums3和nums4,数组长度都是n,请你计算有多少个元组(i,j,......
  • java注解-使用注解处理器实现动态生成get和set方法
    目录一、简介二、如何实现1.环境说明:2.创建项目3.定义@Data注解4.定义@Data的注解处理器5.创建一个测试类6.通过idea的maven工具栏进行编译7.查看编译后的结果8.通过反射查......
  • JAVA---单例模式
    单例模式单例的目的是保证某个类仅有一个实例。当有某些类创建对象内存开销较大时可以考虑使用该模式。单例模式又分为饿汉式和懒汉式。1.饿汉式饿汉式。顾名思义,该......