子类化JavaFX Stage/Scene [英] Subclassing JavaFX Stage / Scene

查看:92
本文介绍了子类化JavaFX Stage/Scene的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Swing并且是JavaFX的新手,我尝试将Java FX舞台和场景归为子类.但是我很快遇到了问题,例如在初始化期间找不到init方法的init方法.

Coming from Swing and being new to JavaFX I tried to subclass Java FX Stages and Scenes. However I quickly run into problems, like the init method not being of my subclassed sceen not being found during the initialization.

所以我想知道:Java FX Stages和Sceens是否被子类化,就像在Swing中将JFrames和JPanels子类化一样?还是不鼓励这样做?

So I was wondering: Are Java FX Stages and Sceens to be subclassed like one would subclass JFrames and JPanels in Swing or is this discouraged?

推荐答案

您可以以几乎相同的方式将SceneStage以及许多其他FX库类作为子类.我不确定我会推荐它,而且它似乎并不是官方教程中任何示例中出现的样式. (实际上,我很久以前就停止在我的大多数摆动代码中使用JFrameJPanel的子类,而在FX示例中更喜欢使用样式.)

You can subclass Scene and Stage and many other FX library classes in pretty much the same way. I'm not sure I'd recommend it, and it doesn't seem to be a style that appears in any of the examples from the official tutorials. (In fact, I long ago stopped using subclasses of JFrame and JPanel in the vast majority of my swing code, preferring instead more the style in the FX examples.)

但是肯定有可能:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SubclassingExample extends Application {

    @Override
    public void start(Stage defaultStageIgnored) {
        Stage stage = new MyStage();
        stage.show();
    }

    public static class MyStackPane extends StackPane{
        public MyStackPane() {
            getChildren().add(new Label("Hello World"));
        }
    }

    public static class MyScene extends Scene {
        public MyScene() {
            super(new MyStackPane(), 250, 75);
        }
    }

    public static class MyStage extends Stage {
        public MyStage() {
            setScene(new MyScene());
        }
    }

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

这篇关于子类化JavaFX Stage/Scene的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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