- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个 Singleton JavaFX 中的主类但我遇到了困难,因为主类必须扩展 Application ,所以构造函数不能是私有(private)的。
我希望主类是单例,因为我想从任何类访问几个实例方法。
在我当前的代码中,我将主类用作伪单例,因为我不保证该类不会在代码的某些部分再次实例化。
我想知道当您不能使用私有(private)构造函数时,是否有一种合适的方法来创建单例。
这是我的主类的代码:
public final class MainClass extends Application {
private static MainClass instance;
private Stage primaryStage, optionsStage;
@Override
public void start(final Stage primaryStage) {
instance = this;
try {
// Main scene.
{
Parent page = (Parent) FXMLLoader.load(
MainWindowController.class.getResource("main.fxml"));
Scene mainScene = new Scene(page);
primaryStage.setScene(mainScene);
primaryStage.show();
}
// Options scene.
{
optionsStage = new Stage();
optionsStage.setTitle("Options");
optionsStage.setResizable(false);
optionsStage.initModality(Modality.APPLICATION_MODAL);
optionsStage.initOwner(primaryStage);
Parent page = (Parent) FXMLLoader.load(
OptionsWindowController.class.getResource("options.fxml"));
Scene scene = new Scene(page);
optionsStage.setScene(scene);
}
} catch (Exception ex) {
Constants.LOGGER.log(Level.SEVERE, ex.toString());
}
}
/**
* Returns the instance of this class.
* @return
*/
public static MainClass getInstance() {
return instance;
}
public Stage getPrimaryStage() {
return primaryStage;
}
/**
* Returns the options stage.
* @return
*/
public Stage getOptionsStage() {
return optionsStage;
}
/**
* The main() method is ignored in correctly deployed JavaFX
* application. main() serves only as fallback in case the
* application can not be launched through deployment artifacts,
* e.g., in IDEs with limited FX support. NetBeans ignores main().
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
最佳答案
我认为添加如下所示的公共(public)构造函数并删除 start
方法中的 instance = this;
赋值可以解决问题,尽管我不是精通JavaFX。我认为它不会多次尝试实例化您的主类,但如果 API 不提供此类保证,您以后可能会发现自己遇到麻烦。
无论如何...如果您执行以下操作,您会很快发现:
public MainClass(){
super();
synchronized(MainClass.class){
if(instance != null) throw new UnsupportedOperationException(
getClass()+" is singleton but constructor called more than once");
instance = this;
}
}
NetBeans 的编译器(如果您正在使用它)会提示“this
泄漏”,但这将保证您不会多次运行构造函数完成。
关于java - 如何在 JavaFX 中创建单例 MainClass?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13943654/
我有父项目“giftcard-service”,仅用于构建。所有代码都位于名为“giftcard-service-main”的模块中。我能够构建(干净安装)礼品卡服务,但无法部署/运行它,这是通过 m
我如何使用不同的参数多次调用 Maven 主类 org.codehaus.mojo exec-maven-plugin 1.2.1
我有两个 gradle 配置的项目,projectA 和 projectB。 ProjectA是一个Spring Boot项目,有两个主要类,一个在直接运行projectA时运行,另一个在运行proj
我似乎无法找到有关 mainClass 的任何细节选项适用于 sbt 的构建配置。您指定在调用 run 时要使用的类的名称。行动,但它实际上用它做什么?它是否调用类上的方法? 最佳答案 http://
我有一个多模块 Maven 项目,并且 javaFx 有问题(我使用了插件 zenJava)。 当我想构建 jar 时,出现以下错误:无法在项目 XXXX 上执行 objective-c om.zen
我想将 mysql-connector 添加到我的类路径中,但它不起作用: @echo off java -cp ../lib/mysql-connector-java-5.1.18-bin.jar;
我在项目中使用的是Gradle 6.1版。 下面给出了该项目的 build.gradle : plugins { id 'application' } application { ma
有没有办法验证(甚至使用另一个插件)为 maven-jar-plugin 指定的 mainClass 是一个有效的类,甚至更好有一个 public static void main(String[]
我有课 TouchImageView extends ImageView 然后我在这门课上: setOnTouchListener(new OnTouchListener() { @O
我想创建一个 Singleton JavaFX 中的主类但我遇到了困难,因为主类必须扩展 Application ,所以构造函数不能是私有(private)的。 我希望主类是单例,因为我想从任何类访问
我正在尝试从使用 maven 创建的命令行运行 Java10“hello world”示例。 但我收到消息了 Error: Could not find or load main class App
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我在我的 Maven 中指定了一个插件来使用 maven-jar-plugin 构建 jar。 org.apache.maven.plugins maven-jar-plugin
我正在学习 Spring 教程,但一直停留在 creating RestController App 11.4 它只是没有运行。 [INFO] ----------------------------
我正在尝试执行pom.xml有mainClass其中提到但没有被执行。 命令:mvn test -f "path_to_pom.xml\pom.xml" 输出: [INFO] Scanning for
如您所见,我想获取指定学生的总数。 我已经尝试了下面的代码,但我没有得到我正在寻找的东西! //数据库助手 public Cursor MyCredits(String StudentNo){
我必须实现一个工具来读取配置文件并运行配置文件中指定的应用程序/程序。一种用于测试的自动运行程序。我已经实现了一个执行此操作的程序,但是我遇到了依赖墙。在我当前的设计中,工具解析配置文件并检索程序类型
我正在一个项目中的 scala.js 上完成本教程,并且已经有另外两个主类:https://www.scala-js.org/doc/tutorial/basic/index.html 在我的 bui
我正在使用 sbt-native-packager 0.7.0-RC3 来打包我的一些应用程序,并且很多时候我需要与另一个 mainClass 一起打包。 在开发模式中我使用这个: mainClass
我有一个 maven 项目,它通过 maven assembly plugin 生成一个 jar。我想作为控制台应用程序运行。但是,未在 MANIFEST.MF 中设置 MainClass 属性。这是
我是一名优秀的程序员,十分优秀!