gpt4 book ai didi

java - 我如何知道是否按下了 2 个按钮? JavaFX

转载 作者:行者123 更新时间:2023-11-30 03:33:36 24 4
gpt4 key购买 nike

我有一个带有 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);
}


}

最佳答案

您只需存储在某处单击的第一个 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);
}
}

从这里开始,创建突出显示和游戏逻辑的其余部分应该非常简单。

关于java - 我如何知道是否按下了 2 个按钮? JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28495621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com