【问题标题】:JavaFX TabPane and resizing parent StageJavaFX TabPane 和调整父级的大小
【发布时间】:2015-07-21 03:26:10
【问题描述】:

我有一个带有可变复选框列表的阶段。当列表更改时,舞台必须调整大小。这在大多数情况下都可以正常工作,在将所有复选框添加到 VBox 之后,您然后调用 stage.sizeToScene() 并相应地调整舞台大小。但是,如果您将所有这些都放在 TabPane stage.sizeToScene() 内的 Tab 中,则不再有效。我有点不知道为什么会这样。任何人有一个好的工作或解决方案?

一些示例代码如下。只需将 withTabs 字段更改为 false 或 true,即可显示没有和使用 TabPane 的情况。

感谢您的帮助。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TabResizeError extends Application {

boolean withTabs=false;

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

@Override
public void start(Stage primaryStage) {

   primaryStage.setTitle("Hello World!");


   //holds variable number of check boxes
   VBox channelBox=new VBox();
   channelBox.setSpacing(5);

   //create controls
   HBox controlsHBox=new HBox(); 
   controlsHBox.setSpacing(5);

   Button btn = new Button();
   btn.setText("Resize Test");  

   ComboBox<Integer> comboBox=new ComboBox<Integer>();
   ObservableList<Integer> number=FXCollections.observableArrayList();
   for (int i=0; i<32; i++){
       number.add(i);
   }
   comboBox.setItems(number);
   comboBox.getSelectionModel().select(5);

   controlsHBox.getChildren().addAll(btn, comboBox);

   btn.setOnAction(new EventHandler<ActionEvent>() {

       @Override
       public void handle(ActionEvent event) {
           //remove all check boxes
           channelBox.getChildren().removeAll(channelBox.getChildren());
           //add new check boxes
           for (int i=0; i<comboBox.getSelectionModel().getSelectedItem(); i++){
               CheckBox checkBox=new CheckBox(("Channel "+i));
               channelBox.getChildren().add(checkBox);
           }
           primaryStage.sizeToScene();
       }
   });

   Pane root=null;

   VBox mainPane = new VBox();
   mainPane.getChildren().addAll(controlsHBox,channelBox);
   mainPane.setSpacing(5);
   if (withTabs){
       TabPane tabbedPane=new TabPane();
       Tab tab=new Tab("TestTab");
       tab.setContent(mainPane);
       tabbedPane.getTabs().add(tab);
       root=new BorderPane(tabbedPane);
   }
   else{
       root = mainPane;
   }
   //        root.setTop(controlsHBox);
   //        root.setCenter(channelBox);

    primaryStage.setScene(new Scene(root));
    primaryStage.setMinHeight(300);
    primaryStage.show();
}

}

【问题讨论】:

    标签: javafx tabs resize stage


    【解决方案1】:

    这是我找到的一些信息。请记住,我仍然是一个业余程序员。

    我复制了您的代码并试图找到解决此问题的方法。

    我注意到,tabpane 发生了一些奇怪的事情 - 选项卡的内容会突然变白,返回边界的方法总是返回相同的值,而 sizeToSceneMethod() 就是这样做的 -根据里面的内容设置舞台的宽度和高度。

    现在注意到奇怪的事情后,我通过谷歌挖到find this bug submission,它说它仍然有几年没有解决。

    在那里我找到了一个可以工作的代码 sn-p,但它有这个问题 - 每次都删除并添加标签,你可以直观地看到这个

    private static final void changeTabPaneSize(final TabPane tabPane) { 
        final int index = tabPane.getSelectionModel().getSelectedIndex(); 
        if (index < 0 || tabPane.getTabs().isEmpty()) { 
            return; 
        } 
    
        if (tabPane.getTabs().size() == 1) { 
            final Tab tab = tabPane.getTabs().remove(0); 
            tabPane.getTabs().add(tab); 
        } else if (tabPane.getTabs().size() > 1 && index == 0) { 
            tabPane.getSelectionModel().select(1); 
        } else if (tabPane.getTabs().size() > 1 && index != 0) { 
            tabPane.getSelectionModel().select(1); 
        } 
    
        tabPane.getSelectionModel().select(index); 
    } 
    

    使用此方法然后调用 sizeToScene 确实有效:

    changeTabPaneSize(tabbedPane);
    primaryStage.sizeToScene();//method called after resetting the tab
    

    除此之外,最好的办法是手动设置舞台大小,尽管这种气味很有效:

    primaryStage.sizeToScene();
    double constantFoundByTrialAndError = 70;
    primaryStage.setHeight(mainPane.getHeight()+constantFoundByTrialAndError);
    

    编辑:我现在注意到我回答了一个 2 年前的问题。

    【讨论】:

    • 非常感谢!无论如何都要感谢答案 - 总是有利于其他人阅读。
    • 好吧,我现在在这里 xD
    猜你喜欢
    • 1970-01-01
    • 2014-07-14
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2016-10-17
    • 2018-03-10
    • 1970-01-01
    相关资源
    最近更新 更多