JavaFX Project не отображается - PullRequest
0 голосов
/ 21 марта 2020

Ниже у меня есть код для запуска JavaFX GUI для проекта, над которым я работаю. Однако, когда я go для выполнения классов, он застревает. Все, что он печатает, это Запуск MCPY GUI и просто сидит какая-нибудь помощь?

Класс запуска

public class MCPY {
    public static void main(String args[]) {
        System.out.println("Starting MCPY GUI...");   
        Main.main(args);
    }
}

Main

package com.mcpy.gui;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        FXMLLoader loader = new FXMLLoader(getClass().getResource("styles/Main.fxml"));
        Parent root;

        try {
            root = loader.load();
        } catch (IOException ioe) {
            // log exception
            return;
        }

        //init the connection to the controller class
        MainController MainController = loader.getController();
        MainController.setMain(this);

       //load the scene
       primaryStage.setTitle("Minecraft Launcher Python");
       primaryStage.setScene(new Scene(root, 800, 500));
       primaryStage.show();

    };


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

Main.F XML


<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.control.Button?>

<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="Minecraft Launcher Python Login Screen" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mcpy.gui.MainController">
    <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
            <children>
                <ImageView fitHeight="135.0" fitWidth="119.0" layoutX="14.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
                    <image>
                    </image>
                </ImageView>
                <Button id="play" layoutX="231.0" layoutY="176.0" mnemonicParsing="false" onAction="#playButtonActionEvent" text="Play" />
                <Button id="login" layoutX="313.0" layoutY="176.0" mnemonicParsing="false" onAction="#loginButtonEvent" text="Login" />
                <Button id="create_account" layoutX="77.0" layoutY="176.0" mnemonicParsing="false" onAction="#create_accountButtonEvent" text="Create Account" />
            </children></AnchorPane>
    </content>
</TitledPane>

MainController

package com.mcpy.gui;


import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;

import com.mcpy.gui.Play;
import com.mcpy.gui.Login;
import com.mcpy.gui.CreateAccount;
/* Logic for the main gui. DO NOT EDIT. */
public class MainController {

    @SuppressWarnings("unused")
    private Main main;

    public void setMain(Main main) {
        this.main = main;

    }
    @FXML
    private static Button login;

    @FXML
    private static Button create_account;

    @FXML 
    private static Button play;

    @FXML
    public void loginButtonEvent(ActionEvent lbe) {
        String[] args = (null);        
        Login.main(args);
    };

    @FXML    
    public void create_accountButtonEvent(ActionEvent cabe) {
        String[] args = (null);          
        CreateAccount.main(args);
    };

    @FXML    
    public void playButtonActionEvent(ActionEvent pbae) {       
        Play.play_mc();
    }
}

Весь код здесь. Launcher (MCPY. java) основной gui и его соответствующий контроллер и таблица стилей (Main. java, MainController. java и Main.F XML)

РЕДАКТИРОВАТЬ: I применил изменения F XML, предложенные @naimdjon, однако при нажатии на кнопку появляется эта ошибка: https://pastebin.com/8EV9wfnm

1 Ответ

0 голосов
/ 22 марта 2020

В вашем xml есть несколько объявлений пространства имен, и импорт отсутствует. Измените свой f xml на этот (удаленное изображение для тестирования), это показывает окно:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.control.Button?>
<TitledPane fx:controller="com.mcpy.gui.MainController" animated="false"
            maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
            minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
            text="Minecraft Launcher Python Login Screen"
            xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml">
    <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
            <children>
                <ImageView fitHeight="135.0" fitWidth="119.0" layoutX="14.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
                    <image>
                    </image>
                </ImageView>
                <Button id="play" layoutX="231.0" layoutY="176.0" mnemonicParsing="false" text="Play" />
                <Button id="login" layoutX="313.0" layoutY="176.0" mnemonicParsing="false" text="Login" />
                <Button id="create_account" layoutX="77.0" layoutY="176.0" mnemonicParsing="false" text="Create Account" />
            </children></AnchorPane>
    </content>
</TitledPane>

ОБНОВЛЕНИЕ

После нажатия на кнопку, вы, кажется, вызывать метод launch(). Вы не можете вызывать метод launch () более одного раза в приложении Java FX.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...