简体   繁体   English

Java - GUI在编译后不会运行

[英]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. 在过去的几周里,我做了一些其他简单的GUI项目,没有任何问题,但现在我在编译后无法让程序显示出来。 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. 任何关于在屏幕上获取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();
    }

}

You will want to call setVisible(true) on the JFrame after adding all components to it. 在向JFrame添加所有组件之后,您将需要在JFrame上调用setVisible(true) 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. 此外,您不希望此类扩展JFrame,因为这不是必需的。 You are already using a JFrame in the constructor and so have no need for another JFrame in the class itself. 您已经在构造函数中使用了JFrame,因此不需要在类本身中使用另一个JFrame。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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