Как новичок я хотел бы понять использование событий, создав приложение для перетасовки тайлов. Пожалуйста, помогите мне
Я бы хотел приложение javafx с двумя кнопками:
- Событие кнопки 1
clickToShuffle(MouseEvent mouseEvent)
- щелчок по нему перетасовывает готовый список строк из 136 items (list1: String[] allTiles
) - Событие кнопки 2
clickToDeal(MouseEvent mouseEvent)
- при нажатии на него первые 13 элементов удаляются из списка list1 и переносятся в новый пустой список (list2)
Сведения о результате:
После нажатия кнопки 2 приложение должно удалить первые 13 элементов из списка1. Таким образом, нажатие кнопки 2 означает, что в списке list1 будет 123 элемента. list2 становится списком этих 13 элементов. Не должно быть возможности иметь более 13 элементов в списке 2.
Проблема:
Я бьюсь над кнопкой 2. Я не могу разместить данные " Переменная списка allTiles ", доступная для использования в методе кнопки 2" public void clickToDeal(MouseEvent mouseEvent)
"
Вопрос:
После нажатия кнопки 1, как я могу использовать список" allTiles "( в этот момент) и уберите 13 элементов из списка 1, нажав кнопку 2?
Это файл main :
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.Arrays;
public class Main extends Application {
String[] allTiles = {};
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/resources/home.fxml"));
// create tiles Array
String[] allTiles = new String[] {"1m1","2m1","3m1","4m1","5m1","6m1","7m1","8m1","9m1","1m2","2m2","3m2","4m2","5m2","6m2","7m2","8m2","9m2","1m3","2m3","3m3","4m3","5m3","6m3","7m3","8m3","9m3","1m4","2m4","3m4","4m4","5m4","6m4","7m4","8m4","9m4","1s1","2s1","3s1","4s1","5s1","6s1","7s1","8s1","9s1","1s2","2s2","3s2","4s2","5s2","6s2","7s2","8s2","9s2","1s3","2s3","3s3","4s3","5s3","6s3","7s3","8s3","9s3","1s4","2s4","3s4","4s4","5s4","6s4","7s4","8s4","9s4","1p1","2p1","3p1","4p1","5p1","6p1","7p1","8p1","9p1","1p2","2p2","3p2","4p2","5p2","6p2","7p2","8p2","9p2","1p3","2p3","3p3","4p3","5p3","6p3","7p3","8p3","9p3","1p4","2p4","3p4","4p4","5p4","6p4","7p4","8p4","9p4","n1","n2","n3","n4","e1","e2","e3","e4","w1","w2","w3","w4","s1","s2","s3","s4","dr1","dr2","dr3","dr4","dg1","dg2","dg3","dg4","dw1","dw2","dw3","dw4"};
System.out.println(Arrays.toString(allTiles));
primaryStage.setWidth(1280);
primaryStage.setHeight(720);
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Это контроллер файл:
package controller;
import com.sun.tools.javac.Main;
import javafx.scene.input.MouseEvent;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Controller extends Main {
public void clickToShuffle(MouseEvent mouseEvent) {
// create tiles Array
String[] allTiles = new String[] {"1m1","2m1","3m1","4m1","5m1","6m1","7m1","8m1","9m1","1m2","2m2","3m2","4m2","5m2","6m2","7m2","8m2","9m2","1m3","2m3","3m3","4m3","5m3","6m3","7m3","8m3","9m3","1m4","2m4","3m4","4m4","5m4","6m4","7m4","8m4","9m4","1s1","2s1","3s1","4s1","5s1","6s1","7s1","8s1","9s1","1s2","2s2","3s2","4s2","5s2","6s2","7s2","8s2","9s2","1s3","2s3","3s3","4s3","5s3","6s3","7s3","8s3","9s3","1s4","2s4","3s4","4s4","5s4","6s4","7s4","8s4","9s4","1p1","2p1","3p1","4p1","5p1","6p1","7p1","8p1","9p1","1p2","2p2","3p2","4p2","5p2","6p2","7p2","8p2","9p2","1p3","2p3","3p3","4p3","5p3","6p3","7p3","8p3","9p3","1p4","2p4","3p4","4p4","5p4","6p4","7p4","8p4","9p4","n1","n2","n3","n4","e1","e2","e3","e4","w1","w2","w3","w4","s1","s2","s3","s4","dr1","dr2","dr3","dr4","dg1","dg2","dg3","dg4","dw1","dw2","dw3","dw4"};
System.out.println(Arrays.toString(allTiles));
// Converting tiles Array to List String
List<String> list = Arrays.asList(allTiles);
System.out.println(list);
System.out.println(allTiles.length);
System.out.println(list.size());
// shuffle
for (int i = 0; i < allTiles.length; i++) {
int index = (int)(Math.random()*allTiles.length);
String shuffleTmp = allTiles[i];
allTiles[i] = allTiles[index];
allTiles[index] = shuffleTmp;
}
}
Тогда это файл f xml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
<bottom>
<HBox prefHeight="161.0" prefWidth="1280.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="btn_00" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="86.0" prefWidth="81.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_00" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_01" layoutX="10.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="86.0" prefWidth="79.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_01" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_02" layoutX="81.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_02" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_03" layoutX="170.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_03" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_04" layoutX="250.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_04" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_05" layoutX="330.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_05" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_06" layoutX="410.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_06" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_07" layoutX="490.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_07" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_08" layoutX="570.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_08" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_09" layoutX="650.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_09" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_10" layoutX="570.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_10" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_11" layoutX="650.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_11" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="btn_12" layoutX="730.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="71.0" mnemonicParsing="false" prefHeight="89.0" prefWidth="80.0" translateX="90.0" translateY="5.0">
<graphic>
<ImageView fx:id="handTile_12" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</graphic>
</Button>
</children>
</HBox>
</bottom>
<top>
<SplitPane dividerPositions="0.5" orientation="VERTICAL" prefHeight="319.0" prefWidth="1280.0" BorderPane.alignment="CENTER">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="135.0" prefWidth="1285.0">
<children>
<GridPane prefHeight="126.0" prefWidth="1168.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="96.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</top>
<center>
<BorderPane prefHeight="157.0" prefWidth="1280.0" BorderPane.alignment="CENTER">
<bottom>
<HBox fx:id="nextTile_hbox" prefHeight="118.0" prefWidth="1280.0" BorderPane.alignment="CENTER">
<children>
<ImageView fx:id="nextTile_00" fitHeight="170.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" rotate="90.0" translateX="170.0" translateY="18.0">
<image>
<Image url="@Black/Back.png" />
</image>
</ImageView>
</children>
</HBox>
</bottom>
<left>
<Button fx:id="shuffle_btn" mnemonicParsing="false" onMouseClicked="#clickToShuffle" prefHeight="54.0" prefWidth="120.0" text="Shuffle" BorderPane.alignment="CENTER" />
</left>
<top>
<Button fx:id="deal_btn" mnemonicParsing="false" onMouseClicked="#clickToDeal" prefHeight="53.0" prefWidth="120.0" text="Deal" BorderPane.alignment="TOP_LEFT" />
</top>
</BorderPane>
</center>
</BorderPane>