JavaFX,外部类扩展 Pane ,将其添加到主类将不起作用

标签 java css 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);
    
}

样式表.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;
}

我的输出窗口:

enter image description here

我的数字应该会在我创建的背景之上弹出。我在主类中制作了这么多矩形,只是为了根据我的需要分隔我的数字。你可以忽略我创建多边形的部分(所以我什至在我的 digit.java 的底部创建了一个圆圈,但在我的输出窗口中看不到 digit.java 的任何内容。我在这里问了这个问题:JavaFX, external class extends pane, adding that to main class doesn't work 和它适用于我创建的测试用例。不知道我正在处理的这个实际程序有什么问题

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

最佳答案

内联初始化的实例变量构造函数被调用之前被初始化,所以在那个点,比如说,top被初始化所有startxstartydigitWidthdigitHeightlineWidth 将为零。

参见 JLS, section 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. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. 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.

  3. 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.

  4. 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.

  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 步)之前。

将多边形的初始化移至构造函数:

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,外部类扩展 Pane ,将其添加到主类将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45288767/

相关文章:

html - 掌握语义标记和良好的 CSS 有多难?

java - JavaFX8 标签中的颜色不匹配

java - Spring Security 3 - 总是返回错误 302

java - JAXB 父类(super class)字段未反序列化

java - 自定义对象列表作为泛型方法的参数

jquery - 为什么我的 div 会损坏,为什么我的弹跳效果不起作用?

css - 加载 twitter bootstrap responsive.css 时如何防止特定元素的响应行为?

java - 在 paintComponent 中使用变量

java - 删除条形图中条形之间的空格?

java - 使用自定义标题栏管理 Windows Aero 震动功能