我想创建简单的gridPane,它会在单击按钮后更改其大小。我有这段代码:
public class NewSimScene extends GridPane{
Button testButton;
public NewSimScene(){
setPrefSize(500, 500);
testButton = new Button("TEST");
testButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
Node source = (Node) t.getSource();
NewSimScene pane = (NewSimScene) source.getParent();
pane.setPrefSize(100, 100);
pane.setMaxSize(100, 100);
}
});
getChildren().add(testButton);
}
}
当我调试时,我可以看到prefHeight和prefWidth值已更改,但窗格外观没有任何变化。可能有什么问题?
我不知道它是否相关,但我在一个单独的场景中运行此窗格,从MenuBar触发。
答案 0 :(得分:0)
您的代码中没有问题,Grid Pane正在重新调整大小。请查看以下代码,如果您正在寻找其他内容,请随时发表评论!
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class TableViewSample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
GridPane grid = new NewSimScene();
((Group) scene.getRoot()).getChildren().addAll(grid);
stage.setScene(scene);
stage.show();
}
public class NewSimScene extends GridPane {
Button testButton;
public NewSimScene() {
setPrefSize(500, 500);
setStyle("-fx-background-color: palegreen;");
testButton = new Button("TEST");
testButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
Node source = (Node) t.getSource();
NewSimScene pane = (NewSimScene) source.getParent();
pane.setPrefSize(100, 100);
}
});
getChildren().add(testButton);
}
}
}