我有一个带10x10按钮的GridPane。全部放在名为'按钮的ArrayList中。现在我需要知道,当单击一个按钮时,它应该记住一个按钮,当单击另一个按钮时执行一个动作。所以你会得到一个方法 makeTurn(按钮1,按钮2); 现在问我的问题,我该怎么做?这是我制作GUI的第一堂课
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.File;
import java.util.ArrayList;
public class DamspelApp extends Application implements EventHandler<ActionEvent>{
private ArrayList<Button> buttons = new ArrayList<Button>();
private Damspel spel = new Damspel();
@Override
public void start(Stage primaryStage) {
//CREATING UPPER HBOX
Label melding = new Label(""+spel.getMeldingProperty());
if(spel.getMelding()==null){
melding.setText("Hier komen de meldingen!");
}
HBox hbox = new HBox();
hbox.getChildren().add(melding);
hbox.setPrefHeight(40);
hbox.setAlignment(Pos.CENTER_RIGHT);
//CREATING HBOX
Button reset = new Button("reset");
reset.setPrefWidth(60);
CheckBox roteren = new CheckBox();
roteren.getStyleClass().add("roteren");
Label rot = new Label("Bord roteren");
Label speler_lb = new Label("Speler: " + spel.getSpelerProperty());
Label stenenZ_lb = new Label("Stenen Z: ");
Label stenenW_lb = new Label("Stenen W: ");
//CREATING VBOX
VBox vbox = new VBox();
vbox.getChildren().add(reset);
HBox hbxo = new HBox();
hbxo.getChildren().add(roteren);
hbxo.getChildren().add(rot);
vbox.getChildren().add(hbxo);
vbox.getChildren().add(speler_lb);
vbox.getChildren().add(stenenZ_lb);
vbox.getChildren().add(stenenW_lb);
vbox.setSpacing(10);
vbox.setPrefWidth(100);
//CREATING AND POSITIONING IN BORDERPANE
BorderPane border = new BorderPane();
GridPane grid = new GridPane();
border.setCenter(grid);
border.setTop(hbox);
border.setLeft(vbox);
//CREATING THE BUTTONS
int columns = 1;
int rows=0;
for(int i = 0; i < 100; i++){
buttons.add(new Button(""+i));
}
for(Button b : buttons){
grid.add(b, columns, rows);
String text = b.getText();
int texti = Integer.parseInt(text);
columns++;
b.setId(text);
b.setPrefSize(45,45);
b.setOnAction(this);
if(texti == 9 || texti == 19||texti==29||texti==39||texti==49||texti==59||texti==69||texti==79||texti==89||texti==99){
rows++;
columns = 1;
}
String cssClassVoorButton = spel.getVeldStatus(texti);
switch(cssClassVoorButton){
case "WITDAM":b.getStyleClass().add("witdam");break;
case "ZWARTDAM": b.getStyleClass().add("zwartdam");break;
case "WIT": b.getStyleClass().add("wit");break;
case "ZWART": b.getStyleClass().add("zwart"); break;
case "NIETSPEELBAAR": b.getStyleClass().add("nietspeelbaar");break;
case "LEEG": b.getStyleClass().add("leeg");break;
default: break;
}
}
//MAKING THE SCENE
Scene scene = new Scene(border, 500, 500);
//File cssSheet = new File("myCSS.css");
//scene.getStylesheets().add(cssSheet.toURI().toString());
scene.getStylesheets().add("stylesheet2.css");
primaryStage.setTitle("Kamer Boeken");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
//---------------------------------------------------------------------------------------------------------Rotate-RESET
reset.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
RotateTransition rt = new RotateTransition(Duration.millis(3000), grid);
rt.setByAngle(180);
rt.setCycleCount(1);
rt.setAutoReverse(false);
rt.play();
}
});
if(roteren.isSelected() && spel.getCheck()){
RotateTransition rt = new RotateTransition(Duration.millis(3000), grid);
rt.setByAngle(180);
rt.setCycleCount(1);
rt.setAutoReverse(false);
}
}
public void handle(ActionEvent event) {
System.out.println(""+ event.getSource());
for(Button b : buttons){
if(event.getSource() == b){
System.out.println(""+ event.getSource());
b.getStyleClass().add("leeg");
String styleclass = spel.getVeldStatus(Integer.parseInt(b.getText()));
String stylec = styleclass.toLowerCase();
b.getStyleClass().remove(stylec);
System.out.println(spel.getVeldStatus(Integer.parseInt(b.getText())));
}
}
}
public static void main(String args[]) {
Application.launch(args);
}
}
答案 0 :(得分:0)
您只需存储在某处点击的第一个Button
即可。此外,我建议使用ToggleButton
,因为用户可以更清楚地看到哪个Button
被按下了:
public class Main extends Application {
private final ObjectProperty<ToggleButton> rememberMe = new SimpleObjectProperty<>();
private final List<ToggleButton> buttons = new ArrayList<>();
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
grid.setHgap(5);
grid.setVgap(5);
grid.setPadding(new Insets(5));
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
ToggleButton button = new ToggleButton(row + " | " + col);
button.selectedProperty().addListener((w, o, n) -> {
if (n) {
if(rememberMe.get() != null) {
turn(rememberMe.get() , button);
}
else {
rememberMe.set(button);
}
}
});
grid.add(button, col, row);
buttons.add(button);
}
}
Scene scene = new Scene(grid, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
private void turn(ToggleButton pButton1, ToggleButton pButton2) {
rememberMe.set(null);
System.out.println(pButton1 + " - " + pButton2);
buttons.forEach(tb -> tb.setSelected(false));
}
public static void main(String[] args) {
launch(args);
}
}
从现在开始,创建突出显示和其余游戏逻辑应该非常简单。
答案 1 :(得分:0)
您可以使用节点的方法setUserData和getUserData来存储各种信息,例如您是否点击了按钮。
作为替代方案,您可以使用BitSet来存储布尔状态。