我正在尝试FXML。这是java文件。它位于/ Users / morrison / teaching / java / xml中,并且所有文件都具有读取权限。
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
import java.io.File;
import java.net.URL;
public class Example extends Application
{
private Stage primary;
@Override
public void start(Stage primary)
{
this.primary = primary;
Parent root = null;
try
{
File f = new File("/Users/morrison/teaching/java/xml/");
FXMLLoader loader = new FXMLLoader(f.toURI().toURL());
System.out.println(loader.getLocation());
root = loader.load(getClass().getResource("Example.fxml"));
primary.setScene(new Scene(root, 500, 500));
primary.setTitle("FXML Example");
primary.show();
}
catch(IOException ex){System.err.println("file cant be found");}
}
}
这是.fxml
文件,它位于同一目录中。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.canvas.Canvas?>
<BorderPane xmlns:fx="http://javafx.com/fxml">
<top>
<HBox>
<Button text="Go!" onAction="#handleGo" fx:id="goButton"/>
<Button text="Stop!" onAction="#handleStop" fx:id="stopButton"/>
</HBox>
</top>
<center>
<canvas fx:id="colorField">
</canvas>
</center>
</BorderPane>
运行此程序时,系统提示我找不到FXML文件。
还有其他我想念的东西吗?