拼图小游戏

  做一个拼图小游戏要有登入页面注册页面,可以换图片,一键看图,一键还原,还有存档

游戏页面:

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Properties;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    int[][] ints = new int[3][3];
    int[][]win=new int[][]{
            {1,2,3},
            {4,5,6},
            {7,8,0}
    };
    //空白处的的位置
    int x=0;
    int y=0;
    String path="java\\imger1\\";
    //步数
    int step=0;
    //菜单项
    JMenuItem jMenuItem = new JMenuItem("重新游戏");
    JMenuItem jMenuItem1 = new JMenuItem("重新登入");
    JMenuItem jMenuItem2 = new JMenuItem("关闭游戏");


    JMenuItem jMenuItem3 = new JMenuItem("公众号");
    JMenuItem jMenuItem0 = new JMenuItem("美女");
    JMenuItem jMenuItem4 = new JMenuItem("植物");
    JMenuItem jMenuItem5 = new JMenuItem("动物");
    JMenuItem jMenuItem6 = new JMenuItem("存档0(空)");
    JMenuItem jMenuItem7 = new JMenuItem("存档1(空)");
    JMenuItem jMenuItem8 = new JMenuItem("存档2(空)");
    JMenuItem jMenuItem9 = new JMenuItem("存档3(空)");
    JMenuItem jMenuItem10 = new JMenuItem("存档4(空)");
    JMenuItem jMenuItem11 = new JMenuItem("存档5(空)");
    JMenuItem jMenuItem12 = new JMenuItem("读档0(空)");
    JMenuItem jMenuItem13 = new JMenuItem("读档1(空)");
    JMenuItem jMenuItem14 = new JMenuItem("读档2(空)");
    JMenuItem jMenuItem15 = new JMenuItem("读档3(空)");
    JMenuItem jMenuItem16 = new JMenuItem("读档4(空)");
    JMenuItem jMenuItem17 = new JMenuItem("读档5(空)");
    JMenu jMenu4 = new JMenu("读档");
    JMenu jMenu3 = new JMenu("存档");
    public GameJFrame() {
        intiJFrame();
        initJFrame();
        initDate();
        initImager();
        //显示
        this.setVisible(true);
    }

    private void initDate() {
        ArrayList<Integer> integers = new ArrayList<>();
        Collections.addAll(integers, 0, 1, 2, 3, 4, 5, 6, 7, 8);
        Collections.shuffle(integers);
        int index = 0;
        for (int i1 = 0; i1 < ints.length; i1++) {
            for (int i = 0; i < ints[i1].length; i++) {
                ints[i1][i] = integers.get(index);
                if (integers.get(index)==0){
                    x=i1;
                    y=i;
                }
                index++;
            }
        }
    }

    private void initImager(){
        //清空原本出现的图片
        this.getContentPane().removeAll();
        //判断
        if (victory()){
            JLabel jLabel = new JLabel(new ImageIcon(path + "胜利.png"));
            jLabel.setBounds(20,20,179,78);
            this.getContentPane().add(jLabel);
        }
        //步数
        JLabel jLabel2 = new JLabel("步数" + step);
        jLabel2.setBounds(500,50,200,50);
        this.getContentPane().add(jLabel2);
        //先加载的图片在上面 后加载的图片在下方
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                //创建一个图片ImagerIcon对象
                //创建JLabel对象(容器)
                int i1 = ints[i][j];
                JLabel jLabel = new JLabel(new ImageIcon(path+"未标题-"+i1+".png"));
                //设置图片位置
                jLabel.setBounds(105*j+132,105*i+150,105,105);
                //添加图片边框
                //0:让图片突出
                //1:让图片凹进去
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                //把容器添加界面中
                this.getContentPane().add(jLabel);
            }
    }
        //背景
        JLabel jLabel1 = new JLabel(new ImageIcon(path+"img.png"));
        jLabel1.setBounds(35,4,506,560);
        this.getContentPane().add(jLabel1);
        //刷新
        this.getContentPane().repaint();
}
    private void initJFrame() {
        //菜单栏
        JMenuBar jMenuBar = new JMenuBar();
        //菜单
        JMenu jMenu = new JMenu("功能");
        JMenu jMenu1 = new JMenu("关于我们");
        JMenu jMenu2 = new JMenu("更换图片");


        jMenu.add(jMenu2);
        jMenu.add(jMenu3);
        jMenu.add(jMenu4);
        jMenu2.add(jMenuItem0);
        jMenu2.add(jMenuItem4);
        jMenu2.add(jMenuItem5);
        jMenu3.add(jMenuItem6);
        jMenu3.add(jMenuItem7);
        jMenu3.add(jMenuItem8);
        jMenu3.add(jMenuItem9);
        jMenu3.add(jMenuItem10);
        jMenu3.add(jMenuItem11);
        jMenu4.add(jMenuItem12);
        jMenu4.add(jMenuItem13);
        jMenu4.add(jMenuItem14);
        jMenu4.add(jMenuItem15);
        jMenu4.add(jMenuItem16);
        jMenu4.add(jMenuItem17);
        //监听
        jMenuItem0.addActionListener(this);
        jMenuItem.addActionListener(this);
        jMenuItem1.addActionListener(this);
        jMenuItem2.addActionListener(this);
        jMenuItem3.addActionListener(this);
        jMenuItem4.addActionListener(this);
        jMenuItem5.addActionListener(this);
        jMenuItem6.addActionListener(this);
        jMenuItem7.addActionListener(this);
        jMenuItem8.addActionListener(this);
        jMenuItem9.addActionListener(this);
        jMenuItem10.addActionListener(this);
        jMenuItem11.addActionListener(this);
        jMenuItem12.addActionListener(this);
        jMenuItem13.addActionListener(this);
        jMenuItem14.addActionListener(this);
        jMenuItem15.addActionListener(this);
        jMenuItem16.addActionListener(this);
        jMenuItem17.addActionListener(this);
        //添加
        jMenu.add(jMenuItem);
        jMenu.add(jMenuItem1);
        jMenu.add(jMenuItem2);

        jMenu1.add(jMenuItem3);

        jMenuBar.add(jMenu);
        jMenuBar.add(jMenu1);
        getGameinfo();
        this.setJMenuBar(jMenuBar);
    }
    private void getGameinfo(){
        File file = new File("java\\src\\Game03\\save");
        File[] files = file.listFiles();
        for (File file1 : files) {
            Gameinfo gi= null;
            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file1));
                gi = (Gameinfo)objectInputStream.readObject();
                objectInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            //步数
            int step1 = gi.getStep();
            //文件名
            String name = file1.getName();
            int i1 = name.charAt(4) - '0';
            jMenu3.getItem(i1).setText("存档"+i1+"("+step1+")");
            jMenu4.getItem(i1).setText("读档"+i1+"("+step1+")");
        }
    }

    private void intiJFrame() {
        //设置宽高
        this.setSize(603,680);
        //设置界面标题
        this.setTitle("拼图单机小游戏1.0");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭页面
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //关闭容器的默认值
        this.setLayout(null);
        //添加键盘监听
        this.addKeyListener(this);
    }

    /**
     * Invoked when a key has been typed.
     * See the class description for {@link KeyEvent} for a definition of
     * a key typed event.
     *
     * @param e
     */
    @Override
    public void keyTyped(KeyEvent e) {

    }

    /**
     * Invoked when a key has been pressed.
     * See the class description for {@link KeyEvent} for a definition of
     * a key pressed event.
     *
     * @param e
     */
    @Override
    public void keyPressed(KeyEvent e) {
        //按下不松
        int keyCode = e.getKeyCode();
        if (keyCode==65){
            //清空当前页面的图片
            this.getContentPane().removeAll();
            //加载
            JLabel jLabel = new JLabel(new ImageIcon(path+"未标题-10.png"));
            jLabel.setBounds(130,150,320,320);
            this.getContentPane().add(jLabel);
            JLabel jLabel1 = new JLabel(new ImageIcon(path+"img.png"));
            jLabel1.setBounds(35,4,506,560);
            this.getContentPane().add(jLabel1);
            this.getContentPane().repaint();
        }
    }

    /**
     * Invoked when a key has been released.
     * See the class description for {@link KeyEvent} for a definition of
     * a key released event.
     *
     * @param e
     */
    @Override
    public void keyReleased(KeyEvent e) {
        //松开
        if (victory()){
            //结束方法
            return;
        }
        //上38
        //下40
        //左37
        //右39
        int keyCode = e.getKeyCode();

        if (keyCode==38){
            if (x==2){
                return;
            }
            //零变成零下面的数
            ints[x][y]=ints[x+1][y];
            ints[x+1][y]=0;
            x++;
            step++;
            //新的零的位置
            initImager();
        }else if (keyCode==40){
            if (x==0){
                return;
            }
            ints[x][y]=ints[x-1][y];
            ints[x-1][y]=0;
            x--;
            step++;
            initImager();
        }else if (keyCode==37){
            if (y==2){
                return;
            }

            ints[x][y]=ints[x][y+1];
            ints[x][y+1]=0;
            y++;
            step++;
            initImager();
        }else if (keyCode==39){
            if (y==0){
                return;
            }

            ints[x][y]=ints[x][y-1];
            ints[x][y-1]=0;
            y--;
            step++;
            initImager();
        }else if (keyCode==65){
            initImager();
        }else if (keyCode==87){
            ints=new int[][]{
                    {1,2,3},
                    {4,5,6},
                    {7,8,0}
            };
            initImager();
            x=2;
            y=2;
        }
    }
    //胜利方法
    private boolean victory(){
        for (int i1 = 0; i1 < ints.length; i1++) {
            for (int i = 0; i < ints[i1].length; i++) {
                if (ints[i1][i]!=win[i1][i]){
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source==jMenuItem){
            //重新游戏
            step=0;
            initDate();
            initImager();
        }else if (source==jMenuItem1){
            //重新登入
            this.setVisible(false);
            new LoginJFrame();
        }else if (source==jMenuItem2){
            //关闭游戏
            System.exit(0);
        }else if (source==jMenuItem3) {
            //创建一个弹框对象
            JDialog jDialog = new JDialog();
            Properties properties = new Properties();
            try {
                FileInputStream stream = new FileInputStream("java\\picture.properties");
                properties.load(stream);
                stream.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            String s = (String) properties.get("a");
            //图片
            JLabel jLabel = new JLabel(new ImageIcon(s));
            //图片位置
            jLabel.setBounds(0, 0, 258, 258);
            //添加到弹框里
            jDialog.getContentPane().add(jLabel);
            //设置弹框大小
            jDialog.setSize(344, 344);
            //置顶
            jDialog.setAlwaysOnTop(true);
            //居中
            jDialog.setLocationRelativeTo(null);
            //不关闭不可以执行
            jDialog.setModal(true);
            //显示
            jDialog.setVisible(true);
        }else if (source==jMenuItem0){
            step=0;
            path="java\\imger2\\";
            initDate();
            initImager();
        } else if (source==jMenuItem4) {
            step=0;
            path="java\\imger3\\";
            initDate();
            initImager();
        } else if (source==jMenuItem5) {
            step=0;
            path="java\\imger4\\";
            initDate();
            initImager();
        } else if (source==jMenuItem6||source==jMenuItem7||source==jMenuItem8||source==jMenuItem9||source==jMenuItem10||source==jMenuItem11) {
            System.out.println("存档");
            //获得里面的菜单信息
            JMenuItem source1 = (JMenuItem) source;
            String text = source1.getText();
            int i1 = text.charAt(2) - '0';
            System.out.println(i1);
            try {
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("java\\src\\Game03\\save\\save"+i1+".date"));
                Gameinfo gameinfo = new Gameinfo(ints, x, y, path, step);
                objectOutputStream.writeObject(gameinfo);
                objectOutputStream.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            source1.setText("存档"+i1+"步数"+step);
            //获取读档里的数据
            //
            jMenu4.getItem(i1).setText("读档"+i1+"步数"+step);
        } else if (source==jMenuItem12||source==jMenuItem13||source==jMenuItem14||source==jMenuItem15||source==jMenuItem16||source==jMenuItem17) {
            System.out.println("读档");
            //获得里面的菜单信息
            JMenuItem source1 = (JMenuItem) source;
            String text = source1.getText();
            int i1 = text.charAt(2) - '0';
            Gameinfo gameinfo=null;
            System.out.println(i1);
            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("java\\src\\Game03\\save\\save"+i1+".date"));
                 gameinfo = (Gameinfo) objectInputStream.readObject();
                objectInputStream.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            } catch (ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }
           ints=gameinfo.getData();
            x=gameinfo.getX();
            y=gameinfo.getY();
            path=gameinfo.getPath();
            step=gameinfo.getStep();
            initImager();
        }
    }
}

