简体   繁体   中英

Java - GUI won't run after compiling

I've done a few other simple GUI projects the past few weeks, without much of any problem but now I am having trouble getting the program to show after compiling. I can't figure out what might be wrong as there are no errors being thrown and it is in pretty much the same format as other projects I was given for school.

Any guidance on where to look to get the GUI on screen would be appreciated so that I may go through and tweek things I want before I turn this in.

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();
    }

}

You will want to call setVisible(true) on the JFrame after adding all components to it. The program won't mathemagically know that you want to to display anything unless you specifically tell it to do so.

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);
}  

Also, you don't want to have this class extend JFrame since this is not necessary. You are already using a JFrame in the constructor and so have no need for another JFrame in the class itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM