将 SceneBuilder 连接到 fxmlloader
Connect SceneBuilder to fxmlloader
我在 IntelliJ 中制作了这个简短的绘图应用程序,并且我第一次尝试使用 SceneBuilder。我在场景生成器中制作的 "sample.fxml" 不会加载到我的 Main-class 中,所以我自己直接在 Main-class 中制作了 Canvas 等。
我对 FXML loader/file 做错了什么?
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
try {
// FXMLLoader load = FXMLLoader.load(getClass().getResource("sample.fxml"));
// load.load();
Group root = new Group();
Controller controller = new Controller();
primaryStage.setTitle("Paint app");
primaryStage.setScene(new Scene(root,800,500));
primaryStage.show();
root.getChildren().add(controller.canvas);
/*METODER I PROGRAMMET */
controller.drawCanvas();
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Controller {
Canvas canvas = new Canvas(800,500);
@FXML
public void drawCanvas(){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setLineWidth(3);
gc.setStroke(Color.BLACK);
System.out.println("drawCanvas");
try {
canvas.setOnMousePressed(event -> {
System.out.println("Mouse click");
gc.beginPath();
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
canvas.setOnMouseDragged(event -> {
System.out.println("Mouse dragged");
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<Canvas id="canvas" fx:id="canvas" height="359.0" onMouseClicked="#drawCanvas" onMouseDragged="#drawCanvas" width="394.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
关于 scenebuilder 的文档不多。
谢谢!
我使用了您的 fxml 和控制器,并将它们添加到我在 Netbeans 中创建的项目中,并且成功了。
This is your problem:
变化:
Canvas canvas = new Canvas(800,500);
至:
@FXML Canvas canvas;
在您的控制器中
Sample.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Sample extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));//You may need so make the s lowercase.
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Controller {
@FXML Canvas canvas;
@FXML
public void drawCanvas(){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setLineWidth(3);
gc.setStroke(Color.BLACK);
System.out.println("drawCanvas");
try {
canvas.setOnMousePressed(event -> {
System.out.println("Mouse click");
gc.beginPath();
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
canvas.setOnMouseDragged(event -> {
System.out.println("Mouse dragged");
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
}
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<Canvas id="canvas" fx:id="canvas" height="359.0" onMouseClicked="#drawCanvas" onMouseDragged="#drawCanvas" width="394.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
不太确定为什么,但我尝试将其更改几次,它确实有效:谢谢@Sedrick Jefferson
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
try {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Paint app");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}
我在 IntelliJ 中制作了这个简短的绘图应用程序,并且我第一次尝试使用 SceneBuilder。我在场景生成器中制作的 "sample.fxml" 不会加载到我的 Main-class 中,所以我自己直接在 Main-class 中制作了 Canvas 等。 我对 FXML loader/file 做错了什么?
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
try {
// FXMLLoader load = FXMLLoader.load(getClass().getResource("sample.fxml"));
// load.load();
Group root = new Group();
Controller controller = new Controller();
primaryStage.setTitle("Paint app");
primaryStage.setScene(new Scene(root,800,500));
primaryStage.show();
root.getChildren().add(controller.canvas);
/*METODER I PROGRAMMET */
controller.drawCanvas();
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Controller {
Canvas canvas = new Canvas(800,500);
@FXML
public void drawCanvas(){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setLineWidth(3);
gc.setStroke(Color.BLACK);
System.out.println("drawCanvas");
try {
canvas.setOnMousePressed(event -> {
System.out.println("Mouse click");
gc.beginPath();
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
canvas.setOnMouseDragged(event -> {
System.out.println("Mouse dragged");
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<Canvas id="canvas" fx:id="canvas" height="359.0" onMouseClicked="#drawCanvas" onMouseDragged="#drawCanvas" width="394.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
关于 scenebuilder 的文档不多。 谢谢!
我使用了您的 fxml 和控制器,并将它们添加到我在 Netbeans 中创建的项目中,并且成功了。
This is your problem:
变化:
Canvas canvas = new Canvas(800,500);
至:
@FXML Canvas canvas;
在您的控制器中
Sample.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Sample extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));//You may need so make the s lowercase.
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Controller {
@FXML Canvas canvas;
@FXML
public void drawCanvas(){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setLineWidth(3);
gc.setStroke(Color.BLACK);
System.out.println("drawCanvas");
try {
canvas.setOnMousePressed(event -> {
System.out.println("Mouse click");
gc.beginPath();
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
canvas.setOnMouseDragged(event -> {
System.out.println("Mouse dragged");
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
}
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<Canvas id="canvas" fx:id="canvas" height="359.0" onMouseClicked="#drawCanvas" onMouseDragged="#drawCanvas" width="394.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
不太确定为什么,但我尝试将其更改几次,它确实有效:谢谢@Sedrick Jefferson
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
try {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Paint app");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}catch (Exception e){
System.out.println(e);
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}