登入页面:

import cn.hutool.core.io.FileUtil;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LoginJFrame extends JFrame implements ActionListener, MouseListener {
    ArrayList<User> users = new ArrayList<>();
    ArrayList<String> list = new ArrayList<>();
    JTextField jTextField = new JTextField();
    JPasswordField jTextField2 = new JPasswordField();
    JTextField jTextField3 = new JTextField();
    JLabel jLabel4 = new JLabel();
    JButton jButton3 = new JButton("换一下");
    JButton jButton = new JButton();
    JButton jButton2 = new JButton();
    String app="java\\imger1\\登入.png";
    String app2="java\\imger1\\注册.png";
    public LoginJFrame(){
        readUserinto();
       initJFrame();
        String();
       initImager();
        //设置界面显示
        this.setVisible(true);
    }

    private void readUserinto() {
        List<String> list = FileUtil.readUtf8Lines("Game03\\Geme\\用户信息.txt");
        for (String s : list) {
            String[] split = s.split("&");
            String[] split1 = split[0].split("=");
            String[] split2 = split[1].split("=");
            User user = new User(split1[1], split2[1]);
            users.add(user);
        }

    }

    private void String(){

        Collections.addAll(list,"A","B","C","D",
                "E","F","G","H","I","J","K","L","M","N","O","P","Q",
                "R","S","T","U","V","W","X","Y","Z",
                "a","b","c","d","e","f","g","h","i",
                "j","k","l","m","n","o","p","q","r",
                "s","t","u","v","w","x","y","z",
                "1","2","3","4","5","6","7","8","9",
                "10","0");
        Collections.shuffle(list);

            jLabel4.setText(list.get(0)+list.get(1)+list.get(2)+list.get(3)+list.get(4));
    }
    private void initJFrame(){
        //设置界面大小
        this.setSize(480,430);
        //设置界面标题
        this.setTitle("登入");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置界面关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //关闭默认
        this.setLayout(null);
    }
    private void initImager(){
        this.getContentPane().removeAll();
        JLabel jLabel1 = new JLabel(new ImageIcon("java\\imger1\\用户名.png"));
        jLabel1.setBounds(25,120,200,60);
        this.getContentPane().add(jLabel1);
        //文本框

        jTextField.setBounds(180,138,200,30);
        jTextField.setBorder(new BevelBorder(BevelBorder.LOWERED));
        this.getContentPane().add(jTextField);
        JLabel jLabel2 = new JLabel(new ImageIcon("java\\imger1\\密码.png"));
        jLabel2.setBounds(25,180,200,60);
        this.getContentPane().add(jLabel2);

        jTextField2.setBounds(180,190,200,30);
        jTextField2.setBorder(new BevelBorder(BevelBorder.LOWERED));
        this.getContentPane().add(jTextField2);
        JLabel jLabel3 = new JLabel(new ImageIcon("java\\imger1\\码.png"));
        jLabel3.setBounds(25,220,200,60);
        this.getContentPane().add(jLabel3);

        jTextField3.setBounds(180,240,100,30);
        jTextField3.setBorder(new BevelBorder(BevelBorder.LOWERED));
        this.getContentPane().add(jTextField3);

        jLabel4.setBounds(300,230,100,50);
        this.getContentPane().add(jLabel4);
        jButton3.setBounds(320,240,80,30);
        jButton3.setBorderPainted(false);
        jButton3.setContentAreaFilled(false);
        //文字颜色
        jButton3.setForeground(Color.red);
        jButton3.addActionListener(this);
        this.getContentPane().add(jButton3);
        //按钮
        jButton.setBounds(120,290,80,40);
        //设置按钮背景
        jButton.setIcon(new ImageIcon(app));
        //关闭边框
        jButton.setBorderPainted(false);
        //关闭按钮背景
        jButton.setContentAreaFilled(false);
        //监听
        jButton.addMouseListener(this);
        this.getContentPane().add(jButton);

        jButton2.addMouseListener(this);
        jButton2.setBounds(270,290,80,45);
        //设置按钮背景
        jButton2.setIcon(new ImageIcon(app2));
        //关闭边框
        jButton2.setBorderPainted(false);
        //关闭按钮背景
        jButton2.setContentAreaFilled(false);
        this.getContentPane().add(jButton2);
        JLabel jLabel = new JLabel(new ImageIcon("java\\imger1\\1.png"));
        jLabel.setBounds(0,0,480,430);
        this.getContentPane().add(jLabel);
this.getContentPane().repaint();
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source==jButton3){
            String();
            initImager();
        }
    }

    /**
     * Invoked when the mouse button has been clicked (pressed
     * and released) on a component.
     *
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e) {

    }

    /**
     * Invoked when a mouse button has been pressed on a component.
     *
     * @param e
     */
    @Override
    public void mousePressed(MouseEvent e) {
        Object source = e.getSource();
        if (source == jButton) {
            app = "java\\imger1\\变色1.png";
            initImager();
            String text = jTextField.getText();
            String text2 = jTextField2.getText();
            String text3 = jTextField3.getText();
            for (User user2 : users) {
                String name = user2.getName();
                String poss = user2.getPoss();
                System.out.println(name);
                System.out.println(poss);
                if (text3.length() == 0) {
                    JDialog jDialog = new JDialog();
                    JLabel jLabel = new JLabel("验证码不能为空");
                    jLabel.setBounds(80, 50, 200, 50);
                    jDialog.add(jLabel);
                    jDialog.setSize(40, 80);
                    //置顶
                    jDialog.setAlwaysOnTop(true);
                    //居中
                    jDialog.setLocationRelativeTo(null);
                    //不关闭不可以执行
                    jDialog.setModal(true);
                    //显示
                    jDialog.setVisible(true);
                    return;
                } else if (text.length() == 0 && text2.length() == 0) {
                    JDialog jDialog = new JDialog();
                    JLabel jLabel = new JLabel("账号和密码不能为空");
                    jLabel.setBounds(80, 50, 200, 50);
                    jDialog.add(jLabel);
                    jDialog.setSize(40, 80);
                    //置顶
                    jDialog.setAlwaysOnTop(true);
                    //居中
                    jDialog.setLocationRelativeTo(null);
                    //不关闭不可以执行
                    jDialog.setModal(true);
                    //显示
                    jDialog.setVisible(true);
                    return;
                } else if (!text3.equals(jLabel4.getText())) {
                    JDialog jDialog = new JDialog();
                   JLabel jLabel = new JLabel("验证码错误");
                    jLabel.setBounds(80, 50, 200, 50);
                    jDialog.add(jLabel);
                    jDialog.setSize(40, 80);
                    //置顶
                    jDialog.setAlwaysOnTop(true);
                    //居中
                    jDialog.setLocationRelativeTo(null);
                    //不关闭不可以执行
                    jDialog.setModal(true);
                    //显示
                    jDialog.setVisible(true);
                    return;
                } else if (!text.equals(name)||text.equals(poss)) {
                    JDialog jDialog = new JDialog();
                    JLabel jLabel = new JLabel("账号和密码错误");
                    jLabel.setBounds(80, 50, 200, 50);
                    jDialog.add(jLabel);
                    jDialog.setSize(40, 80);
                    //置顶
                    jDialog.setAlwaysOnTop(true);
                    //居中
                    jDialog.setLocationRelativeTo(null);
                    //不关闭不可以执行
                    jDialog.setModal(true);
                    //显示
                    jDialog.setVisible(true);
                    return;
                } else if (text3.equals(jLabel4.getText())&&text.equals(name)&&text2.equals(poss)) {
                    this.setVisible(false);
                    new GameJFrame();
                    return;
                }
            }
        } else if (source == jButton2) {
            app2 = "java\\imger1\\变色2.png";
            initImager();
            this.setVisible(false);
            new RegisterJFrame();
            return;
        }
    }

    /**
     * Invoked when a mouse button has been released on a component.
     *
     * @param e
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        Object source = e.getSource();
        if (source==jButton){
            app="java\\imger1\\登入.png";
            initImager();
        } else if (source==jButton2) {
            app2="java\\imger1\\注册.png";
            initImager();
        }
    }

    /**
     * Invoked when the mouse enters a component.
     *
     * @param e
     */
    @Override
    public void mouseEntered(MouseEvent e) {

    }

    /**
     * Invoked when the mouse exits a component.
     *
     * @param e
     */
    @Override
    public void mouseExited(MouseEvent e) {

    }
}

 注册页面:

