gpt4 book ai didi

JavaFX TabPane,Tab.setContent() 不起作用

转载 作者:行者123 更新时间:2023-12-01 09:35:20 24 4
gpt4 key购买 nike

问题是我想在 GridPaneTab 之间传输项目所以我首先将该项目添加到选项卡中。

然后我将相同的项目添加到 GridPane 中。直到这里它都运行良好。但是后来我想将该项目添加回选项卡并且我正在使用

tab.setContent(item);

但它不起作用。该项目既没有从 GridPane 中删除,也没有作为内容添加到选项卡中。为什么会发生这种情况?

The .java file:

package pack;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Starter extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(new UI()));
primaryStage.centerOnScreen();
primaryStage.show();
}

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

/**
* This class contains the graphical interface
*
* @author Alexander
*
*/
public class UI extends BorderPane implements Initializable {

@FXML
private Tab tab;

@FXML
private Button item;

@FXML
private GridPane gridPane;

@FXML
private Button transferButton;

boolean onGridPane = false;

/**
* Constructor
*/
public UI() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("UI.fxml"));
loader.setController(this);
loader.setRoot(this);

try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("Main Controller has been initialized....");

transferButton.setOnAction(a -> {
if (!onGridPane) {
gridPane.add(item, 0, 0);
onGridPane = true;
transferButton.setText("<-Move to Tab");
} else {
tab.setContent(item);
onGridPane = false;
transferButton.setText("Move to GridPane->");
}
});
}

}

}

And the .fxml file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<fx:root prefHeight="322.0" prefWidth="538.0" type="BorderPane" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
<top>
<TabPane prefHeight="141.0" prefWidth="538.0" style="-fx-border-color: red;" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
<tabs>
<Tab fx:id="tab" text="Tab">
<content>
<Button fx:id="item" mnemonicParsing="false" text="Moving Item" />
</content>
</Tab>
</tabs>
</TabPane>
</top>
<bottom>
<GridPane fx:id="gridPane" gridLinesVisible="true" style="-fx-border-color: red;" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</bottom>
<right>
<Button fx:id="transferButton" mnemonicParsing="false" text="Move to GridPane-&gt;" BorderPane.alignment="CENTER">
<font>
<Font size="16.0" />
</font></Button>
</right>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<center>
<Label prefHeight="82.0" prefWidth="340.0" style="-fx-font-weight: bold; -fx-font-size: 15;" text="Above is a [TabPane] and Below is a [GridPane]" BorderPane.alignment="CENTER" />
</center>
</fx:root>

最佳答案

节点不能在场景图中出现两次; API 明确禁止再次将已经属于场景图一部分的节点添加到场景图,因此如果您尝试这样做,则行为是未定义的。

您需要先从场景图中删除该按钮,然后再将其添加到其他位置:

transferButton.setOnAction(a -> {
if (!onGridPane) {
tab.setContent(null);
gridPane.add(item, 0, 0);
onGridPane = true;
transferButton.setText("<-Move to Tab");
} else {
gridPane.getChildren().remove(item);
tab.setContent(item);
onGridPane = false;
transferButton.setText("Move to GridPane->");
}
});

关于JavaFX TabPane,Tab.setContent() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39023718/

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