Можно ли заставить кнопки работать правильно на VBox? - PullRequest
0 голосов
/ 23 марта 2020

Кнопки работают только иногда.

Когда я нажимаю на кнопки (1, 2, 3), я не могу нажать кнопку 2, пока не нажму их по порядку (3, 2, 1).

Пожалуйста, помогите - я понял, что должен использовать якорную панель, но как? Я однажды использовал анкорпан, и он все еще не работал для меня. anchorpane намного сложнее, потому что я должен был установить координаты x и y для всех 10 кнопок, что очень хлопотно и не так чисто, как хотелось бы, чтобы мой код был. Если бы кто-нибудь мог понять это, это было бы здорово, спасибо!

MainClass

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text; 
import javafx.scene.text.Font;
import javafx.geometry.Pos;
import java.util.ArrayList;

import javafx.scene.input.MouseEvent;

import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.util.Duration;
import java.util.Random;

import javafx.scene.layout.AnchorPane;

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.layout.*; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.collections.*; 
import javafx.stage.Stage; 
import javafx.scene.text.Text.*; 
import javafx.scene.text.*; 
import javafx.scene.text.Text; 
/**
 * Shut the box game
 *
 * @author Bryan Worden
 * @version 3/22/20
 */
public class mainClass extends Application
{
    //imports pane and 2 dice
    Boxes boxPane = new Boxes();
    Dice die1 = new Dice();
    Dice die2 = new Dice();

    //creates array list to keep track of what boxes are open and shut.
    public ArrayList<String> boxes = new ArrayList<String>();

    //creates array list to keep track of what user puts in for boxes.
    public ArrayList<Integer> userBoxes = new ArrayList<Integer>();

    //sets dice sum and user sum
    int diceSum;
    int userSum=0;

    int padding = 10;
    int alignment = 35;

    int L = 100;
    int W = 50;



