JavaFX,外部类扩展窗格,将其添加到主类将无法正常工作 [英] JavaFX, external class extends pane, adding that to main class won't work

查看:153
本文介绍了JavaFX,外部类扩展窗格,将其添加到主类将无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的主要课程:

public class Digital_Analog_Clock_Beta_1 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        double outerBoxWidth = 500, outerBoxHeight = outerBoxWidth / 2.5;
        Rectangle outerBox = new Rectangle(0, 0, outerBoxWidth, outerBoxHeight);
        outerBox.setId("outer-box");

        Rectangle part1 = new Rectangle(0, 0, outerBoxWidth * .35, outerBoxHeight);
        part1.setId("partition");
        Rectangle part2 = new Rectangle(outerBoxWidth * .35, 0, outerBoxWidth * .05, outerBoxHeight);
        part2.setId("partition-alternate");
        Rectangle part3 = new Rectangle(outerBoxWidth * .4, 0, outerBoxWidth * .35, outerBoxHeight);
        part3.setId("partition");
        Rectangle part4 = new Rectangle(outerBoxWidth * .75, 0, outerBoxWidth * .35, outerBoxHeight);
        part4.setId("partition-alternate");

        double bigNumWidth = outerBoxWidth * .35;
        double digitWidth = (.9 * bigNumWidth / 2) * 0.95;
        double digitHeight = .9*outerBoxHeight;


        digit Digit1 = new digit(outerBoxWidth*.1,outerBoxHeight*.1,digitWidth,digitHeight);
        Digit1.bottom.setId("digits");
        Digit1.c.setFill(Color.AQUA);

        Pane pane = new Pane();
        pane.getChildren().add(outerBox);
        pane.getChildren().addAll(part1, part2, part3, part4);
        //pane.setId("background");

        pane.getChildren().add(Digit1);

        Scene scene = new Scene(pane, outerBoxWidth, outerBoxHeight);
        scene.getStylesheets().add(getClass().getResource("styleSheet.css").toExternalForm());

        primaryStage.setTitle("DIgital Analog Clock Beta 1");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

我的数字类:

public class digit extends Pane
{

    double startX, startY, digitWidth, digitHeight, lineWidth;

    public digit(double startX, double startY, double digitWidth, double digitHeight)
    {
        this.startX = startX;
        this.startY = startY;
        this.digitWidth = digitWidth;
        this.digitHeight = digitHeight;
        lineWidth = digitHeight / 20;
        getChildren().addAll(top,middle,bottom,upperLeft,upperRight,lowerLeft,lowerRight, c);
    }

    Polygon top = new Polygon(startX, startY, startX + digitWidth, startY, startX + digitWidth - lineWidth, startY + lineWidth * .95, startX + lineWidth, startY + lineWidth * .95);
    Polygon middle = new Polygon(startX + lineWidth, startY + digitHeight / 2 - lineWidth / 2 + lineWidth * .05,
            startX + digitWidth - lineWidth, startY + digitHeight / 2 - lineWidth / 2 + lineWidth * .05,
            startX + digitWidth - lineWidth * .05, startY + digitHeight, startX + digitWidth - lineWidth, startY + digitHeight / 2 + lineWidth / 2 - lineWidth * .05,
            startX + lineWidth, startY + digitHeight / 2 + lineWidth / 2 - lineWidth * .05, startX + lineWidth * .05, startY + digitHeight / 2);
    Polygon bottom = new Polygon(startX, startY + digitHeight, startX + digitWidth, startY + digitHeight, startX + digitWidth - lineWidth, startY + digitHeight - lineWidth * .95,
            startX + lineWidth, startY + digitHeight - lineWidth * .95);
    Polygon upperLeft = new Polygon(startX, startY, startX + lineWidth * .95, startY + lineWidth, startX + lineWidth * .95, startY + digitHeight / 2 - lineWidth / 2, startX, startY + digitHeight / 2);
    Polygon lowerLeft = new Polygon(startX, startY + digitHeight / 2, startX + lineWidth * .95, startY + digitHeight / 2 + lineWidth / 2, startX + lineWidth * .95, startY + digitHeight - lineWidth,
            startX, startY + digitHeight);
    Polygon upperRight = new Polygon(startX + digitWidth, startY, startX + digitWidth, startY + digitHeight / 2, startX + digitWidth - lineWidth * .95, startY + digitHeight / 2 - lineWidth / 2,
            startX + digitWidth - lineWidth * .95, startY + lineWidth);
    Polygon lowerRight = new Polygon(startX+digitWidth,startY+digitHeight/2,startX+digitWidth,startY+digitHeight,startX+digitWidth-lineWidth*.95,startY+digitHeight-lineWidth,
            startX+digitWidth-lineWidth*.95,startY+lineWidth/2+digitHeight/2);

   Circle c = new Circle(100.0f,100.0f,1000.0f);

}

