【问题标题】:Javafx: How to use a driver file for a source file to show shape createdJavafx:如何使用源文件的驱动程序文件来显示创建的形状
【发布时间】:2019-12-04 18:12:14
【问题描述】:

我正在尝试了解针对我正在处理的某个问题提供的驱动程序文件。我刚开始学习 javafx 并尝试在驱动程序文件中创建一个形状,但是初始化、鼠标事件等的额外代码在源文件中。我创建了一个函数,它返回矩形的初始化 X 和我的驱动程序文件中的 system.out.println,所以我知道它们是连接的。但是,每当我运行我的驱动程序文件时,我都会得到一个没有形状的空白屏幕。有人可以告诉我我在场景/根目录/舞台显示方面做错了什么。

这是我的代码:

Multishape.java:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class Multishape extends Group {
//Declaring variables here gives them greater scope

private Rectangle rectangle;
private Circle circle;
Group root = new Group();

public Multishape (double x, double y, double len){
  rectangle = new Rectangle(len, len, Color.BLUE);
  rectangle.setX(x);
  rectangle.setY(y);
  circle = new Circle(len, Color.RED);    

}

//@Override
public void start(Stage primaryStage) {
    //rectangle.setOnMouseClicked(handleMouseClick);//Set mouse click handler
    //circle.setOnMouseClicked(handleMouseClick);//Set mouse click handler

    root.getChildren().add(rectangle);//Set initial shape.

    Scene scene = new Scene(root, 500, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
}


double getLen(){
  return (rectangle.getX());
}

}

MultishapeDriver.java:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.input.KeyEvent;
import javafx.event.EventHandler;

public class MultishapeDriver extends Application
{
  public static void main(String[] args)
  {
    launch(args);
  }

  public void start(Stage stage)
  {
    stage.setTitle("Multishape lab");
    Group root = new Group();
    Multishape shape = new Multishape(320, 240, 40);
    root.getChildren().add(shape);
//stage.addEventHandler(KeyEvent.KEY_TYPED, shape.getKeyHandler());

System.out.println(shape.getLen());
stage.setScene(new Scene(root, 640, 480));
stage.show();
  }
}

【问题讨论】:

  • 您永远不会将任何孩子添加到Multishape。这样,对象对渲染结果没有影响。我希望 this.getChildren().add(rectangle); 在构造函数的末尾。 root 字段和 start 方法应该从 Multishape 类中删除,如果我正确理解这个类背后的意图。顺便说一句:不知道你为什么在这里使用术语“驱动程序”。我希望司机完全不同。

标签: java javafx shapes scene stage


【解决方案1】:

您的 Multishape 是一个 Group 类型的容器。 但是在它的构造函数中,你只创建了 2 个对象,你从来没有将它们添加到你的 multishape 中。 Sou 您需要将它们与getChildren().add(rectangele) 等一起添加到您的多重形状中。

您的 Multishape 构造函数将如下所示:

public Multishape (double x, double y, double len){
  rectangle = new Rectangle(len, len, Color.BLUE);
  rectangle.setX(x);
  rectangle.setY(y);
  circle = new Circle(len, Color.RED);    
  getChildren().add(rectangle);
  getChildren().add(circle);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多