1

ビューの上にある webView の更新ボタンを作成したい (ビューの一部を非表示にしても)、ボタンをグリッド ペインに配置すると、webView が下または横に押し出されます (場所によって異なります)。ボタンを配置します)

「更新」ボタンを webView の上に配置し、横に移動しないようにするにはどうすればよいですか?

import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class webviewbrowser extends Application {

@Override public void start(Stage primaryStage) throws Exception {
    Pane root = new WebViewPane();
    primaryStage.setScene(new Scene(root, 1024, 768));
    primaryStage.setFullScreen(true);
    primaryStage.show();
}
public static void main(String[] args) {
    launch(args);
}

/**
 * Create a resizable WebView pane
 */
public class WebViewPane extends Pane {

    public WebViewPane() {
        VBox.setVgrow(this, Priority.ALWAYS);
        setMaxWidth(Double.MAX_VALUE);
        setMaxHeight(Double.MAX_VALUE);

        WebView view = new WebView();
        view.setMinSize(500, 400);
        view.setPrefSize(500, 400);
        final WebEngine eng = view.getEngine();
        eng.load("http://google.com");
        //final TextField locationField = new TextField("http://www.google.com");
        //locationField.setMaxHeight(Double.MAX_VALUE);
        Button goButton = new Button("Refresh");
        goButton.setDefaultButton(true);

        EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent event) {
                eng.reload();
            }
        };
        goButton.setOnAction(goAction);

        GridPane grid = new GridPane();
        grid.setVgap(0);
        grid.setHgap(0);

        GridPane.setConstraints(goButton,2,0,2,1, HPos.RIGHT, VPos.BOTTOM, Priority.ALWAYS, Priority.ALWAYS);
        GridPane.setConstraints(view, 0, 0, 2, 1, HPos.CENTER, VPos.CENTER, Priority.SOMETIMES, Priority.SOMETIMES);
        grid.getColumnConstraints().addAll(
                new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
                new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true)
        );
        grid.getChildren().addAll(goButton, view);
        getChildren().add(grid);
    }

    @Override protected void layoutChildren() {
        List<Node> managed = getManagedChildren();
        double width = getWidth();
        double height = getHeight();
        double top = getInsets().getTop();
        double right = getInsets().getRight();
        double left = getInsets().getLeft();
        double bottom = getInsets().getBottom();
        for (int i = 0; i < managed.size(); i++) {
            Node child = managed.get(i);
            layoutInArea(child, left, top,
                           width - left - right, height - top - bottom,
                           0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
        }
    }
}

}

4

1 に答える 1