【发布时间】: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();
}
}
【问题讨论】: