繁体   English   中英

Javafx将新的fxml文件加载到滚动窗格中

[英]Javafx load a new fxml file into a scrollpane

是否可以将新的fxml文件加载到滚动窗格中

有关更多详细信息,请查看此图像

点击这里查看图片

请帮我..

以下代码显示了如何使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM