首先,我已经搜索了这个。有没有答案?是的,但我似乎无论如何都无法工作。我对Java的经验非常少,所以我不太了解。
无论如何,我的主要课程名为Game
,我也有GameFrame
Game
package xd.winkler.guessing_game;
import java.awt.Canvas;
import java.util.Random;
import java.util.Scanner;
// main game class
public class Game extends Canvas implements Runnable {
// variables
private static final long serialVersionUID = 2723208685741428944L;
public static final int WIDTH = 640, HEIGHT = 480;
public final String TITLE = "Number Guessing Game";
private Thread thread;
Scanner s = new Scanner(System.in);
Random r = new Random();
Boolean running = false, gameOver = false;
static int rounds = 0, tries = 0;;
static int userGuess = 0, randNum = 0;
// creates a new frame called GameFrame
public Game() {
new GameFrame(WIDTH, HEIGHT, TITLE, this);
System.out.println("[" + TITLE + " has started.]");
}
// called in the GameFrame class. starts the thread/game loop
public synchronized void start() {
if (running)
return;
thread = new Thread(this);
thread.start();
running = true;
}
// called at the end of the game loop. stops the program from running
public synchronized void stop() {
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
running = false;
s.close();
}
// game loop
public void run() {
while (running) {
randNum = r.nextInt(10) + 1;
System.out.println("[Random Number: " + randNum + "]");
this.gameOverCheck();
System.out.println("Round: " + rounds);
while (tries < 3) {
tries++;
this.guess();
}
}
stop();
}
// checks if the game is over
public void gameOverCheck() {
if (rounds == 10) {
running = false;
} else {
rounds++;
}
}
// gets the users guess and checks if it's right/wrong
public void guess() {
userGuess = s.nextInt();
if (userGuess == randNum) {
System.out.println("RIGHT!");
} else {
System.out.println("WRONG!");
}
}
// creates a new "Game" method to call/create the GameFrame
public static void main(String[] args) {
new Game();
}
}
GameFrame
package xd.winkler.guessing_game;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.DefaultCaret;
public class GameFrame extends Canvas {
private static final long serialVersionUID = -7329745595446257624L;
static JButton enterButton;
public static JTextArea output;
public static JTextField input;
private static String ENTER = "Enter";
public static String testString = "test";
static JPanel panel;
public GameFrame(int WIDTH, int HEIGHT, String TITLE, Game game) {
JFrame frame = new JFrame(TITLE);
frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.setMaximumSize(new Dimension(WIDTH, HEIGHT));
frame.setMinimumSize(new Dimension(WIDTH, HEIGHT));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
// ---------------------------------------------------
// I did not do this section on my own and it's a mess
// ---------------------------------------------------
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(true);
ButtonListener buttonListener = new ButtonListener();
output = new JTextArea(15, 50);
output.setWrapStyleWord(true);
output.setEditable(false);
JScrollPane scroller = new JScrollPane(output);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JPanel inputpanel = new JPanel();
inputpanel.setLayout(new FlowLayout());
input = new JTextField(20);
enterButton = new JButton("Enter");
enterButton.setActionCommand(ENTER);
enterButton.addActionListener(buttonListener);
// enterButton.setEnabled(false);
input.setActionCommand(ENTER);
input.addActionListener(buttonListener);
DefaultCaret caret = (DefaultCaret) output.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.add(scroller);
inputpanel.add(input);
inputpanel.add(enterButton);
panel.add(inputpanel);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
// ---------------------------------------------------
frame.add(game);
frame.pack();
frame.setVisible(true);
input.requestFocus();
game.start();
}
public static class ButtonListener implements ActionListener {
public void actionPerformed(final ActionEvent ev) {
if (!input.getText().trim().equals("")) {
String cmd = ev.getActionCommand();
if (ENTER.equals(cmd)) {
output.append(input.getText());
if (input.getText().trim().equals(testString))
output.append(" = " + testString);
else
output.append(" != " + testString);
output.append("\n");
}
}
input.setText("");
input.requestFocus();
}
}
}
你可以说游戏还没有完成。我只是乱搞并使用系统输出来测试。我想在Game类中完成所有主要的“游戏内容”,然后输出到我拥有的swing输出文本字段。我还想使用输入框来设置userGuess
。
我不确定如何在两个班级中使用不同的值,而且就像我说的那样,我已经搜索了它并且看起来和吸气者和制定者但是我并不理解它。此外,如果您可以推荐一个很好的教程网站或者其他东西,以便用Java获得很好的知识。