Java窗口的猜數字游戲
1️⃣ 2️⃣ 3️⃣ 4️⃣ 5️⃣ 6️⃣ 7️⃣ 8️⃣ ...
最近學習java看到了猜數字游戲,想自己照着寫,結果找的代碼有問題,就想着自己能模仿改寫一個。
最開始找的代碼 ⬇️
import javax.swing.JOptionPane;
public class GuessGame{
public static void main(String[] args){
boolean guessflag = false;
int realnumber = (int)(Math.random()*100+1);
int guessnumber = 0;
int count = 0;
while(guessflag != true && count < 3 ){
guessnumber = Integer.parseInt(JOptionPane.showInputDialog("請輸入一個整數進行試猜,注意允許競猜的最大次數是3次!",new Integer(guessnumber)));
if(guessnumber > realnumber){
count++;
System.out.println("您輸入的數字太大了,請重新猜!");
}
else if(guessnumber < realnumber){
count++;
System.out.println("您輸入的數字太小了,請重新猜!");
}
else{
count++;
System.out.println("恭喜您猜對了,您共猜了 "+count+" 次。" );
}
}
if(guessflag != true && count == 3) System.out.println("您共猜了 "+count+" 次,已經超過了允許競猜的最大次數!游戲結束!");
}
}
我的java版本是14的,運行結果提示說Swing的JOptionPane過時了。。。
~$ java -version
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment (build 14.0.2+12-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 14.0.2+12-Ubuntu-120.04, mixed mode, sharing)
~$ javac GuessGame.java
Note: GuessGame.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
因為代碼導入的只是Swing包,小白我於是想去擺弄一下,現在開始入坑。。。
第一步:借鑒菜鳥教程
首先得熟悉一下Swing,去教程網比較容易找到基本方法。
菜鳥教程地址【點擊這里】
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class SwingLoginExample {
public static void main(String[] args) {
// 創建 JFrame 實例
JFrame frame = new JFrame("Login Example");
// Setting the width and height of frame
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* 創建面板,這個類似於 HTML 的 div 標簽
* 我們可以創建多個面板並在 JFrame 中指定位置
* 面板中我們可以添加文本字段,按鈕及其他組件。
*/
JPanel panel = new JPanel();
// 添加面板
frame.add(panel);
/*
* 調用用戶定義的方法並添加組件到面板
*/
placeComponents(panel);
// 設置界面可見
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
/* 布局部分我們這邊不多做介紹
* 這邊設置布局為 null
*/
panel.setLayout(null);
// 創建 JLabel
JLabel userLabel = new JLabel("User:");
/* 這個方法定義了組件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
/*
* 創建文本域用於用戶輸入
*/
JTextField userText = new JTextField(20);
userText.setBounds(100,20,165,25);
panel.add(userText);
// 輸入密碼的文本域
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
/*
*這個類似用於輸入的文本域
* 但是輸入的信息會以點號代替,用於包含密碼的安全性
*/
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
// 創建登錄按鈕
JButton loginButton = new JButton("login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
}
}
通過上面的代碼,我們最基本可以得到兩樣東西,輸入框、標簽和確定按鈕(乍一看,感覺是為我准備的呀!)
第二步: 做流程圖 和 一些代碼分析
-
流程圖
我在Ubutnu里面使用的流程圖軟件叫做Dia,如果有更好的希望可以推薦一下Y(附上我的潦草流程圖)。
-
一些代碼分析
import javax.swing.JButton;
import ...;
//導入Swing的所需組件;
JFrame frame = new JFrame("窗口標題");
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JFrame窗體是一個容器,承載Swing各個組件。
//這里創建了一個窗口,frame.setSize設置大小。
//當關閉窗口時,關閉java虛擬機(結束程序)。
placeComponents(panel);
//panel的布局部分
第三步: 補充 和 修改
[-] 導入部分的import javax.swing.*代替原來的一堆 import javax.swing.xx,方便以后添加組件。
[-] 刪除密碼部分。
[+] 通過文本框接收用戶數據。
找到了 JTextField的getText()屬性 獲取用戶數據
[+] 確定按鈕部分,添加事件。
導入事件包 import java.awt.event.ActionEvent; 和import java.awt.event.ActionListener;
使用Button組件的addActionListener()屬性創建事件
[+] 更改標簽內容
使用JLabel組件的setText()屬性更改標簽內容
[+] 讓輸入框里面居中,清除內容
JTextFiel組件的setHorizontalAlignment(JTextField.CENTER)屬性讓內部居中
通過JTextFiel的setText("")屬性將值改為空值
完成代碼(里面有很多BUG)⬇️
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//導入相應的包
public class U3_5_3 {
public static int couBut = 0;
//創建一個公共整形coutBut用戶控制輸入次數
public static void main(String[] args) {
/*創建 JFrame 實例,設置寬400,高200
*設置窗口關閉退出虛擬機。
*添加面板
*調用用戶定義的方法並添加組件到面板,窗口顯示
*panel的布局部分
*設置界面可見
*/
JFrame frame = new JFrame("*猜數字游戲*");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
/* 布局部分,設置布局為 空值
*創建 JLabel,初始化內容
*/
panel.setLayout(null);
JLabel userLabel = new JLabel("請輸入一個100內整數進行試猜,注意允許競猜的最大次數是5次!", SwingConstants.CENTER);
/* 這個方法定義了組件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
userLabel.setBounds(10,20,360,25);
//將標簽添加進面板
panel.add(userLabel);
/*創建文本域用於用戶輸入設置寬度為20
*設置文本域位置
*添加進面板
*讓文本域內容居中
*/
JTextField userText = new JTextField(20);
userText.setBounds(160,50,80,25);
panel.add(userText);
userText.setHorizontalAlignment(JTextField.CENTER);
/*創建登錄按鈕
*設置按鈕位置
*將按鈕添加進面板
*/
JButton loginButton = new JButton("確定");
loginButton.setBounds(160, 80, 80, 25);
panel.add(loginButton);
//設置JButton的addActionListener()屬性
loginButton.addActionListener(new ActionListener() {
/*創建鼠標單擊按鈕事件*/
public void actionPerformed(ActionEvent actionEvent) {
//猜的次數加1
couBut += 1;
//創建整形guessnum接收用戶數據
int guessnum;
//通過JTextField的getText()屬性接收用戶數據轉換為整形
guessnum = Integer.parseInt(userText.getText());
//生成隨機數
int realnumber = (int)(Math.random()*100+1);
//控制猜的次數為5
if(couBut > 4 && couBut < 6){
userLabel.setText("您猜 5 次了都沒猜對,這不怪你,程序就是這么扣!");
userText.setText("");
}
//如果超出指定次數退出程序
else if(couBut > 4){
System.exit(0);
}
else if(guessnum > realnumber){
userLabel.setText("您輸入的數字太大了,請重新競猜!");
userText.setText("");
}
else if(guessnum < realnumber){
userLabel.setText("您輸入的數字太小了,請重新競猜!");
userText.setText("");
}
//用戶猜對了,退出游戲
else{
userLabel.setText("這么摳門的游戲,你竟然猜對了,這程序怕是你寫的吧!");
couBut = 6;
}
}
});
}
}
效果圖
小實例的總結
1.學習了用Swing包創建窗口
2.知道如何向窗口添加標簽、輸入框、密碼框
3.懂得了創建鼠標單擊按鈕事件的方法