嗨,StackOverflow的人,我对Java很新,需要一些帮助, 我制作了一个程序来编译我想要的程序,一个猜谜游戏,计算机随机生成一个数字,并要求用户猜测它,但这完全在控制台中。我希望为游戏创建一个GUI,我已经在代码中制作了GUI的大纲,但我很难将逻辑放入其中,我最好从控制台程序中调用方法(并进行一些更改)或启动从GUI代码中的顶部开始并将逻辑放在那里?如果是后者,我把逻辑放在哪里?任何帮助将不胜感激!
到目前为止,这是我的两个程序,两个程序都已编译,但GUI现在没有任何意义!
提前谢谢
此代码是控制台代码,扫描用户的猜测并与随机数进行比较。
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class GuessingGameSystem {
public static int randomizer() {
Random rand = new Random();
int num = rand.nextInt(10)+1;
System.out.println(num);
return num;
}
public static int userInput() {
System.out.println("I've thought of a number between 1 and 10");
System.out.println("Enter your guess...");
Scanner scan = new Scanner(System.in);
int guess = scan.nextInt();
return guess;
}
public static String compare(int a, int b) {
String result = null;
if (a < b) {
result = "Higher!";
}
else if (a > b) {
result = "Lower!";
}
else {
result = "You guessed it - I was thinking of " + b;
}
return result;
}
public static void main(String[] args) {
Scanner scanLine = new Scanner(System.in);
String playAgain = "";
int count = 0;
int a;
int b;
do {
b= randomizer();
count = 0;
ArrayList<Integer> guessesSoFar = new ArrayList<>();
do {
a = userInput();
count++;
guessesSoFar.add(a);
System.out.println("Guesses so far: " + Arrays.toString(guessesSoFar.toArray()));
System.out.println("Number of guesses so far: " + guessesSoFar.size());
System.out.println(compare(a,b));
} while (a != b);
System.out.println("It took you " + count + " guesses.");
System.out.println("Play again? Yes/No");
playAgain = scanLine.nextLine();
} while (playAgain.equalsIgnoreCase("yes") || playAgain.equalsIgnoreCase("y"));
}
}
这是我到目前为止的GUI代码:
import javax.swing.*;
import java.awt.event.*;
public class GuessingGameGUI {
private JFrame frame;
private JPanel panel;
private JLabel lblInstructions, lblResult, lblGuesses, lblNoOfGuesses;
private JButton btnCheck, btnNewGame, btnExit;
private JTextField txtUserGuess, txtListOfGuesses, txtGuessesCount;
public static void main(String[] args) {
new GuessingGameGUI();
}
public GuessingGameGUI() {
createForm();
createFields();
createButtons();
createTextField();
frame.add(panel);
frame.setVisible(true);
}
public void createForm() {
frame = new JFrame();
frame.setTitle("Guessing Game");
frame.setSize(800,350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
}
public void createFields() {
lblInstructions = new JLabel("Guess the number between 1 and 10:");
lblInstructions.setBounds(285, 50, 350, 20);
panel.add(lblInstructions);
lblResult = new JLabel("Result");
lblResult.setBounds(380, 200, 100, 20);
panel.add(lblResult);
lblGuesses = new JLabel("Your Guesses:");
lblGuesses.setBounds(70, 50, 100, 20);
panel.add(lblGuesses);
lblNoOfGuesses = new JLabel("Number of Guesses:");
lblNoOfGuesses.setBounds(600, 50, 200, 20);
panel.add(lblNoOfGuesses);
}
public void createButtons() {
btnCheck = new JButton("Check");
btnCheck.setBounds(325, 150, 150, 20);
panel.add(btnCheck);
btnNewGame = new JButton("New Game");
btnNewGame.setBounds(160, 300, 150, 20);
panel.add(btnNewGame);
btnExit = new JButton("Exit");
btnExit.setBounds(495, 300, 150, 20);
panel.add(btnExit);
btnCheck.addActionListener(new CheckHandler());
btnNewGame.addActionListener(new NewGameHandler());
btnExit.addActionListener(new ExitHandler());
}
public void createTextField() {
txtUserGuess = new JTextField();
txtUserGuess.setBounds(300, 100, 200, 20);
panel.add(txtUserGuess);
txtListOfGuesses = new JTextField("List of Guesses");
txtListOfGuesses.setBounds(65, 75, 115, 200);
panel.add(txtListOfGuesses);
txtGuessesCount = new JTextField("Guesses Count");
txtGuessesCount.setBounds(610,75, 110, 20);
panel.add(txtGuessesCount);
}
class CheckHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
//to check random number against user guess in txtUserGuess
}
}
class NewGameHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
//to start a new game
}
}
class ExitHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
}
答案 0 :(得分:0)
你应该看看MVP(模型视图演示者)。
GUI代码不应该具有游戏的逻辑 - 你不应该调用定义游戏行为的方法,只能向前传递用户命令。
以下是使用MVP实现它的可能方式的概要(A> B表示A调用B&#39方法; GUI是视图):
onCheckCommand(int number)
receiveGuess(int number)
HIGHER
,LOWER
或RIGHT
getListOfGuesses()
和getGuessesCount()
showListOfGuesses(List<Integer> list)
,showGuessesCount(int count)
和showResult(Result result)
(HIGHER
,LOWER
或RIGHT
)
RIGHT
,请停用&#34;检查&#34;按钮onNewGameCommand()
startNewGame()
基本上,演示者是谁知道应该调用哪些游戏方法(以及何时)以及应该显示什么(以及何时)。 模型应该有一个公共方法,用于响应用户输入或其他外部事件(在任何情况下)发生的事情(在游戏中) - 例如,每秒触发一次更新剩余时间的计时器。
我假设视图强制用户输入整数。您应该小心调用更新GUI on the event dispatching thread的方法。在这个例子中已经发生了这种情况,因为在按下&#34;按钮时会调用所有方法。事件。从处理程序调用所有内容并不总是有效。