[英]Javafx load a new fxml file into a scrollpane
以下代碼顯示了如何使用FXMLLoader將String轉換為某些FXML對象。 然后,通常的GetChildren()。Add(XX)可用於分配給您需要的任何位置。
羅伯特
package ic.ac.uk.relationshipvisualiser.app;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class tmpTest extends Application {
public static void main(String[] args) {
System.out.println("Start tmpTest");
launch(args);
System.out.println("Start tmpTest");
}
final Group m_root = new Group();
@Override
public void start(Stage primaryStage) throws Exception {
String sample_fxml =
"<?import javafx.scene.control.Label?>" +
"<?import javafx.scene.Group?>" +
"<Group xmlns:fx=\"http://javafx.com/fxml\">" +
" <Label fx:id=\"Name\" style=\"-fx-font-weight: bold;\" alignment=\"CENTER\">It worked</Label>" +
"</Group>";
InputStream stream = new ByteArrayInputStream(sample_fxml.getBytes("UTF-8"));
FXMLLoader l = new FXMLLoader();
Group mG = (Group) l.load(stream);
m_root.getChildren().add(mG);
primaryStage.setScene(new Scene(m_root));
primaryStage.show();
}
}
在我之前的回答中,它顯示了從文件中讀取的內容:首先創建一個文件c:\\ test.fxml,其中包含:
<?import javafx.scene.control.Label?>
<?import javafx.scene.Group?>
<Group xmlns:fx="http://javafx.com/fxml">
<Label fx:id="Name" style="-fx-font-weight: bold;" alignment="CENTER">It worked</Label>
</Group>
接下來使用以下代碼:
package ic.ac.uk.relationshipvisualiser.app;
import java.io.FileInputStream;
import java.io.InputStream;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class tmpTest extends Application {
public static void main(String[] args) {
System.out.println("Start tmpTest");
launch(args);
System.out.println("Start tmpTest");
}
final Group m_root = new Group();
@Override
public void start(Stage primaryStage) throws Exception {
InputStream stream = new FileInputStream("c:\\test.fxml");
FXMLLoader l = new FXMLLoader();
Group mG = (Group) l.load(stream);
m_root.getChildren().add(mG);
primaryStage.setScene(new Scene(m_root));
primaryStage.show();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.