    @Override
    public void start(Stage primaryStage) throws Exception
    {
        //imports my 2 dice and boxes and my roll button
        boxPane.Boxes();
        die1.Dice();
        die2.Dice();



        //creates all buttons 
        Button roll =       new Button("Roll");
        Button submit =     new Button("Submit");
        Button endGame =    new Button("End Game");
        Button tryAgain =   new Button("Try Again");

        submit.setStyle("-fx-text-fill: green");

        //sets all boxes to open
        for (int i=1; i<11; i++)
        {
            boxes.add("open");
            System.out.println(i);
        }     

        //draws the boxes
        boxPane.drawBoxes(boxes);
        boxPane.setAlignment(Pos.CENTER);

        //creates the buttons for the boxes
        Button one =    new Button("Shut");
        Button two =    new Button("Shut");
        Button three =  new Button("Shut");
        Button four =   new Button("Shut");
        Button five =   new Button("Shut");
        Button six =    new Button("Shut");
        Button seven =  new Button("Shut");
        Button eight =  new Button("Shut");
        Button nine =   new Button("Shut");

        HBox buttonPane = new HBox();
        buttonPane.getChildren().addAll(one, two,three, four, five, six, seven, eight, nine);


        //draws the dice
        die1.drawDie(1);
        die2.drawDie(1);

        //creates the pane for the dice
        HBox diePane = new HBox(2);
        diePane.setAlignment(Pos.CENTER);
        diePane.getChildren().addAll(die1, die2);


        //puts boxes buttons and dice in one pane
        VBox mainPane = new VBox(25);
        mainPane.setAlignment(Pos.CENTER);
        mainPane.getChildren().addAll(boxPane,buttonPane, diePane, roll, submit);


        /* HANDLES ACTIONS  */

        //chooses random dice when roll is clicked.
        Random rand = new Random(); 


        //sets the submit button up
        submit.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) 
        {
            if (diceSum == userSum)
            {
                for (int i=0; i< userBoxes.size(); i++)
                {
                    userBoxes.get(i);
                    if (userBoxes.get(i) == 1)
                    {
                        one.setDisable(true);
                    }
                    if (userBoxes.get(i) == 2)
                    {
                        two.setDisable(true);
                    }
                    if (userBoxes.get(i) == 3)
                    {
                        three.setDisable(true);
                    }
                    if (userBoxes.get(i) == 4)
                    {
                        four.setDisable(true);
                    }
                    if (userBoxes.get(i) == 5)
                    {
                        five.setDisable(true);
                    }
                    if (userBoxes.get(i) == 6)
                    {
                        six.setDisable(true);
                    }
                    if (userBoxes.get(i) == 7)
                    {
                        seven.setDisable(true);
                    }
                    if (userBoxes.get(i) == 8)
                    {
                        eight.setDisable(true);
                    }
                    if (userBoxes.get(i) == 9)
                    {
                        nine.setDisable(true);
                    }
                    roll.setDisable(false);
                    userBoxes.clear();
                    //user
                }
            }
            else 
            {
                Text text = new Text();
                text.setText("You entered the wrong combination");

                HBox hbox = new HBox();
                hbox.getChildren().addAll(endGame,tryAgain);
                VBox vbox = new VBox();
                vbox.getChildren().addAll(text, hbox);

                Scene scene1= new Scene(vbox, 300, 250);
                primaryStage.setScene(scene1);
                primaryStage.show();

            }
        }});




        //sets the shut/open buttons up.



        one.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t) {
                if (boxes.get(1).equals("open"))
            {
                boxPane.switchBox(1, boxes);
                if (one.getText().toString().equals("Shut"))
                {
                    one.setText("Open");
                    one.setStyle("-fx-font-size:11");
                }
                else
                {
                    one.setText("Shut");
                    one.setStyle("-fx-font-size:12");
                }
                userSum+=1;
            }
            }
        });
        two.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t) {
                if (boxes.get(2).equals("open"))
            {
                boxPane.switchBox(2, boxes);
                if (two.getText().toString().equals("Shut"))
                {
                    two.setText("Open");
                    two.setStyle("-fx-font-size:11");
                }
                else
                {
                    two.setText("Shut");
                    two.setStyle("-fx-font-size:12");
                }
                userSum+=1;
            }
            }
        });
        three.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t) {
                if (boxes.get(3).equals("open"))
            {
                boxPane.switchBox(3, boxes);
                if (three.getText().toString().equals("Shut"))
                {
                    three.setText("Open");
                    three.setStyle("-fx-font-size:11");
                }
                else
                {
                    three.setText("Shut");
                    three.setStyle("-fx-font-size:12");
                }
                userSum+=1;
            }
            }
        });

        eight.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) 
        {
            if (boxes.get(8).equals("open"))
            {
                boxPane.switchBox(3, boxes);
                if (eight.getText().toString().equals("Shut"))
                {
                    eight.setText("Open");
                    eight.setStyle("-fx-font-size:11");
                }
                else
                {
                    eight.setText("Shut");
                    eight.setStyle("-fx-font-size:12");
                }
                userSum+=8;
            }
            userBoxes.add(8);
        }});

        nine.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) 
        {
            if (boxes.get(9).equals("open"))
            {
                boxPane.switchBox(9, boxes);
                if (nine.getText().toString().equals("Shut"))
                {
                    nine.setText("Open");
                    nine.setStyle("-fx-font-size:11");
                }
                else
                {
                    nine.setText("Shut");
                    nine.setStyle("-fx-font-size:12");
                }
                userSum+=9;
            }
            userBoxes.add(9);
        }});


        tryAgain.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) 
        {
            Scene scene3 = new Scene(mainPane);
            primaryStage.show();
            }});

        //Creates scene/title and shows the scene
        Scene scene = new Scene(mainPane);
        primaryStage.setTitle("JavaFX Example");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Класс ящиков

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text; 
import javafx.scene.text.Font;
import javafx.geometry.Pos;
import java.util.ArrayList;
import javafx.scene.layout.Pane;
/**
 * Boxes
 *
 * @author Bryan Worden
 * @version 3/22/20
 */
public class Boxes extends HBox
{
    //sets the width and length of rectangles
    int L = 100;
    int W = 50;


    public void Boxes()
    {
        setSpacing(5);
        setPadding(new Insets(10, 10, 10, 10));
    }
    public void switchBox(int i, ArrayList boxes)
    {
        i--;
        if (boxes.get(i).equals("open"))
        {
            boxes.set(i, "closed");
        }
        else if (boxes.get(i).equals("closed"))
        {
            boxes.set(i, "open");
        }
        //drawBoxes(boxes);

    }
    public void drawBoxes(ArrayList boxes)
    {
        getChildren().clear();
        System.out.println("WORKS");
        for (int i=1; i<10; i++)
        {
            if (boxes.get(i-1).equals("open"))
            {
                Rectangle rect = new Rectangle(W,L); rect.setFill(Color.WHITE); rect.setStroke(Color.BLACK);
                Text text = new Text(String.valueOf(i)); text.setFont(Font.font(50));
                StackPane shutboxpane= new StackPane(rect, text);
                getChildren().add(shutboxpane);
            }
            else
            {
               Rectangle rect = new Rectangle(W,L); rect.setFill(Color.BLACK); rect.setStroke(Color.BLACK);
               StackPane shutboxpane= new StackPane(rect);
               getChildren().add(shutboxpane);
            }
        }
    }

}

