繁体   English   中英

Java,使用javaFX作为主菜单,然后切换到游戏本身的JFrame

[英]Java, Using javaFX for the main menu then switching over to JFrame for the game itself

我在这里问是否可以在游戏的主菜单中使用JavaFX,然后切换到游戏本身的JFrame。

我想这样做的原因是因为我知道如何在JavaFX中而不是在JFrame中制作漂亮的游戏菜单,而JavaFX看起来比JFrame还要漂亮。

我将非常感谢您给我的任何帮助。

可以做到这一点:您只需要确保对所有内容使用正确的线程即可。 特别是,请确保您在AWT事件调度线程上启动Swing应用程序。

这是一个简单的例子。

SwingApp:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SwingApp extends JFrame {

    public SwingApp() {
        setLayout(new BorderLayout());
        add(new JLabel("This is the Swing App", JLabel.CENTER), BorderLayout.CENTER);
        JButton quitButton = new JButton("Exit");
        quitButton.addActionListener(e -> System.exit(0));
        add(quitButton, BorderLayout.SOUTH);
        setSize(600, 600);
        setLocationRelativeTo(null);
        setVisible(true);
    }
}

接着

import javax.swing.SwingUtilities;

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

public class LaunchSwingFromFX extends Application {

    @Override
    public void start(Stage primaryStage) {

        Platform.setImplicitExit(false);

        Button launch = new Button("Launch");
        launch.setOnAction(e -> {
            SwingUtilities.invokeLater(SwingApp::new);
            primaryStage.hide();
        });
        StackPane root = new StackPane(launch);
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM