-3

I am making a Guess Your Number game but it will not run, just says it terminated.

Also need more help on the overall code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class GuessMyNumber extends JFrame {


    private JTextField textField;
    public JPanel Mainpanel;
    public JLabel lblEnterNumberHere;
    public JLabel lblNewLabel;
    public JButton btnCheckGuess;
    public JLabel lblNewLabel_1;
    Random rand = new Random();
    int random = rand.nextInt(100);
    int cap = 3;
    int tries = 0;

    public GuessMyNumber(){
        getContentPane().setLayout(null);

        Mainpanel = new JPanel();
        Mainpanel.setBounds(0, 0, 424, 250);
        getContentPane().add(Mainpanel);
        Mainpanel.setLayout(null);

        lblNewLabel = new JLabel("Enter a number between 1-100 ");
        lblNewLabel.setBounds(126, 11, 153, 14);
        Mainpanel.add(lblNewLabel);

        lblEnterNumberHere = new JLabel("Enter number here");
        lblEnterNumberHere.setBounds(164, 51, 90, 14);
        Mainpanel.add(lblEnterNumberHere);

        textField = new JTextField();
        textField.setBounds(174, 76, 86, 20);
        Mainpanel.add(textField);
        textField.setColumns(10);

        btnCheckGuess = new JButton("Check guess");
        btnCheckGuess.setBounds(161, 107, 93, 23);
        Mainpanel.add(btnCheckGuess);

        lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setBounds(150, 159, 129, 47);
        Mainpanel.add(lblNewLabel_1);

        initialize();

    }

    private class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent arg0)
        {
            int guess = Integer.parseInt(textField.getText());
            if(tries==cap){
                lblNewLabel_1.setText("You have guessed too many times.");
            }
            else{
                tries++;
                if(guess>random)
                    lblNewLabel_1.setText("Your guess was too high.");
                else if(guess<random)
                    lblNewLabel_1.setText("Your guess is too low.");
                else
                    lblNewLabel_1.setText("You are correct.");
            }
        }
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GuessMyNumber();
            }
        });
    }

    private void initialize()
    {
        Mainpanel.setPreferredSize(new Dimension(300,300));
        // Create labels, buttons, etc. and place them on the window here...
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • What's that random ending `}` for? – Laurel Apr 28 '16 at 22:53
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Apr 28 '16 at 23:12

1 Answers1

2

It appears as though you have forgotten to make the window visible. This can be done with: setVisible(true);

If the window is not made visible, event listeners will not start, and as there is no further code to execute, the program will end.

Betato
  • 108
  • 8