1

在过去的几周里,我做了一些其他简单的 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();
    }

}
4

1 回答 1

4

在将所有组件添加到 JFrame 之后,您将需要调用setVisible(true)它。除非您明确告诉它这样做,否则程序不会在数学上知道您要显示任何内容。

IE,

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。

于 2013-02-09T04:13:11.060 回答