JavaFX相当于Swing的pack() [英] JavaFX equivalent of Swing's pack()

查看:87
本文介绍了JavaFX相当于Swing的pack()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调整窗口大小以适应窗口的内容。在Swing中有!这不是我应该做的事情,它是如此基本...

解决方案

尝试使用 sizeToScene()我认为这就是你想要的。让我给
一个例子:



  import javafx.application.Application; 
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

公共类NewFXMain扩展Application {

public static void main(String [] args){
launch(args);
}

@Override
public void start(Stage primaryStage){
primaryStage.setTitle(Hello World!);
Button btn = new Button();
btn.setText(Say'Hello World');
btn.setOnAction(new EventHandler< ActionEvent>(){
@Override
public void handle(ActionEvent event){
System.out.println(Hello World!) ;
}
});

StackPane root = new StackPane();
root.getChildren()。add(btn);
primaryStage.setScene(new Scene(root));
primaryStage.sizeToScene();
primaryStage.show();
}
}


I want to resize a window to fit the contents of the window. In Swing there is the pack() method. Is there a similar method to do this in JavaFX?

What I am trying to do is to create a confirmation dialog. When I create the dialog, it is wider than the contents, so I was asking myself if I need something like the pack method.

Here is a screenshot of what is happening:

And here is my code:

mainClass.getPrimaryStage().setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(final WindowEvent e) {
        e.consume();

        final Stage dialog = new Stage();
        dialog.setTitle("Confirm Before Exit");
        dialog.setResizable(false);
        dialog.initOwner(mainClass.getPrimaryStage());
        dialog.initModality(Modality.APPLICATION_MODAL);

        FlowPane buttons = new FlowPane(10,10);
        buttons.setAlignment(Pos.CENTER);
        Button yes = new Button("Yes");
        Button no = new Button("No");
        buttons.getChildren().addAll(yes, no);
        VBox box = new VBox();
        box.setAlignment(Pos.CENTER);
        box.setSpacing(10);
        box.getChildren().addAll(new Label("Do you really want to exit?"), buttons);

        yes.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                Platform.exit();
            }
        });
        no.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                dialog.close();
            }
        });

        Scene s = new Scene(box);
        dialog.setScene(s);
        dialog.show();
    }
});

I hope they implement something like JOptionPane in JavaFX soon! This is not something that I should be doing, it is so basic...

解决方案

Try using sizeToScene() I think this is it what you want. Let me give an example:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class NewFXMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root));
        primaryStage.sizeToScene();
        primaryStage.show();
    }
}

这篇关于JavaFX相当于Swing的pack()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