import cn.hutool.core.io.FileUtil;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

public class RegisterJFrame extends JFrame implements ActionListener, MouseListener {

    ArrayList<User> users = new ArrayList<>();
    String app="java\\imger1\\注册.png";
    String app2="java\\imger1\\重置.png";
    JButton jButton = new JButton();
    JButton jButton1 = new JButton();
    JTextField jTextField = new JTextField();
    JPasswordField jTextField2 = new JPasswordField();
    JPasswordField jTextField3 = new JPasswordField();
    public RegisterJFrame() {
        readUserinto();
        initJFrame();
       initImager();
        //设置界面显示
        this.setVisible(true);
    }
    private void readUserinto() {
        List<String> list = FileUtil.readUtf8Lines("Game03\\Geme\\用户信息.txt");
        for (String s : list) {
            String[] split = s.split("&");
            String[] split1 = split[0].split("=");
            String[] split2 = split[1].split("=");
            User user = new User(split1[1], split2[1]);
            users.add(user);
        }
    }
    private void initJFrame(){
        //设置宽高
        this.setSize(488,500);
        //设置界面标题
        this.setTitle("注册");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭页面
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    private boolean USerName(String app){
        for (User user : users) {
            if (user.getName().equals(app)){
                return true;
            }
        }
        return false;
    }

    private void initImager() {
        this.getContentPane().removeAll();
        JLabel jLabel1 = new JLabel(new ImageIcon("java\\imger1\\注册用户.png"));
        jLabel1.setBounds(25, 120, 200, 60);
        this.getContentPane().add(jLabel1);
        //文本框
        jTextField.setBounds(180, 138, 200, 30);
        jTextField.setBorder(new BevelBorder(BevelBorder.LOWERED));
        this.getContentPane().add(jTextField);
        JLabel jLabel2 = new JLabel(new ImageIcon("java\\imger1\\注册密码.png"));
        jLabel2.setBounds(25, 180, 200, 60);
        this.getContentPane().add(jLabel2);

        String s = new String(jTextField2.getPassword());
        System.out.println(s);
        jTextField2.setBounds(180, 190, 200, 30);
        jTextField2.setBorder(new BevelBorder(BevelBorder.LOWERED));
        this.getContentPane().add(jTextField2);
        JLabel jLabel3 = new JLabel(new ImageIcon("java\\imger1\\再次密码.png"));
        jLabel3.setBounds(25, 240, 200, 60);
        this.getContentPane().add(jLabel3);

        jTextField3.setBounds(180, 250, 200, 30);
        jTextField3.setBorder(new BevelBorder(BevelBorder.LOWERED));
        this.getContentPane().add(jTextField3);
        jButton.setBounds(150,300,80,40);
        //设置按钮背景
        jButton.setIcon(new ImageIcon(app));
        //关闭边框
        jButton.setBorderPainted(false);
        //关闭按钮背景
        jButton.setContentAreaFilled(false);
        jButton.addActionListener(this);
        jButton.addMouseListener(this);
        this.getContentPane().add(jButton);
        jButton1.addActionListener(this);
        jButton1.setBounds(250,305,80,25);
        jButton1.setIcon(new ImageIcon(app2));
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        this.getContentPane().add(jButton1);
        JLabel jLabel = new JLabel(new ImageIcon("java\\imger1\\1.png"));
        jLabel.setBounds(0,0,480,430);
        this.getContentPane().add(jLabel);
        this.getContentPane().repaint();
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source==jButton){
            String text = jTextField.getText();
            char[] password = jTextField2.getPassword();
            String s = new String(password);
            char[] password1 = jTextField3.getPassword();
            String s1 = new String(password1);
            if (!s.equals(s1)){
                JDialog jDialog = new JDialog();
                JLabel jLabel = new JLabel("密码输入错误");
                jLabel.setBounds(80,50,200,50);
                jDialog.add(jLabel);
                jDialog.setSize(40, 80);
                //置顶
                jDialog.setAlwaysOnTop(true);
                //居中
                jDialog.setLocationRelativeTo(null);
                //不关闭不可以执行
                jDialog.setModal(true);
                //显示
                jDialog.setVisible(true);
                return;
            } else if (text.length()==0||s.length()==0||s1.length()==0) {
                JDialog jDialog = new JDialog();
                JLabel jLabel = new JLabel("不能为空");
                jLabel.setBounds(80, 50, 200, 50);
                jDialog.add(jLabel);
                jDialog.setSize(40, 80);
                //置顶
                jDialog.setAlwaysOnTop(true);
                //居中
                jDialog.setLocationRelativeTo(null);
                //不关闭不可以执行
                jDialog.setModal(true);
                //显示
                jDialog.setVisible(true);
                return;
            } else if (USerName(text)) {
                JDialog jDialog = new JDialog();
                JLabel jLabel = new JLabel("用户重复");
                jLabel.setBounds(80, 50, 200, 50);
                jDialog.add(jLabel);
                jDialog.setSize(40, 80);
                //置顶
                jDialog.setAlwaysOnTop(true);
                //居中
                jDialog.setLocationRelativeTo(null);
                //不关闭不可以执行
                jDialog.setModal(true);
                //显示
                jDialog.setVisible(true);
                return;
            } else if (!text.matches("[a-zA-Z0-9]{4,16}")) {
                JDialog jDialog = new JDialog();
                JLabel jLabel = new JLabel("用户起名不规范");
                jLabel.setBounds(80, 50, 200, 50);
                jDialog.add(jLabel);
                jDialog.setSize(40, 80);
                //置顶
                jDialog.setAlwaysOnTop(true);
                //居中
                jDialog.setLocationRelativeTo(null);
                //不关闭不可以执行
                jDialog.setModal(true);
                //显示
                jDialog.setVisible(true);
                return;
            }  else if (!s.matches("\\S*(?=\\S{6,})(?=\\S*\\d)(?=\\S*[A-Z])(?=\\S*[a-z])\\S*")) {
                JDialog jDialog = new JDialog();
                JLabel jLabel = new JLabel("密码不规范,至少一个字母,一个数字,6位数");
                jLabel.setBounds(80, 50, 200, 50);
                jDialog.add(jLabel);
                jDialog.setSize(40, 80);
                //置顶
                jDialog.setAlwaysOnTop(true);
                //居中
                jDialog.setLocationRelativeTo(null);
                //不关闭不可以执行
                jDialog.setModal(true);
                //显示
                jDialog.setVisible(true);
                return;
            }else {
                User user = new User(text, s);
                users.add(user);
                System.out.println(users);
                FileUtil.appendLines(users, "Game03\\Geme\\用户信息.txt", "UTF-8");
                this.setVisible(false);
                new LoginJFrame();
                return;
            }
        }
        if (source==jButton1) {
            jTextField.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            return;
        }
    }

