如何在java(fx)上启动窗口?

时间:2017-12-25 21:42:40

标签: javafx fxml

我遇到了问题,我在部分代码中放了一个异常,然后返回

  

“抓住了预期:   /Users/delorszimi/Documents/Documents/Java%20CW/JavaFXbasics/bin/Main.fxml:8"

我的fxml文件中的anchorPane部分似乎存在问题,导致它无法启动。我的大多数代码都在......

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;

public class Main extends Application {

static Stage window = new Stage();

@Override
public void start(Stage window) throws Exception {

    try{
    window.setTitle("Sprite Editor");

    BorderPane pane = new BorderPane();
    Scene sc = new Scene(pane,300,300);
    Parent content = FXMLLoader.load(getClass().getClassLoader().getResource("Main.fxml"));
    pane.setCenter(content);
    window.setScene(sc);
    window.show();
    window.setOnCloseRequest(e -> closeWindow());
    }
    catch(Exception e){
        System.err.println("Caught expection : " + e.getMessage());
    }


            }
private void closeWindow() {
    Boolean decision = ConfirmBox.display("Are you sure you want to exit?", "Title");
    if(decision)
    window.close();
}

public static void main(String[] args) {
    launch(args);
}
}

FXML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.layout.AnchorPane?>



<AnchorPane prefHeight="432.0" prefWidth="498.0" 
 xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="MainController">
   <children>
  <Canvas fx:id="Canvas1" height="240.0" layoutX="81.0" layoutY="39.0" 
width="308.0" />
   </children>
</AnchorPane>
package application;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;

public class MainController {


@FXML 
public void initialize(){




    Button botao = new Button("Browse Files");
    Button exit = new Button("Exit");
    exit.setOnAction(e -> System.exit(0));
    StackPane layout = new StackPane();
    layout.getChildren().addAll(botao,exit);

     final Canvas canvas1 = new Canvas(500,300);

        GraphicsContext gc = canvas1.getGraphicsContext2D();
        gc.setStroke(Color.BLACK);
        gc.setLineWidth(1);

        ColorPicker cp = new ColorPicker();
        layout.getChildren().addAll(canvas1,cp);
        Scene sc = new Scene(cp);

        sc.setOnMousePressed(e->{
            gc.beginPath();
            gc.lineTo(e.getSceneX(), e.getSceneY());
            gc.stroke();

        });

        sc.setOnMouseDragged(e->{
            gc.lineTo(e.getSceneX(), e.getSceneY());
            gc.stroke();

        });
    }


    public void size(){
    double width,height;

}


public void fileSelection(ActionEvent evento){




}

}

1 个答案:

答案 0 :(得分:0)

在FXML文件中,您可以使用AnchorPane,但在代码部分中,您可以创建一个borderpane实例,但不要理解这部分内容。并删除getClassLoader()并尝试一次。我不确定,但你可以检查一次。