Я пытаюсь создать программу, которая позволяет пользователю забронировать место и изменить цвет (в этом случае изменить только изображение), когда место выбрано.Мой код на самом деле работает нормально, но я думаю, что это многословно, и должен быть более эффективный способ, а также невозможно «отменить выбор» места.
Существует способ написать только один метод, в зависимости от которогоКнопка (я использовал обычные изображения с оператором onClick = "# ...") изменяет изображение на конкретном выбранном изображении / кнопке?
В классе Controller я объявил тип объекта:
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class TheatreController {
@FXML
private ImageView a1, a2, a3, b1, b2, b3, c1, c2, c3;
@FXML
private void ona2Click(){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Do you want to proceed with the booking?", ButtonType.YES, ButtonType.NO);
Image redSeat = new Image("sample/resources/seatsandicons/icons8-armchair-96.png");
a2.setImage(redSeat);
}
@FXML
private void ona3Click(){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Do you want to proceed with the booking?", ButtonType.YES, ButtonType.NO);
Image redSeat = new Image("sample/resources/seatsandicons/icons8-armchair-96.png");
a3.setImage(redSeat);
}
@FXML
private void onb1Click(){
...
}
FXML:
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="505.0" prefWidth="650.0" style="-fx-background-color: #4ce4ef;" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.CharlotteBronteController">
<children>
<GridPane alignment="CENTER" layoutX="194.0" layoutY="146.0" prefHeight="214.0" prefWidth="262.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView fx:id="a1" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" onMouseClicked="#ona1Click">
<image>
<Image url="@../resources/seatsandicons/icons8-armchair-96-3.png" />
</image>
</ImageView>
<ImageView fx:id="a2" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" onMouseClicked="#ona2Click">
<image>
<Image url="@../resources/seatsandicons/icons8-armchair-96-3.png" />
</image>
</ImageView>
<ImageView fx:id="a3" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" onMouseClicked="#ona3Click">
<image>
<Image url="@../resources/seatsandicons/icons8-armchair-96-3.png" />
</image>
</ImageView>
<ImageView fx:id="b1" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.rowIndex="1" onMouseClicked="#onb1Click">
<image>
<Image url="@../resources/seatsandicons/icons8-armchair-96-3.png" />
</image>
</ImageView>
<ImageView fx:id="b2" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="1" onMouseClicked="#onb2Click">
<image>
<Image url="@../resources/seatsandicons/icons8-armchair-96-3.png" />
</image>
</ImageView>
<ImageView fx:id="b3" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.rowIndex="1" onMouseClicked="#onb3Click">
<image>
<Image url="@../resources/seatsandicons/icons8-armchair-96-3.png" />
</image>
</ImageView>
<ImageView fx:id="c1" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.rowIndex="2" onMouseClicked="#onc1Click">
<image>
<Image url="@../resources/seatsandicons/icons8-armchair-96-3.png" />
</image>
</ImageView>
<ImageView fx:id="c2" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="2" onMouseClicked="#onc2Click">
<image>
<Image url="@../resources/seatsandicons/icons8-armchair-96-3.png" />
</image>
</ImageView>
<ImageView fx:id="c3" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.rowIndex="2" onMouseClicked="#onc3Click">
<image>
<Image url="@../resources/seatsandicons/icons8-armchair-96-3.png" />
</image>
</ImageView>
</children>
</GridPane>
<Label layoutX="227.0" layoutY="122.0" text="A" textFill="WHITE" />
<Label layoutX="316.0" layoutY="122.0" text="B" textFill="WHITE" />
<Label layoutX="400.0" layoutY="122.0" text="C" textFill="WHITE" />
<Label layoutX="168.0" layoutY="168.0" text="1" textFill="WHITE" />
<Label layoutX="168.0" layoutY="244.0" text="2" textFill="WHITE" />
<Label layoutX="168.0" layoutY="312.0" text="3" textFill="WHITE" />
</children>
</AnchorPane>