    /**
     * Invoked when the mouse button has been clicked (pressed
     * and released) on a component.
     *
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e) {

    }

    /**
     * Invoked when a mouse button has been pressed on a component.
     *
     * @param e
     */
    @Override
    public void mousePressed(MouseEvent e) {

    }

    /**
     * Invoked when a mouse button has been released on a component.
     *
     * @param e
     */
    @Override
    public void mouseReleased(MouseEvent e) {

    }

    /**
     * Invoked when the mouse enters a component.
     *
     * @param e
     */
    @Override
    public void mouseEntered(MouseEvent e) {

    }

    /**
     * Invoked when the mouse exits a component.
     *
     * @param e
     */
    @Override
    public void mouseExited(MouseEvent e) {

    }
}

注意:有些代码的实现要用到糊涂包

读档信息存储:

import java.io.Serializable;

public class Gameinfo implements Serializable {
private int[][]data;
private int x=0;
private int y=0;
private String path;
private int step;

 public Gameinfo(){

    }


    public Gameinfo(int[][] data, int x, int y, String path, int step) {
        this.data = data;
        this.x = x;
        this.y = y;
        this.path = path;
        this.step = step;
    }

    /**
     * 获取
     * @return data
     */
    public int[][] getData() {
        return data;
    }

    /**
     * 设置
     * @param data
     */
    public void setData(int[][] data) {
        this.data = data;
    }

    /**
     * 获取
     * @return x
     */
    public int getX() {
        return x;
    }

    /**
     * 设置
     * @param x
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * 获取
     * @return y
     */
    public int getY() {
        return y;
    }

    /**
     * 设置
     * @param y
     */
    public void setY(int y) {
        this.y = y;
    }

    /**
     * 获取
     * @return path
     */
    public String getPath() {
        return path;
    }

    /**
     * 设置
     * @param path
     */
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * 获取
     * @return step
     */
    public int getStep() {
        return step;
    }

    /**
     * 设置
     * @param step
     */
    public void setStep(int step) {
        this.step = step;
    }

    public String toString() {
        return "Gameinfo{data = " + data + ", x = " + x + ", y = " + y + ", path = " + path + ", step = " + step + "}";
    }
}

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值