资源简介
一个使用FXMLLoader加载fxml文件的例子,可以加载当前包之外的fxml文件。详细内容参考:http://blog.csdn.net/lngrvr/article/details/17067605
代码片段和文件信息
package sample.fxml;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.Fxml;
import javafx.fxml.FxmlLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class FxmlMain extends Application {
private URL location;
private Stage stage;
public Stage getStage(){
return stage;
}
public Pane loadFxml(InputStream in) {
FxmlLoader loader = new FxmlLoader();
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(location);
try {
Pane pane = (Pane)loader.load(in);
Page page = (Page)loader.getController();
page.setMain(this);
return pane;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public void popupWindow(Pane root Stage stage) {
showStage(root stage this.stage);
}
private void showStage(Pane root Stage stage Stage parent) {
Scene scene = new Scene(root);
stage.setScene(scene);
stage.sizeToScene();
stage.setResizable(false);
if (parent != null) {
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(parent);
}
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
@Override
public void start(Stage stage) throws Exception {
String fxml = “FxmlSelection.fxml“;
location = FxmlMain.class.getResource(fxml);
Pane pane = loadFxml(FxmlMain.class.getResourceAsStream(fxml));
if (pane == null)
return;
showStage(pane stage null);
this.stage = stage;
}
public static void main(String[] args) {
Application.launch(FxmlMain.class (java.lang.String[])null);
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 1980-01-01 00:00 src\
目录 0 2013-12-02 10:06 fxm
目录 0 2013-12-02 09:49 src\sample\
目录 0 2013-12-02 09:01 src\sample\fxm
文件 2259 2013-12-02 09:48 src\sample\fxm
文件 1545 2013-12-02 09:06 src\sample\fxm
文件 1769 2013-12-02 09:04 src\sample\fxm
文件 40 2013-11-29 16:10 src\sample\fxm
文件 121 2013-12-02 09:03 src\sample\fxm
文件 370 2013-12-02 09:04 src\sample\fxm
文件 739 2013-11-30 07:52 fxm
文件 654 2013-11-29 15:37 fxm
评论
共有 条评论