简体   繁体   中英

Stop Java Swing from executing without user input

I have created the generic battleships game on Java and have included a snippet of the code below. I am getting user input much in the same way as using the console in that on the GUI I have a JLabel acting as the print statements and a JTextField accepting the users input and then a JButton to "submit" the input.

The problem is because I created the program first to work without a GUI and am now converting it the program and I think the action listener is running away with it's self. I have started the code snippet at where the user is inputting the positions of the ships, they have already selected how many of each type of ship they want. It is at this point though where I would like the user to input for example 1 (to select horizontal) then A4 to start the ship at A4, after this the program would keep looping till the desired number of ships have been placed.

However the program instead skips ahead never prompting the user to enter orientation and changes the label to "enter a guess" (the next method which is shooting at the CPU ships) and the console prints "Would you like the ship Sub horizontal or vertical ? For horizontal enter number 1 Or for vertical enter number 2: The CPU shot was at: 42 The result of the CPU's shot was: miss Enter a guess" Essentially skipping any input from the user as to placement or orientation, as i said I think this may be due to the action listener although I am unsure, ideally the program would run and as soon as it reaches any label1.getText(); the program would "pause" until the user has input text and then pressed the submit button then carry on running.

So my question is is there a way of inserting this "break' or "pause" into the code without changing the whole program?

for (int i = 0; i < ship.size(); i++)
        {
            final int shipNumber = i;
                while (!worked)
                {
                    playerLocationToSet.clear();
                    worked = true;
                    label1.setText("Would you like the ship " + ship.get(shipNumber).getName() + " horizontal(enter 1) or vertical(enter 2) ?");
                    System.out.println("Would you like the ship " + ship.get(shipNumber).getName() + " horizontal or vertical ?");
                    System.out.println("For horizontal enter number 1");
                    System.out.println("Or for vertical enter number 2: ");
                    button1.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent f){
                            int addLetter;
                            int addNumber;
                        String orientString = textField1.getText();
                          int orient = Integer.parseInt(orientString);
                          // horizontal
                    if (orient == 1)
                    {
                        addLetter = 0;
                        addNumber = 1;
                    }
                    // vertical
                    else if (orient == 2)
                    {
                        addLetter = 1;
                        addNumber = 0;
                    }
                            int letterNumber = 0;
                            label1.setText("Where would you like to start " +  ship.get(shipNumber).getName() + "?");
                            String start = textField1.getText();
                            start = start.toUpperCase();
                            int letter = start.charAt(0);
                            int number = start.charAt(1) - 48;
                            for (int x = 0; x < alphabet.length; x++)
                            {
                                if(alphabet[x] == letter)
                                    {   
                                        letterNumber = x + 1;
                                    }
                            }
                            for (int j = 0; j <ship.get(shipNumber).getSize(); j++)
                            {
                                String position = "" + letterNumber + number;
                                playerLocationToSet.add(position);
                                letterNumber = letterNumber + addLetter;
                                number = number + addNumber;
                                for (int t = 0; t < ship.size(); t++)                       }
                    });
                    ship.get(i).setLocation(playerLocationToSet);
                }
        }
    }

    private void playerShoot()
    {
        String guess;
        String result;
        char[] alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        char letter;
        int number;
        int guessLetter = 0;
        String finalGuess;
        while (!CPUship.isEmpty())
        {
            result = "miss";
            label1.setText("Enter a guess");
            System.out.println("Enter a guess");
            guess = textField1.getText();
            guess = guess.toUpperCase();
            letter = guess.charAt(0);
            number = guess.charAt(1) - 48;

Apologies for such a long question I wanted to try make it as clear as possible. Feel free to ask any questions and doubtless I have missed something. Thanks in advance

is is there a way of inserting this "break' or "pause"

Didn't look at the code or the structure of your code, but in general you would use a JOptionPane to prompt a user for input. This will create a modal JDialog which will stop processing until the dialog is closed.

Read the section from the Swing tutorial on How to Make Dialogs for more information and examples.

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