我在过去几周内完成了一些其他简单的GUI项目,没有太多任何问题,但现在我无法在编译后让程序显示出来。我无法弄清楚可能出现的错误,因为没有错误被抛出,而且与我上学的其他项目的格式基本相同。
任何关于在屏幕上获取GUI的指导都将受到赞赏,以便在我将其转入之前我可以通过并调整我想要的内容。
import java.util.Random;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/***********************************************************************
Program Name: ProgramName.java
Programmer's Name: Student Name
Program Description: Describe here what this program will do
***********************************************************************/
public class GuessGame extends JFrame{
//Declare GUI components
private JFrame mainFrame;
private JButton guessButton;
private JButton exitButton;
private JTextField guessField;
private JTextField answerField;
private JLabel directionsLabel;
private JLabel guessLabel;
private JLabel answerLabel;
public GuessGame(){
//Initialize window components
mainFrame = new JFrame("Number Guessing Game");
exitButton = new JButton("Exit");
guessButton = new JButton("Try your luck");
guessField = new JTextField(4);
answerField = new JTextField(50);
guessLabel = new JLabel("What is your guess?");
answerLabel = new JLabel("Now it is/isn't");
directionsLabel = new JLabel("Enter a number and then press the" +
"guess button until you are correct");
//Build the GUI
Container c = mainFrame.getContentPane();
c.setLayout(new FlowLayout());
c.add(directionsLabel);
c.add(guessLabel);
c.add(guessField);
c.add(answerLabel);
c.add(answerField);
c.add(guessButton);
c.add(exitButton);
//Set Mnemonics
guessButton.setMnemonic('G');
exitButton.setMnemonic('E');
mainFrame.setSize(450, 300);
mainFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//Call the handler methods for specific functions
GuessButtonHandler ghandler = new GuessButtonHandler();
guessButton.addActionListener(ghandler);
ExitButtonHandler ehandler = new ExitButtonHandler();
exitButton.addActionListener(ehandler);
FocusHandler fhandler = new FocusHandler();
guessField.addFocusListener(fhandler);
answerField.addFocusListener(fhandler);
}
//Implement actionListener for the Guess button
class GuessButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
String instring;
int counter = 0;
int guess;
Random rand = new Random();
int numberToGuess = rand.nextInt(1000);
instring = guessField.getText();
guess = Integer.parseInt(instring);
counter++;
if (guess == numberToGuess){
answerLabel = new JLabel("You win! " +
"\nThe number was: " + numberToGuess +
"\nIt took you " + counter + " tries");
}
else if (guess < numberToGuess){
answerLabel = new JLabel("Too low");
}
else if (guess > numberToGuess){
answerLabel = new JLabel("Too high");
}
}
}
//Implement ActionListener for the exit button
class ExitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
//Implement focus listener
class FocusHandler implements FocusListener{
public void focusGained(FocusEvent e){
if (e.getSource()== guessField){
answerField.setText("");
}
else if (e.getSource() == answerField){
guessButton.requestFocus();
}
}
public void focusLost(FocusEvent arg0) {
}
}
//Main to run program, call GuessGame method
public static void main(String[] args) {
new GuessGame();
}
}
答案 0 :(得分:4)
在向其添加所有组件后,您需要在JFrame上调用setVisible(true)
。除非你明确告诉它,否则该程序不会在数学上知道你想要显示任何东西。
即,
public GuessGame() {
// Initialize window components
mainFrame = new JFrame("Number Guessing Game");
// ..... etc....
FocusHandler fhandler = new FocusHandler();
guessField.addFocusListener(fhandler);
answerField.addFocusListener(fhandler);
mainFrame.setVisible(true);
}
此外,您不希望此类扩展JFrame,因为这不是必需的。您已经在构造函数中使用了JFrame,因此不需要在类本身中使用另一个JFrame。