使用focusedproperty()监听器时出现问题

时间:2017-11-02 00:31:22

标签: java canvas javafx

我写了一个由以下GUI组成的应用程序 该应用程序的目标是在画布上添加一个蓝色矩形到左侧的黑色窗格。因此,我写了一个类预览(也包含黑色窗格)

public class Preview
{
    Canvas can=new Canvas();
    GraphicsContext gc=can.getGraphicsContext2D();
    Pane canvas=new Pane();

    int WIDTH=1920;
    int HEIGHT=1080;
    int y=50;

    public Node area()
    {
       BorderPane container=new BorderPane();
       ScrollPane sp = new ScrollPane(can);
       sp.setFitToHeight(true);
       sp.setFitToWidth(true);

       sp.setStyle("-fx-background: black;");
       container.setCenter(sp);
       return container;
    }

    public void placeCanvas()
    {
        can.setHeight(100);
        can.setWidth(100);
        gc.setFill(Color.BLUE);
        gc.fillRect(0,0,100,100);
        canvas.getChildren().add(can);
    }
}

我在GUI类中创建了一个预览对象。当我尝试在我的GUI类中调用preview.placeCanvas()时,它按预期工作,产生blue rectangle 但是,最终目标是在顶部文本字段(“标题”)失去焦点时使矩形显示。

//Layout of Application
    root.setCenter(preview.area());
    root.setRight(sp);
    root.setBottom(btns);

    //preview.placeCanvas();

    title.focusedProperty().addListener((obs, outFocus, inFocus) ->
    {
        if(outFocus)
        {
            preview.placeCanvas();
            Alert placed=new Alert(AlertType.CONFIRMATION,"Placed Canvas!",ButtonType.OK);
            placed.showAndWait();
        }   
    });

当我运行上面的代码时,警告弹出,告诉我,我输入了if语句,但是我看到no blue rectangle

非常感谢帮助!

P.S。如果有人有兴趣重现这个问题,下面是完整的申请代码:)

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class GUI extends Application
{

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

    @Override
    public void start(Stage frame) throws Exception 
    {   
        VBox main=new VBox();
        ScrollPane sp = new ScrollPane(main);

        Label header=new Label();
        Label performers=new Label();
        Label crew=new Label();

        TextField title=new TextField();
        TextField subtitle=new TextField();
        TextField roleLabel=new TextField();
        TextField role=new TextField();
        TextField actor=new TextField();
        TextField crewLabel=new TextField();
        TextField job=new TextField();
        TextField name=new TextField();

        Button save=new Button("Speichern ->");
        Button clear=new Button("Clear");

        BorderPane head=new BorderPane();
        BorderPane performersPane=new BorderPane();
        BorderPane crewPane=new BorderPane();
        BorderPane btns=new BorderPane();

        GridPane performersGrid=new GridPane();
        GridPane crewGrid=new GridPane();

        ColumnConstraints col1=new ColumnConstraints();
        ColumnConstraints col2=new ColumnConstraints();
        col1.setPercentWidth(60);

        //Preview Area
        Preview preview=new Preview();

        //Root Pane
        BorderPane root=new BorderPane();

        frame.setTitle("End Credits Generator");

        //VBox Elements
        header.setText("Titel/Untertitel");
        title.setPromptText("Titel des Films eingeben");
        subtitle.setPromptText("Untertitel des Films eingeben");

        performers.setText("Schauspieler");
        roleLabel.setPromptText(performers.getText()+" ändern");
        role.setPromptText("Name des Schauspieler eingeben");
        actor.setPromptText("Name des Schauspieler eingeben");

        crew.setText("Crew");
        crewLabel.setPromptText(crew.getText()+" ändern");
        job.setPromptText("Name des Departments eingeben");
        name.setPromptText("Name der Mitarbeiter eingeben");

        BorderPane.setAlignment(header,Pos.TOP_CENTER);
        BorderPane.setMargin(header,new Insets(5,0,5,0));
        BorderPane.setMargin(title,new Insets(5,0,5,0));
        BorderPane.setMargin(subtitle,new Insets(5,0,5,0));
        head.setTop(header);
        head.setCenter(title);
        head.setBottom(subtitle);

        performersGrid.getColumnConstraints().addAll(col1,col2);
        GridPane.setHalignment(performers,HPos.CENTER);
        performersGrid.add(performers,0,0);
        performersGrid.add(roleLabel,1,0);
        performersGrid.add(role,0,1,2,1);
        performersGrid.add(actor,0,2,2,1);
        performersPane.setCenter(performersGrid);

        crewGrid.getColumnConstraints().addAll(col1,col2);
        GridPane.setHalignment(crew,HPos.CENTER);
        crewGrid.add(crew,0,0);
        crewGrid.add(crewLabel,1,0);
        crewGrid.add(job,0,1,2,1);
        crewGrid.add(name,0,2,2,1);
        crewPane.setCenter(crewGrid);

        btns.setPadding(new Insets(10));
        btns.setRight(save);
        btns.setLeft(clear);

        //Layout of VBox
        main.setPrefWidth(500);
        main.setAlignment(Pos.TOP_CENTER);
        main.getChildren().addAll(head,new Separator(),performersPane,new Separator(),crewGrid,new Separator());

        //Layout of Application
        root.setCenter(preview.area());
        root.setRight(sp);
        root.setBottom(btns);

        //preview.placeCanvas();

        title.focusedProperty().addListener((obs, outFocus, inFocus) ->
        {
            if(outFocus)
            {
                preview.placeCanvas();
                Alert placed=new Alert(AlertType.CONFIRMATION,"Placed Canvas!",ButtonType.OK);
                placed.showAndWait();
            }   
        });

        Scene ui=new Scene(root);
        main.requestFocus();
        frame.setScene(ui);
        frame.setMaximized(true);

        frame.show();
    }
}

import javafx.scene.Node;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;

public class Preview
{
    Canvas can=new Canvas();
    GraphicsContext gc=can.getGraphicsContext2D();
    Pane canvas=new Pane();

    int WIDTH=1920;
    int HEIGHT=1080;
    int y=50;

    public Node area()
    {
        BorderPane container=new BorderPane();
        ScrollPane sp = new ScrollPane(can);
        sp.setFitToHeight(true);
        sp.setFitToWidth(true);

        sp.setStyle("-fx-background: black;");
        container.setCenter(sp);
        return container;
    }

    public void placeCanvas()
    {
        can.setHeight(100);
        can.setWidth(100);
        gc.setFill(Color.BLUE);
        gc.fillRect(0,0,100,100);
        canvas.getChildren().add(can);
    }
}

0 个答案:

没有答案