styleSheet.css:

styleSheet.css :

#outer-box
{
     -fx-fill: #353839; /* Onyx */
}

#outer-box-t
{
    -fx-fill: rgba(0,0,0,0); /* transparent */
}

#partition
{
    -fx-fill: rgba(200,200,205,0.25)  /* #C8C8CD Blue Grey a : 0.5*/
}

#partition-alternate
{
    -fx-fill: rgba(0,0,0,0); /* transparent i.e. Onyx */
}

#background
{
    -fx-background-color: #C53151; /* Dingy Dungeon */
}

#digits
{
    -fx-fill : #66FF66; /* Screamin' Green */   
     -fx-stroke: #C53151; /* Dingy Dungeon */
    -fx-stroke-width : 3;
}

我的输出窗口:

我的数字应该会在我创建的背景之上弹出。我在大班制作了这么多长方形,只是为了根据我的需要来区分我的数字。你可以忽略我创建多边形的部分(所以我甚至在我的digit.java底部创建了一个圆圈,但是在我的输出窗口中看不到digit.java。我在这里问了这个问题: JavaFX,外部课程扩展窗格,将其添加到主类不起作用它适用于我创建的测试用例。不知道我正在处理的这个实际程序有什么问题

my digits are supposed to pop up on top of this background I created. I made these many rectangles in main class just for spacing out my digits according to my needs. you can ignore the part where I create polygons( so I even created a circle at the bottom of my digit.java but nothing from digit.java is visible in my output window. I kind of asked this question over here : JavaFX, external class extends pane, adding that to main class doesn't work and it worked for the test case that I created. no idea what is the problem with this actual program I am working on

更新:
除了我的圈子以外的所有内容都没有显示

update: everything except my circle is not showing

推荐答案

初始化内联的实例变量将在调用构造函数之前初始化,因此,在 top 初始化所有 startx starty digitWidth digitHeight lineW idth 将为零。

Instance variables initialized inline will be initialized before the constructor is called, so at the point where, say, top is initialized all of startx, starty, digitWidth, digitHeight, and lineWidth will be zero.

参见 JLS,第12.5节


在对新创建的对象的引用之前返回结果,处理指示的构造函数以使用以下过程初始化新对象:

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:


  1. 为参数分配参数构造函数用于此构造函数调用的新创建的参数变量。

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

如果此构造函数以同一个构造函数的显式构造函数调用(第8.8.7.1节)开头class(使用this),然后使用这五个相同的步骤评估参数并递归处理构造函数调用。如果该构造函数调用突然完成,则此过程突然完成,原因相同;否则,继续执行步骤5.

If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

此构造函数不以同一类中另一个构造函数的显式构造函数调用开头(使用此方法)。如果此构造函数用于Object以外的类,则此构造函数将以超类构造函数的显式或隐式调用开始(使用super)。使用这五个相同的步骤评估参数并递归处理超类构造函数调用。如果该构造函数调用突然完成,则此过程突然完成,原因相同。否则,继续执行步骤4.

This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

为此类执行实例初始值设定项和实例变量初始值设定项,将实例变量初始值设定项的值分配给相应的实例变量,按从左到右的顺序,它们以文本方式出现在类的源代码中。如果执行任何这些初始值设定项导致异常,则不会处理其他初始化程序,并且此过程会突然完成同样的异常。否则,继续执行步骤5.

Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

执行此构造函数的其余部分。如果执行突然完成,则此过程突然完成,原因相同。否则,此过程正常完成。

Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.


请注意分配实例的值变量初始化器到相应的实例变量(步骤4)发生在执行此构造函数的其余部分之前(步骤5)。

Note that "assigning the values of instance variable initializers to the corresponding instance variables" (step 4) occurs before "Execute the rest of the body of this constructor" (step 5).

移动初始化多边形到构造函数:

Move the initialization of the polygons to the constructor:

public class digit extends Pane {

    double startX, startY, digitWidth, digitHeight, lineWidth;

    Polygon top ;
    Polygon middle ;
    Polygon bottom ;
    Polygon upperLeft ;
    Polygon upperRight ;
    Polygon upperLeft ;
    Polygon lowerLeft ;

    public digit(double startX, double startY, double digitWidth, double digitHeight)
    {
        this.startX = startX;
        this.startY = startY;
        this.digitWidth = digitWidth;
        this.digitHeight = digitHeight;
        lineWidth = digitHeight / 20;
        getChildren().addAll(top,middle,bottom,upperLeft,upperRight,lowerLeft,lowerRight, c);
        this.top = new Polygon(startX, startY, startX + digitWidth, startY, startX + digitWidth - lineWidth, startY + lineWidth * .95, startX + lineWidth, startY + lineWidth * .95);

        // etc...
    }

}

这篇关于JavaFX,外部类扩展窗格,将其添加到主类将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