【发布时间】:2020-03-02 10:42:36
【问题描述】:
我正在创建一个允许两个用户玩井字游戏的界面。游戏规则不需要强制执行,当用户点击按钮时,需要出现“X”或“O”。
我已经设置了界面,但是我无法将需要发生的事件连接到按钮。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
public class TicTacToe extends Application {
private GridPane gBox;
private HBox hbox;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//int n = 18;
//ArrayList<Button> buttons = new ArrayList<>(n);
Image image = new Image("file:O.png");
Image image2 = new Image("file:X.png");
ImageView imageView1 = new ImageView(image2);
Button button1 = new Button("X");
Button button2 = new Button("O");
Button button3 = new Button("X");
Button button4 = new Button("O");
Button button5 = new Button("X");
Button button6 = new Button("O");
Button button7 = new Button("X");
Button button8 = new Button("O");
Button button9 = new Button("X");
Button button10 = new Button("O");
Button button11 = new Button("X");
Button button12 = new Button("O");
Button button13 = new Button("X");
Button button14 = new Button("O");
Button button15 = new Button("X");
Button button16 = new Button("O");
Button button17 = new Button("X");
Button button18 = new Button("O");
gBox = new GridPane();
gBox.add(button1, 0,0);
gBox.add(button2,0,1);
gBox.add(button3,1,0);
gBox.add(button4, 1,1);
gBox.add(button5, 2,0);
gBox.add(button6,2,1);
gBox.add(button7, 0, 5);
gBox.add(button8,0,6);
gBox.add(button9, 1,5);
gBox.add(button10,1,6);
gBox.add(button11, 2,5);
gBox.add(button12,2,6);
gBox.add(button13,0,10);
gBox.add(button14,0,11);
gBox.add(button15,1,10);
gBox.add(button16,1,11);
gBox.add(button17,2,10);
gBox.add(button18,2,11);
gBox.setHgap(130);
gBox.setVgap(10);
gBox.setPadding(new Insets(10));
ButtonClickHandler mbh = new ButtonClickHandler();
button1.setOnAction(mbh);
Scene scene = new Scene(gBox, 380, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("Tic Tac Toe");
primaryStage.show();
}
class ButtonClickHandler implements EventHandler<ActionEvent>{
@Override
public void handle(ActionEvent event){
Image image = new Image("file:O.png");
ImageView imageView = new ImageView(image);
HBox hbox = new HBox(imageView);
Button btn = (Button) event.getSource();
imageView.setImage(image);
}
}
}
【问题讨论】:
-
看来你快到了。您只需将包含
ImageView和Button的HBox组合起来。那并处理您有一个 6 x 3 字段而不是 3 x 3 字段的事实,并选择了一种方法来创建/添加使用大量重复代码的按钮。 (祝你好运,再写 17 行,如button1.setOnAction(mbh);如果我必须写这样的代码,它只会包含这样的一行......) -
@fabian 我知道它有很多重复的代码,我想你可以把所有的按钮放在一个数组或数组列表中,无论如何你可以告诉我这是怎么做的(或者你会怎么做) :)?
-
我真的不知道你到底想做什么。
btn.setGraphic(hbox);?Pane parent = (Pane)button.getParent(); GridPane.setRowIndex(hbox, GridPane.getRowIndex(btn)); GridPane.setColumnIndex(hbox, GridPane.getColumnIndex()); parent.getChildren().set(parent.getChildren().indexOf(btn), hbox);???还有什么?根据您的要求,正确设计模型并可能设计自定义 gui 元素或至少以专用方法创建/设置按钮可能很有用...