gpt4 book ai didi

JavaFX TitledPane 需要多次单击才能展开,场景不更新

转载 作者:行者123 更新时间:2023-12-02 09:01:23 27 4
gpt4 key购买 nike

我遇到了一个奇怪的问题/错误,第一次单击时 TitledPane 不会展开,它需要多次单击才能展开。除此之外,嵌套的 TitledPanes 根本不会展开,除非您多次单击它们并在选项卡之间切换。

所以代码可以工作,但不正常。

如果专家可以判断这是一个错误还是我做错了什么,那将是一个很大的帮助。谢谢!

package subtables;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Subtables extends Application
{

@Override
public void start(Stage primaryStage)
{
Accordion accordion = new Accordion();

TitledPane pane = new TitledPane();
VBox root = new VBox();

HBox titles = new HBox();
titles.setPadding(new Insets(5, 5, 5, 24));
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());

FlowPane graphic = new FlowPane();
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());

pane.setGraphic(graphic);

pane.setOnMouseClicked((e) ->
{
TabPane tabPane = new TabPane();
Tab tab1 = new Tab("Sub Table 1", getSubWindow());
Tab tab2 = new Tab("Sub Table 2", new Label("Text inside a tab."));
Tab tab3 = new Tab("Sub Table 3", new Label("Text inside a tab."));
Tab tab4 = new Tab("Sub Table 4", new Label("Text inside a tab."));

tabPane.getTabs().add(tab1);
tabPane.getTabs().add(tab2);
tabPane.getTabs().add(tab3);
tabPane.getTabs().add(tab4);

VBox vBox = new VBox(tabPane);
pane.setContent(vBox);
});

accordion.getPanes().add(pane);
root.getChildren().add(titles);
root.getChildren().add(accordion);

primaryStage.setTitle("Hello World!");
Scene scene = new Scene(root, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
}

public TextField getTextField()
{
TextField textField = new TextField();
textField.setPromptText("Value");
textField.setPrefWidth(50);
return textField;
}

public VBox getSubWindow()
{
Accordion accordion = new Accordion();

TitledPane pane = new TitledPane();
VBox root = new VBox();

HBox titles = new HBox();
titles.setPadding(new Insets(5, 5, 5, 24));
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());
titles.getChildren().add(getTextField());

FlowPane graphic = new FlowPane();
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());
graphic.getChildren().add(getTextField());

pane.setGraphic(graphic);
pane.setOnMouseClicked((e) ->
{
TabPane tabPane = new TabPane();
Tab tab1 = new Tab("Sub Table 1", getSubWindow());
Tab tab2 = new Tab("Sub Table 2", new Label("Text inside a tab."));
Tab tab3 = new Tab("Sub Table 3", new Label("Text inside a tab."));
Tab tab4 = new Tab("Sub Table 4", new Label("Text inside a tab."));

tabPane.getTabs().add(tab1);
tabPane.getTabs().add(tab2);
tabPane.getTabs().add(tab3);
tabPane.getTabs().add(tab4);

VBox vBox = new VBox(tabPane);
pane.setContent(vBox);
});

accordion.getPanes().add(pane);
root.getChildren().add(titles);
root.getChildren().add(accordion);

return root;
}

/**
* Main function to launch the application.
*/
public static void main(String[] args)
{
launch(args);
}

}

最佳答案

setConent() 方法被调用 onMouseClicked。首先单击 TitledPane 标题将其展开,然后处理鼠标单击。鼠标点击的核心功能是设置内容。它还没有渲染。第二次单击折叠 TitledPane。第三次单击将展开 TitledPane,其中的内容现在可见。将创建 VBox 选项卡移出 setOnMouseClicked() 方法。

正确的代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TitledPane;
import javafx.stage.Stage;

public class TitledPaneApp extends Application {

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

@Override
public void start(Stage stage) throws Exception {
TabPane subTabPane = new TabPane(new Tab("Sub tab1", new Label("Sub content1")), new Tab("Sub tab2", new Label("Sub content2")));
TitledPane subTitledPane = new TitledPane("Sub titled pane", subTabPane);
Accordion subAccordion = new Accordion(subTitledPane);

Tab tab1 = new Tab("Tab1", subAccordion);
Tab tab2 = new Tab("Tab2", new Label("Content2"));
TabPane tabPane = new TabPane(tab1, tab2);
TitledPane titledPane = new TitledPane("Titled pane", tabPane);
Accordion accordion = new Accordion(titledPane);

Scene scene = new Scene(accordion, 400, 400);
stage.setScene(scene);
stage.show();
}
}

关于JavaFX TitledPane 需要多次单击才能展开,场景不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60134856/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com