Класс игры в кости

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Circle; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color;
import javafx.geometry.Pos;
import java.util.concurrent.TimeUnit;
import java.util.Date;
import java.util.Random;

import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.util.Duration;
/**
 * Dice
 *
 * @author Bryan Worden
 * @version 3/22/20
 */
public class Dice extends Pane 
{
    int S = 50;
    int dot = 3;
    Timeline animation;
    Random rand = new Random(); 
    public void Dice()
    {

    }

    public void drawDie(int number)
    {
        getChildren().clear();
        if (number == 1)
        {
            //draw dice with 1 dot in the middle
            Rectangle die = new Rectangle(S,S); die.setFill(Color.WHITE); die.setStroke(Color.BLACK);
            Circle circle1 = new Circle(S * 0.5 ,S * 0.5, dot);

            //sends this to stackpane
            getChildren().addAll(die,circle1);
        }
        if (number == 2)
        {
            Rectangle die = new Rectangle(S,S); die.setFill(Color.WHITE); die.setStroke(Color.BLACK);
            Circle circle1 = new Circle(S * 0.25, S * 0.25, dot);

            Circle circle2 = new Circle(S * 0.75 ,S * 0.75, dot);

            getChildren().addAll(die,circle1, circle2);
        }
        if (number == 3)
                {
            Rectangle die = new Rectangle(S,S); die.setFill(Color.WHITE); die.setStroke(Color.BLACK);
            Circle circle1 = new Circle(S * 0.25, S * 0.25, dot);

            Circle circle2 = new Circle(S * 0.50 ,S * 0.50, dot);

            Circle circle3 = new Circle(S * 0.75 ,S * 0.75, dot);

            getChildren().addAll(die,circle1, circle2, circle3);
        }
        if (number == 4)
                {
            Rectangle die = new Rectangle(S,S); die.setFill(Color.WHITE); die.setStroke(Color.BLACK);
            Circle circle_topLeft =     new Circle(S * 0.25, S * 0.25, dot);
            Circle circle_topRight =    new Circle(S * 0.75 ,S * 0.25, dot);

            Circle circle_bottomLeft =  new Circle(S * 0.25 ,S * 0.75, dot);
            Circle circle_bottomRight = new Circle(S * 0.75 ,S * 0.75, dot);

            getChildren().addAll(die,circle_topLeft, circle_topRight, circle_bottomLeft, circle_bottomRight);
        }
        if (number == 5)
                {
            Rectangle die = new Rectangle(S,S); die.setFill(Color.WHITE); die.setStroke(Color.BLACK);
            Circle circle_topLeft =     new Circle(S * 0.25, S * 0.25, dot);
            Circle circle_topRight =    new Circle(S * 0.75, S * 0.25, dot);

            Circle circle_Middle   =    new Circle(S * 0.50, S * 0.50, dot);

            Circle circle_bottomLeft =  new Circle(S * 0.25, S * 0.75, dot);
            Circle circle_bottomRight = new Circle(S * 0.75, S * 0.75, dot);

            getChildren().addAll(die,circle_topLeft, circle_topRight, circle_Middle, circle_bottomLeft, circle_bottomRight);
        }
        if (number == 6)
        {
            Rectangle die = new Rectangle(S,S); die.setFill(Color.WHITE); die.setStroke(Color.BLACK);
            Circle circle_topLeft =     new Circle(S * 0.25, S * 0.25, dot);
            Circle circle_midLeft =     new Circle(S * 0.25, S * 0.50, dot);
            Circle circle_topRight =    new Circle(S * 0.75, S * 0.25, dot);

            Circle circle_bottomLeft =  new Circle(S * 0.25, S * 0.75, dot);
            Circle circle_midRight =    new Circle(S * 0.75, S * 0.50, dot);
            Circle circle_bottomRight = new Circle(S * 0.75, S * 0.75, dot);

            getChildren().addAll(die,circle_topLeft, circle_topRight, circle_midLeft, circle_bottomLeft, circle_midRight, circle_bottomRight);
        }
    }

}
...