1

我可以将 ChangeListener 添加到 Scene 并在 scene.widthProperty() 和 scene.heightProperty() 上调用它,但这不适用于通过“最大化”按钮最大化窗口时。

当像 scene.getWindow() 那样访问它时,我找不到窗口的任何 onResize 属性

这是我必须根据调整窗口大小来调整表中列的大小。

当整个窗口最大化时(不符合 scene.widthProperty() 或 scene.heightProperty() 的条件,如何将 resizeColumns 侦听器添加到

        ChangeListener<Object> resizeColumns = new ChangeListener<Object>(){

            @Override
            public void changed(ObservableValue arg0, Object arg1, Object arg2) {
                new Thread() {
                    // runnable for that thread

                    public void run() {
                        Platform.runLater(new Runnable(){
                            public void run() {
                                // what will be ran in gui thread


                                Double width =primaryStage.getWidth();
                                DraftController controller = (DraftController)loader.getController();
                                TableView<Player> teamTable =controller.getTeamTable();
                                centerColumns(width, controller, teamTable);


                                TableView<Player>  top10Table = controller.getTop10Table();

                                AnchorPane anchor = controller.getAnchorPane();
                                centerColumns(anchor.getWidth()+anchor.getWidth()*.04,controller,top10Table);
                            }


                            private void centerColumns(Double width, DraftController controller, TableView<Player> teamTable) {
                                ObservableList<TableColumn<Player, ?>> columnList = teamTable.getColumns();
                                for (int i=0 ; i<columnList.size(); i++){
                                    columnList.get(i).setPrefWidth((width-17)/teamTable.getColumns().size());
                                }
                            }
                        });
                    }
                }.start();

            }

        };

        scene.widthProperty().addListener(resizeColumns);
        scene.heightProperty().addListener(resizeColumns);
        guage: lang-java -->
4

1 回答 1

1

我的小例子在最大化舞台方面效果很好:

package org.example;

import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class ResizeScene extends Application {

    @Override
    public void start(final Stage primaryStage) throws Exception {
        final StackPane stack = new StackPane();
        final Text resolution = new Text();
        stack.getChildren().add(resolution);

        final Scene scene = new Scene(stack);
        primaryStage.setScene(scene);

        final InvalidationListener resizeListener = new InvalidationListener() {
            @Override
            public void invalidated(final Observable observable) {
                final double width = scene.getWidth();
                final double height = scene.getHeight();
                resolution.setText(width + " x " + height);
            }
        };
        scene.widthProperty().addListener(resizeListener);
        scene.heightProperty().addListener(resizeListener);

        // Initial Size
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();
    }

    public static void main(final String[] args) {
        launch(args);
    }
}
于 2013-11-12T22:13:16.510 回答