溫嶺做網(wǎng)站新冠疫情最新消息今天
Java學(xué)習(xí)筆記(新手純小白向)
第一章?JAVA基礎(chǔ)概念
第二章?JAVA安裝和環(huán)境配置
第三章?IntelliJ IDEA安裝
第四章?運算符
第五章 運算符聯(lián)系
第六章?判斷與循環(huán)
第七章?判斷與循環(huán)練習(xí)
第八章 循環(huán)高級綜合
第九章 數(shù)組介紹及其內(nèi)存圖
第十章 數(shù)組基礎(chǔ)練習(xí)
第十一章 方法基礎(chǔ)及簡單應(yīng)用
第十二章 方法基礎(chǔ)練習(xí)
第十三章 前續(xù)知識綜合練習(xí)
第十四章 面向?qū)ο蠡A(chǔ)
第十五章 面向?qū)ο缶C合訓(xùn)練
第十六章 字符串基礎(chǔ)
第十七章 字符串基礎(chǔ)練習(xí)
第十八章 ArrayList集合
第十九章 ArrayList集合基礎(chǔ)練習(xí)
第二十章 面向?qū)ο筮M階
第二十一章 面向?qū)ο筮M階基礎(chǔ)練習(xí)
第二十二章 階段項目——拼圖小游戲
目錄
Java學(xué)習(xí)筆記(新手純小白向)
前言
代碼示例
一、UI界面搭建
????????1.游戲主界面
????????2.登錄界面
????????3.注冊界面
二、業(yè)務(wù)邏輯編寫?
????????1.用戶類
????????2.生成驗證碼的工具類
三、游戲主入口
四、游戲打包exe說明
總結(jié)
前言
本篇章主要展示了階段項目——拼圖小游戲,該項目基本包括了之前所學(xué)的所有知識點,可以很好得鞏固所學(xué)知識點。
代碼示例
一、UI界面搭建
????????1.游戲主界面
//創(chuàng)建游戲主界面
public class GameJFrame extends JFrame implements KeyListener, ActionListener {//創(chuàng)建獲取隨機數(shù)的對象Random r = new Random();//定義二維數(shù)組,記錄圖片的編號int[][] imageCode = new int[4][4];//定義二維數(shù)組,記錄勝利后圖片的編號int[][] win = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定義變量x和y,記錄空白方塊在二維數(shù)組中的位置int x = 0;int y = 0;//定義變量path,記錄當(dāng)前加載圖片的相對路徑String path = "gigsawgame\\image\\animal\\animal1\\";//定義變量step,記錄移動的步數(shù)int step = 0;//創(chuàng)建選項下的條目對象JMenuItem replayItem = new JMenuItem("重新游戲");JMenuItem reLoginItem = new JMenuItem("重新登錄");JMenuItem closeItem = new JMenuItem("關(guān)閉游戲");JMenuItem rewardItem = new JMenuItem("謝謝打賞");JMenuItem girlItem = new JMenuItem("美女");JMenuItem animalItem = new JMenuItem("動物");JMenuItem sportsItem = new JMenuItem("運動");//定義空參構(gòu)造,對界面進行初始化public GameJFrame() {//初始化游戲主界面initJFrame();//初始化菜單initJMenuBar();//初始化數(shù)據(jù)initImageCode();//初始化圖片initImage();//設(shè)置界面是否顯示,建議寫在最后this.setVisible(true);}//打亂圖片編號private void initImageCode() {//定義一維數(shù)組,記錄圖片編號int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};//打亂數(shù)組中號碼的順序for (int i = 0; i < tempArr.length; i++) {//獲取隨機索引int index = r.nextInt(tempArr.length);//進行數(shù)據(jù)交換int temp = tempArr[i];tempArr[i] = tempArr[index];tempArr[index] = temp;}//定義變量index,記錄一維數(shù)組的索引int index = 0;//將打亂后的一維數(shù)組中的編號,添加到二維數(shù)組中for (int i = 0; i < imageCode.length; i++) {for (int j = 0; j < imageCode[i].length; j++) {if (tempArr[index] == 0) {x = i;y = j;}imageCode[i][j] = tempArr[index];index++;}}}//將圖片加載到界面中private void initImage() {//清空已經(jīng)出現(xiàn)的圖片this.getContentPane().removeAll();//如果勝利,加載勝利圖片if (victory()) {JLabel winJLabel = new JLabel(new ImageIcon("gigsawgame\\image\\win.png"));winJLabel.setBounds(203, 283, 197, 73);this.getContentPane().add(winJLabel);}//加載步數(shù)計數(shù)器JLabel stepCount = new JLabel("步數(shù):" + step);stepCount.setBounds(50, 30, 100, 20);this.getContentPane().add(stepCount);//使用循環(huán)添加每一行圖片for (int i = 0; i < 4; i++) {//使用循壞添加每一列圖片for (int j = 0; j < 4; j++) {//定義變量code,記錄圖片編號int code = imageCode[i][j];//創(chuàng)建ImageIcon圖片對象,并創(chuàng)建JLabel管理容器JLabel jLabel = new JLabel(new ImageIcon(path+ code + ".jpg"));//指定圖片位置jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);//給圖片添加邊框jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//將JLabel管理容器添加到界面中(將JLabel管理容器添加到隱藏容器之中)this.getContentPane().add(jLabel);}}//添加背景圖片(后添加的在下方)JLabel background = new JLabel(new ImageIcon("gigsawgame\\image\\background.png"));background.setBounds(40, 40, 508, 560);this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();}//設(shè)置菜單欄基礎(chǔ)信息private void initJMenuBar() {//創(chuàng)建菜單欄對象JMenuBar jMenuBar = new JMenuBar();//創(chuàng)建菜單上面的兩個選項:功能、關(guān)于我們JMenu functionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("關(guān)于我們");//創(chuàng)建功能選項中的更換圖片選項JMenu replaceImage = new JMenu("更換圖片");//匹配選項與條目//匹配功能選項與其條目functionJMenu.add(replaceImage);functionJMenu.add(replayItem);functionJMenu.add(reLoginItem);functionJMenu.add(closeItem);//匹配功能選項下的更換圖片選項與其條目replaceImage.add(girlItem);replaceImage.add(animalItem);replaceImage.add(sportsItem);//匹配關(guān)于我們選項與其條目aboutJMenu.add(rewardItem);//給條目綁定事件replayItem.addActionListener(this);reLoginItem.addActionListener(this);closeItem.addActionListener(this);rewardItem.addActionListener(this);girlItem.addActionListener(this);animalItem.addActionListener(this);sportsItem.addActionListener(this);//將選項添加到菜單欄中jMenuBar.add(functionJMenu);jMenuBar.add(aboutJMenu);//設(shè)置界面菜單欄this.setJMenuBar(jMenuBar);}//設(shè)置界面基礎(chǔ)信息private void initJFrame() {//設(shè)置界面的寬高this.setSize(603, 680);//設(shè)置界面的標(biāo)題this.setTitle("拼圖單機版 v1.0");//設(shè)置界面置頂this.setAlwaysOnTop(true);//設(shè)置界面居中this.setLocationRelativeTo(null);//設(shè)置關(guān)閉模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默認(rèn)的居中放置this.setLayout(null);//添加游戲操作(給整個界面添加鍵盤監(jiān)聽事件)this.addKeyListener(this);}//空實現(xiàn)keyTyped方法@Overridepublic void keyTyped(KeyEvent e) {}//實現(xiàn)鍵盤按下不松按鍵操作方法,查看完整圖片@Overridepublic void keyPressed(KeyEvent e) {//定義變量keyCode,記錄按下按鍵的值int keyCode = e.getKeyCode();if (keyCode == 65) {//清空之前的圖片this.getContentPane().removeAll();//加載完整圖片JLabel jLabel = new JLabel(new ImageIcon(path + "all.jpg"));jLabel.setBounds(83, 134, 420, 420);this.getContentPane().add(jLabel);//加載背景圖片JLabel background = new JLabel(new ImageIcon("gigsawgame\\image\\background.png"));background.setBounds(40, 40, 508, 560);this.getContentPane().add(background);}//重新加載圖片this.getContentPane().repaint();}//實現(xiàn)鍵盤松開按鍵操作方法,使圖片可以左上右下移動及一鍵通關(guān)@Overridepublic void keyReleased(KeyEvent e) {//如果游戲勝利,則結(jié)束方法if (victory()) {return;}//定義變量keyCode,記錄按鍵的值int keyCode = e.getKeyCode();//對按鍵進行判斷,做出對應(yīng)行為switch (keyCode) {//左鍵操作case 37 -> {//判斷是否為空白方塊是否位于最右側(cè)if (y == 3) {return;} else {imageCode[x][y] = imageCode[x][y + 1];imageCode[x][y + 1] = 0;y++;//移動一次,步數(shù)增加一次step++;}//調(diào)用方法initImage,重新加載圖片initImage();}//上鍵操作case 38 -> {//判斷是否為空白方塊是否位于最下側(cè)if (x == 3) {return;} else {imageCode[x][y] = imageCode[x + 1][y];imageCode[x + 1][y] = 0;x++;//移動一次,步數(shù)增加一次step++;}//調(diào)用方法initImage,重新加載圖片initImage();}//右鍵操作case 39 -> {//判斷是否為空白方塊是否位于最左側(cè)if (y == 0) {return;} else {imageCode[x][y] = imageCode[x][y - 1];imageCode[x][y - 1] = 0;y--;//移動一次,步數(shù)增加一次step++;}//調(diào)用方法initImage,重新加載圖片initImage();}//下鍵操作case 40 -> {//判斷是否為空白方塊是否位于最上側(cè)if (x == 0) {return;} else {imageCode[x][y] = imageCode[x - 1][y];imageCode[x - 1][y] = 0;x--;//移動一次,步數(shù)增加一次step++;}//調(diào)用方法initImage,重新加載圖片initImage();}//松開A鍵,恢復(fù)之前的打亂效果case 65 -> {initImage();}//松開按鍵W,實現(xiàn)一鍵通關(guān)case 87 -> {imageCode = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};initImage();}}}//定義方法victory,判斷游戲是否勝利public boolean victory() {for (int i = 0; i < imageCode.length; i++) {for (int j = 0; j < imageCode[i].length; j++) {if (imageCode[i][j] != win[i][j]) {return false;}}}return true;}//給菜單欄中選項下的條目對象綁定事件@Overridepublic void actionPerformed(ActionEvent e) {//獲取被點擊的條目對象Object clickItem = e.getSource();//判斷并執(zhí)行if (clickItem == replayItem) {//步數(shù)歸零step = 0;//重新打亂圖片編號initImageCode();//重新加載圖片initImage();} else if (clickItem == reLoginItem) {//關(guān)閉當(dāng)前游戲界面this.setVisible(false);//打開登錄界面new LoginJFrame();} else if (clickItem == closeItem) {//關(guān)閉虛擬機System.exit(0);} else if (clickItem == rewardItem) {//創(chuàng)建一個彈框?qū)ο驤Dialog jDialog = new JDialog();//創(chuàng)建圖片管理容器JLabelJLabel jLabel = new JLabel(new ImageIcon("gigsawgame\\image\\reward.jpg"));//設(shè)置管理容器的位置和寬高jLabel.setBounds(0, 0, 514, 514);//把管理容器添加到彈框中jDialog.getContentPane().add(jLabel);//設(shè)置彈框的大小jDialog.setSize(600, 600);//設(shè)置彈框置頂jDialog.setAlwaysOnTop(true);//設(shè)置彈框居中jDialog.setLocationRelativeTo(null);//彈窗不關(guān)閉,則無法操作下面的界面jDialog.setModal(true);//讓彈窗顯示jDialog.setVisible(true);} else if (clickItem == girlItem) {//步數(shù)歸零step = 0;//隨機獲取美女圖片編號int beautyCode = r.nextInt(13) + 1;path = "gigsawgame\\image\\girl\\girl" + beautyCode + "\\";//重新打亂圖片編號initImageCode();//重新加載圖片initImage();} else if (clickItem == animalItem) {//步數(shù)歸零step = 0;//隨機獲取動物圖片編號int animalCode = r.nextInt(8) + 1;path = "gigsawgame\\image\\animal\\animal" + animalCode + "\\";//重新打亂圖片編號initImageCode();//重新加載圖片initImage();} else if (clickItem == sportsItem) {//步數(shù)歸零step = 0;//隨機獲取運動圖片編號int sportsCode = r.nextInt(10) + 1;path = "gigsawgame\\image\\sport\\sport" + sportsCode + "\\";//重新打亂圖片編號initImageCode();//重新加載圖片initImage();}}
}
????????2.登錄界面
//創(chuàng)建登錄界面
public class LoginJFrame extends JFrame implements MouseListener {//數(shù)據(jù)初始化//創(chuàng)建集合user,存儲正確的用戶名和密碼static ArrayList<User> users = new ArrayList<>();static {users.add(new User("張三", "123"));users.add(new User("李四", "abc"));}//加載用戶名、密碼和驗證碼輸入框JTextField usernameField = new JTextField();JPasswordField passwordField = new JPasswordField();JTextField iCodeField = new JTextField();//加載顯示密碼按鈕、登錄按鈕、注冊按鈕和驗證碼內(nèi)容管理器JButton passwordVisible = new JButton();JButton loginButton = new JButton();JButton registerButton = new JButton();JLabel rightCode = new JLabel();//加載正確驗證碼String rightICode = ICodeUtil.getCode();//定義空參構(gòu)造,對界面進行初始化public LoginJFrame() {//初始化登錄主界面initJFrame();//初始化圖片、輸入框及按鈕initTextButton();//設(shè)置界面是否顯示,建議寫在最后this.setVisible(true);}//設(shè)置輸入框、按鈕及圖片private void initTextButton() {//清空已經(jīng)出現(xiàn)的圖片、輸入框及按鈕this.getContentPane().removeAll();//加載輸入框基礎(chǔ)圖片JLabel usernameImage = new JLabel(new ImageIcon("gigsawgame\\image\\login\\用戶名.png"));usernameImage.setBounds(105, 129, 47, 17);JLabel passwordImage = new JLabel(new ImageIcon("gigsawgame\\image\\login\\密碼.png"));passwordImage.setBounds(112, 202, 32, 16);JLabel iCodeImage = new JLabel(new ImageIcon("gigsawgame\\image\\login\\驗證碼.png"));iCodeImage.setBounds(100, 274, 56, 21);//設(shè)置用戶名、密碼和驗證碼輸入框的基礎(chǔ)信息usernameField.setBounds(200, 123, 150, 29);passwordField.setBounds(200, 196, 150, 29);iCodeField.setBounds(200, 270, 75, 29);//設(shè)置正確驗證碼內(nèi)容管理器的基礎(chǔ)信息rightCode.setText(rightICode);rightCode.setBounds(275, 270, 75, 29);//設(shè)置顯示密碼按鈕、登錄按鈕和注冊按鈕的基礎(chǔ)信息passwordVisible.setBounds(332, 196, 18, 29);passwordVisible.setIcon(new ImageIcon("gigsawgame\\image\\login\\顯示密碼.png"));loginButton.setBounds(64, 320, 128, 47);loginButton.setIcon(new ImageIcon("gigsawgame\\image\\login\\登錄按鈕.png"));registerButton.setBounds(275, 320, 128, 47);registerButton.setIcon(new ImageIcon("gigsawgame\\image\\login\\注冊按鈕.png"));//去除按鈕的默認(rèn)邊框及背景passwordVisible.setBorderPainted(false);passwordVisible.setContentAreaFilled(false);loginButton.setBorderPainted(false);loginButton.setContentAreaFilled(false);registerButton.setBorderPainted(false);registerButton.setContentAreaFilled(false);//加載背景圖片JLabel background = new JLabel(new ImageIcon("gigsawgame\\image\\login\\background.png"));background.setBounds(0, 0, 470, 390);//給按鈕及驗證碼內(nèi)容管理器綁定事件rightCode.addMouseListener(this);passwordVisible.addMouseListener(this);loginButton.addMouseListener(this);registerButton.addMouseListener(this);//將圖片、輸入框及按鈕添加到界面中this.getContentPane().add(usernameImage);this.getContentPane().add(usernameField);this.getContentPane().add(passwordImage);this.getContentPane().add(passwordVisible);this.getContentPane().add(passwordField);this.getContentPane().add(iCodeImage);this.getContentPane().add(iCodeField);this.getContentPane().add(rightCode);this.getContentPane().add(loginButton);this.getContentPane().add(registerButton);this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();}//設(shè)置界面基礎(chǔ)信息private void initJFrame() {//設(shè)置界面的寬高this.setSize(488, 430);//設(shè)置界面的標(biāo)題this.setTitle("用戶登錄");//設(shè)置界面置頂this.setAlwaysOnTop(true);//設(shè)置界面居中this.setLocationRelativeTo(null);//設(shè)置關(guān)閉模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默認(rèn)的居中放置this.setLocationRelativeTo(null);}//展示用戶名或密碼錯誤提示彈窗private void tipJDialog(String tip) {//創(chuàng)建彈窗JDialog tipJDialog = new JDialog();//設(shè)置彈窗大小tipJDialog.setSize(200, 150);//設(shè)置彈窗置頂tipJDialog.setAlwaysOnTop(true);//設(shè)置彈窗居中tipJDialog.setLocationRelativeTo(null);//設(shè)置彈窗不關(guān)閉無法操作下方界面tipJDialog.setModal(true);//添加提示內(nèi)容JLabel tips = new JLabel(tip);tips.setBounds(0, 0, 200, 150);tipJDialog.getContentPane().add(tips);//使彈窗可見tipJDialog.setVisible(true);}//實現(xiàn)按鈕的鼠標(biāo)監(jiān)聽事件方法@Overridepublic void mouseClicked(MouseEvent e) {//獲取點擊的按鈕Object source = e.getSource();//根據(jù)點擊按鈕不同,做出相應(yīng)操作if (source == rightCode) {//更換一個新的驗證碼rightICode = ICodeUtil.getCode();rightCode.setText(rightICode);}}@Overridepublic void mousePressed(MouseEvent e) {//獲取按下的按鈕Object source = e.getSource();//根據(jù)按下按鈕不同,做出相應(yīng)操作if (source == passwordVisible) {//切換顯示密碼按鈕的背景圖片passwordVisible.setIcon(new ImageIcon("gigsawgame\\image\\login\\顯示密碼按下.png"));} else if (source == loginButton) {//切換登錄按鈕的背景圖片loginButton.setIcon(new ImageIcon("gigsawgame\\image\\login\\登錄按下.png"));} else if (source == registerButton) {//切換注冊按鈕的背景圖片registerButton.setIcon(new ImageIcon("gigsawgame\\image\\login\\注冊按下.png"));}}@Overridepublic void mouseReleased(MouseEvent e) {//獲取松開的按鈕Object source = e.getSource();//根據(jù)松開按鈕不同,做出相應(yīng)操作if (source == passwordVisible) {//恢復(fù)顯示密碼按鈕的背景圖片passwordVisible.setIcon(new ImageIcon("gigsawgame\\image\\login\\顯示密碼.png"));} else if (source == loginButton) {//獲取用戶輸入的用戶名,密碼,驗證碼String username = usernameField.getText();String password = passwordField.getText();String iCode = iCodeField.getText();//判斷驗證碼是否正確//判斷用戶名和密碼是否為空//判斷用戶名、密碼是否正確if (iCode.equalsIgnoreCase(rightICode)) {if (username.length() != 0 || password.length() != 0) {if (contains(username, password)){new GameJFrame();}else {tipJDialog("用戶名或密碼錯誤");}} else {tipJDialog("用戶名或者密碼為空");}} else {tipJDialog("驗證碼輸入錯誤");//更換一個新的驗證碼rightICode = ICodeUtil.getCode();rightCode.setText(rightICode);}//恢復(fù)登錄按鈕的背景圖片loginButton.setIcon(new ImageIcon("gigsawgame\\image\\login\\登錄按鈕.png"));} else if (source == registerButton) {//恢復(fù)注冊按鈕的背景圖片registerButton.setIcon(new ImageIcon("gigsawgame\\image\\login\\注冊按鈕.png"));}}//判斷用戶在集合中是否存在public boolean contains(String username, String password) {for (int i = 0; i < users.size(); i++) {if (username.equals(users.get(i).getUsername()) && password.equals(users.get(i).getPassword())) {return true;}}return false;}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}
}
????????3.注冊界面
//創(chuàng)建注冊界面
public class RegisterJFrame extends JFrame {//定義空參構(gòu)造,對界面進行初始化public RegisterJFrame() {//初始化注冊主界面initJFrame();//初始化圖片及輸入框}//設(shè)置界面基礎(chǔ)信息private void initJFrame() {//設(shè)置界面的寬高this.setSize(488, 500);//設(shè)置界面的標(biāo)題this.setTitle("用戶注冊");//設(shè)置界面置頂this.setAlwaysOnTop(true);//設(shè)置界面居中this.setLocationRelativeTo(null);//設(shè)置關(guān)閉模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默認(rèn)的居中放置this.setLocationRelativeTo(null);//設(shè)置界面是否顯示,建議寫在最后this.setVisible(true);}
}
二、業(yè)務(wù)邏輯編寫?
????????1.用戶類
public class User {//定義用戶的屬性private String username;private String password;//定義空參構(gòu)造和帶參構(gòu)造public User() {}public User(String username, String password) {this.username = username;this.password = password;}//定義所有成員變量的get和set方法public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
????????2.生成驗證碼的工具類
public class ICodeUtil {//私有化空參構(gòu)造private ICodeUtil() {}//定義靜態(tài)方法getCode,獲取驗證碼public static String getCode() {//創(chuàng)建獲取隨機數(shù)的對象Random r = new Random();//創(chuàng)建字符數(shù)組,存儲驗證碼char[] code = new char[5];//隨機添加一個數(shù)字code[0] = (char) (r.nextInt(10) + 48);//定義字符數(shù)組,存儲大小寫字母char[] letter = new char[52];//將大小寫字母添加進letter字符數(shù)組for (int i = 0; i < letter.length; i++) {if (i >= 26) {letter[i] = (char) (97 + i - 26);} else {letter[i] = (char) (65 + i);}}//隨機將大小寫字母加入code字符數(shù)組for (int i = 1; i < code.length; i++) {code[i] = letter[r.nextInt(letter.length)];}//將數(shù)組中的字符打亂char temp;int index = 0;for (int i = 0; i < code.length; i++) {index = r.nextInt(code.length - 1);temp = code[i];code[i] = code[index];code[index] = temp;}return new String(code);}
}
三、游戲主入口
//程序的啟動入口
public class App {public static void main(String[] args) {//想開啟哪個界面,就創(chuàng)建誰的對象
// new RegisterJFrame();new LoginJFrame();
// new GameJFrame();}
}
四、游戲打包exe說明
? ? ? ? 說明文本及工具在百度網(wǎng)盤中。鏈接: https://pan.baidu.com/s/1QxUXy_1ksPxfZ5xYvJQwnQ?pwd=0806 提取碼: 0806
若過期,請在評論區(qū)留言。
總結(jié)
拼圖小游戲有助于了解Java的GUI相關(guān)知識,但在以后并不常用,了解即可。因此,該項目的重中之重還是游戲的業(yè)務(wù)邏輯,及對所學(xué)知識的鞏固。