🍬 博主介紹👨?🎓 博主介紹:大家好,我是 hacker-routing ,很高興認識大家~
?主攻領域:【滲透領域】【應急響應】 【Java】 【VulnHub靶場復現】【面試分析】
🎉點贊?評論?收藏 == 養成習慣(一鍵三連)😋
🎉歡迎關注💗一起學習👍一起討論??一起進步📝文末有彩蛋
🙏作者水平有限,歡迎各位大佬指點,相互學習進步!
目錄
拼圖小游戲
練習
創建主界面1
代碼
創建主界面2
菜單制作
在游戲界面中添加菜單
代碼
添加圖片
游戲主界面添加組件
代碼
拼圖小游戲
練習
創建主界面1
- 到idea中創建一個寬603像素,高680像素的游戲主界面
- 到idea中創建一個寬488像素,高430像素的登錄界面
- 到idea中創建一個寬488像素,高500像素的注冊界面
代碼
測試類:test ,在這個測試類中,我們直接把三個Java用戶圖形化界面生成了,但是這樣三個功能界面全部都寫在同一個main函數里面,對于我們以后的代碼修改很不方便。
所以我們這里進行修改下,分別寫成單獨的類中。
package ui;import javax.swing.*;public class test {public static void main(String[] args) {//JFrame是JavaBean類描述界面的//屬性 (寬 高) 行為//1.創建一個游戲的主界面JFrame gameJFrame = new JFrame();gameJFrame.setSize(603,680);//單位是像素gameJFrame.setVisible(true);//2.創建一個登陸界面JFrame loginJFrame = new JFrame();loginJFrame.setSize(488,430);loginJFrame.setVisible(true);//3.創建一個注冊界面JFrame registerJFrame = new JFrame();registerJFrame.setSize(488,500);registerJFrame.setVisible(true);}
}
注冊界面:RegisterJFrame
package ui;import javax.swing.*;public class RegisterJFrame extends JFrame {//跟相關注冊界面的代碼,都寫里面public RegisterJFrame(){this.setSize(488,500);this.setVisible(true);}}
登錄界面:loginJFrame
package ui;import javax.swing.*;public class loginJFrame extends JFrame {//loginJFrame 表示登錄界面//以后所以跟登錄相關的代碼,都寫里面public loginJFrame(){this.setSize(488,430);this.setVisible(true);}}
游戲界面:GameJFrame
package ui;import javax.swing.*;public class GameJFrame extends JFrame {//JFrame 界面,窗體//子類呢?也表示界面,窗體//規定:GameJFrame這個界面表示的就是游戲的主界面//以后跟游戲相關的所有邏輯都寫在這個類中public GameJFrame(){this.setSize(603,680);//單位是像素this.setVisible(true);}}
程序的啟動入口:App
我們可以把test這個類刪掉了,我們可以直接俄利用App這個程序的啟動入口,我們需要啟動哪個界面,我們直接創建誰就可以了。
import ui.GameJFrame;
import ui.RegisterJFrame;
import ui.loginJFrame;public class App {public static void main(String[] args) {//表示程序的啟動入口//如果我們想要開啟一個界面,就創建誰的對象就好了new RegisterJFrame();new GameJFrame();new loginJFrame();}
}
創建主界面2
簡單初始化界面
public RegisterJFrame(){this.setSize(488,500);//設置界面的標題this.setTitle("拼圖 注冊");//設置界面置頂this.setAlwaysOnTop(true);//設置界面居中this.setLocationRelativeTo(null);//設置關閉模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//讓界面顯示出來this.setVisible(true);
菜單制作
在游戲界面中添加菜單
代碼
游戲界面:GameJFrame
package ui;import javax.swing.*;public class GameJFrame extends JFrame {//JFrame 界面,窗體//子類呢?也表示界面,窗體//規定:GameJFrame這個界面表示的就是游戲的主界面//以后跟游戲相關的所有邏輯都寫在這個類中public GameJFrame(){//初始化界面initJFrame();//初始化菜單initJMenuBar();//讓界面顯示出來,最后寫this.setVisible(true);}private void initJMenuBar() {//初始化菜單//創建整個的菜單對象JMenuBar jMenuBar = new JMenuBar();//創建菜單上面的兩個選項的對象 (功能 關于我們)JMenu fuctionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("關于我們");//創建選項下面的條目對象JMenuItem replayItem = new JMenuItem("重新游戲");JMenuItem reloginItem = new JMenuItem("重新登錄");JMenuItem closeItem = new JMenuItem("關閉游戲");JMenuItem accountItem = new JMenuItem("公眾號");//將每一個選項下的條目添加到對應的選項中fuctionJMenu.add(replayItem);fuctionJMenu.add(reloginItem);fuctionJMenu.add(closeItem);aboutJMenu.add(accountItem);//將菜單里面的兩個選項添加到菜單中jMenuBar.add(fuctionJMenu);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);}}
測試類:App
import ui.GameJFrame;
import ui.RegisterJFrame;
import ui.loginJFrame;public class App {public static void main(String[] args) {//表示程序的啟動入口//如果我們想要開啟一個界面,就創建誰的對象就好了new GameJFrame();
// new RegisterJFrame();
// new loginJFrame();}
}
添加圖片
- 默認添加圖片顯示在正中央
多寫一個初始化圖片
package ui;import javax.swing.*;public class GameJFrame extends JFrame {//JFrame 界面,窗體//子類呢?也表示界面,窗體//規定:GameJFrame這個界面表示的就是游戲的主界面//以后跟游戲相關的所有邏輯都寫在這個類中public GameJFrame(){//初始化界面initJFrame();//初始化菜單initJMenuBar();//初始化圖片initimage();//讓界面顯示出來,最后寫mthis.setVisible(true);}//---------------------------------- ---------------------//初始化圖片private void initimage() {//1.創建一個圖片imageicon的對象ImageIcon icon = new ImageIcon("E:\\tool\\IDEA-java\\java代碼\\routing\\image\\animal\\animal3\\3.jpg");//2.創建一個Jlabel的對象(管理容器)JLabel JLabel = new JLabel(icon);//3.把管理容器添加到界面中this.add(JLabel);}private void initJMenuBar() {//初始化菜單//創建整個的菜單對象JMenuBar jMenuBar = new JMenuBar();//創建菜單上面的兩個選項的對象 (功能 關于我們)JMenu fuctionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("關于我們");//創建選項下面的條目對象JMenuItem replayItem = new JMenuItem("重新游戲");JMenuItem reloginItem = new JMenuItem("重新登錄");JMenuItem closeItem = new JMenuItem("關閉游戲");JMenuItem accountItem = new JMenuItem("公眾號");//將每一個選項下的條目添加到對應的選項中fuctionJMenu.add(replayItem);fuctionJMenu.add(reloginItem);fuctionJMenu.add(closeItem);aboutJMenu.add(accountItem);//將菜單里面的兩個選項添加到菜單中jMenuBar.add(fuctionJMenu);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);}}
app運行:
import ui.GameJFrame;
import ui.RegisterJFrame;
import ui.loginJFrame;public class App {public static void main(String[] args) {//表示程序的啟動入口//如果我們想要開啟一個界面,就創建誰的對象就好了new GameJFrame();
// new RegisterJFrame();
// new loginJFrame();}
}
游戲主界面添加組件
代碼
//初始化圖片private void initimage() {//外循環 --把內循環重復執行了4次int number = 1;for (int i = 0; i < 4; i++) {//內循環 --表示在一行添加4張圖片for (int j = 0; j < 4; j++) {//1.創建一個Jlabel的對象(管理容器)JLabel JLabel = new JLabel(new ImageIcon("E:\\\\tool\\\\IDEA-java\\\\java代碼\\\\routing\\\\image\\\\animal\\\\animal3\\\\" + number +".jpg"));//2.指定圖片的位置JLabel.setBounds(105 * i,105 * j,105,105);//3.把管理容器添加到界面中this.getContentPane().add(JLabel);number++;}}
App 運行
import ui.GameJFrame;
import ui.RegisterJFrame;
import ui.loginJFrame;public class App {public static void main(String[] args) {//表示程序的啟動入口//如果我們想要開啟一個界面,就創建誰的對象就好了new GameJFrame();
// new RegisterJFrame();
// new loginJFrame();}
}