0

I want have a validation in my input. I want that only numbers are allowed in the input. How can I do that? Also I want to split the code of my GUI and gameplay in two classes. Whatever I try, I get keeping errors when I try to split it. I put my code in code snippet. This is my code:

package RaadGetal;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GuessGame extends JFrame {
    private static final long serialVersionUID = 1L;
    private JLabel guessLabel1;

    private JTextField guessField;
    private JButton guessButton;

    private JPanel northPanel;
    private JPanel southPanel;

    private Random random = new Random();

    private int number;

    public GuessGame() {
        super("Raad het getal");

        ButtonListener submitListener = new ButtonListener();
        this.getContentPane().setLayout(new BorderLayout());

        northPanel = new JPanel(new FlowLayout());
        guessLabel1 = new JLabel("Raad een getal tussen 1 en 100");
        northPanel.add(guessLabel1);

        guessField = new JTextField(3);
        northPanel.add(guessField);
        add(northPanel, BorderLayout.NORTH);

        southPanel = new JPanel(new GridLayout(2, 2));
        guessButton = new JButton("Raad");
        guessButton.addActionListener(submitListener);

        southPanel.add(guessButton);
        add(southPanel, BorderLayout.SOUTH);
        number = 1 + random.nextInt(100);
    }

    public static void main(String args[]) {
        GuessGame aGame = new GuessGame();
        aGame.setTitle("Raad Getal");
        aGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aGame.setSize(300, 150);
        aGame.setVisible(true);
    }

    public class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            String guessString;
            int guessNumber;
            if (event.getSource() == guessButton) {
                guessString = guessField.getText();
                guessNumber = Integer.parseInt(guessString);

                if (guessNumber == number) {
                    guessField.setVisible(false);
                    guessLabel1.setText("Gefeliciteerd, je hebt het juiste getal geraden!");
                } else {
                    if (guessNumber > number)
                        guessLabel1.setText("Te hoog!");
                    else
                        guessLabel1.setText("Te laag!");
                }
            }

        }
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Johan
  • 23
  • 9
  • http://stackoverflow.com/questions/2749521/how-to-validate-a-jtextfield explains how you can add validators to `JTextField` whereas for the second problem - how did you split them and what errors did you get – Aimee Borda Feb 02 '17 at 09:53
  • You should pass a instance of `GuessGame` to your `ButtonListener` instance, in this way buttonListener will be able to see guessGame attributes. – Edgard Leal Feb 02 '17 at 11:52
  • Thank you for the validation link, but I'm sorry, I don't know how to place it in the code. What I did with splitting the code was trying to have a class for GUI and a class for the gameplay. Got 'Constructor call must be the first statement in a constructor' – Johan Feb 02 '17 at 14:27

0 Answers0