javafx fxml程序错误(无主要方法) [英] javafx fxml program error(no main method)

查看:114
本文介绍了javafx fxml程序错误(无主要方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对javafx和fxml相当陌生.我想自学!但是,当我为简单的登录GUI创建程序时,当我最终尝试运行程序时遇到了一个问题.它告诉我班上没有主要方法,而且我不确定如何解决.有什么想法吗?

i'm fairly new to javafx and fxml. I'm trying to teach myself! However, when I was creating a program for a simple login GUI I came into an issue when I finally tried to run my program. It told me there was no main method in my class and I'm unsure of how to fix it. Any ideas?

我的程序创建一个登录屏幕,当您输入用户名和密码的测试"时,它将带您进入另一个场景.

My program creates a login screen and when you enter "test" for the username and password it'll take you to another scene.

这是我的Login.java

package com;

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

    /**
     *
     * @author Tyler
     */
    public class Login extends Application{

        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Login");
            stage.show();
        }

    }

这是我的LoginController.java

package com;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author Tyler
 */
public class LoginController implements Initializable {

    @FXML
    private Label lblMessage; 
    @FXML
    private TextField txtUsername;
    @FXML
    private PasswordField txtPassword;
    @FXML
    private void btnLoginAction(ActionEvent event) throws Exception{
        if(txtUsername.getText().equals("test") && txtPassword.getText().equals("test")){
            ((Node) (event.getSource())).getScene().getWindow().hide();
            Parent parent = FXMLLoader.load(getClass().getResource("DateSelection.fxml"));
            Stage stage  = new Stage();
            Scene scene = new Scene(parent);
            stage.setScene(scene);
            stage.setTitle("Date Selection");
            stage.show();
        }else{
            lblMessage.setText("Username or Password is invalid!");
        }
    }
    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

这是我的Login.fxml

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" fx:id="lblMessage" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.LoginController">
   <children>
      <PasswordField fx:id="txtPassword" layoutX="200.0" layoutY="200.0" prefHeight="30.0" prefWidth="200.0" promptText="Password" />
      <TextField fx:id="txtUsername" layoutX="200.0" layoutY="140.0" prefHeight="30.0" prefWidth="200.0" promptText="Username" />
      <Button fx:id="btnLogin" layoutX="269.0" layoutY="251.0" mnemonicParsing="false" onAction="#btnLoginAction" prefHeight="30.0" text="Login">
         <font>
            <Font size="14.0" />
         </font></Button>
      <Label fx:id="lblMessage" layoutX="283.0" layoutY="71.0" text="Label" />
   </children>
</AnchorPane>

Here is my DateSelectionController.java

Here is my DateSelectionController.java

package com;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

/**
 * FXML Controller class
 *
 * @author Tyler
 */
public class DateSelectionController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

这是我的DateSelection.fxml

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="com.DateSelectionController">
   <children>
      <Label layoutX="191.0" layoutY="164.0" text="Welcome">
         <font>
            <Font name="System Bold" size="50.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

推荐答案

来自Oracle :

当使用JavaFX Packager工具创建应用程序的JAR文件时,对于JavaFX应用程序不需要main()方法,该工具将JavaFX Launcher嵌入JAR文件中.但是,包含main()方法很有用,这样您就可以运行在没有JavaFX Launcher的情况下创建的JAR文件,例如在使用未完全集成JavaFX工具的IDE时.另外,嵌入JavaFX代码的Swing应用程序需要main()方法.

The main() method is not required for JavaFX applications when the JAR file for the application is created with the JavaFX Packager tool, which embeds the JavaFX Launcher in the JAR file. However, it is useful to include the main() method so you can run JAR files that were created without the JavaFX Launcher, such as when using an IDE in which the JavaFX tools are not fully integrated. Also, Swing applications that embed JavaFX code require the main() method.

因此,一种解决方案是确保以完全支持JavaFX工具的方式来构建它.另一种解决方案是添加main方法以启动应用程序.这样可以避免任何潜在的问题,并且在不需要的情况下也不会造成任何问题.

So one solution is to make sure it's being built in a way that fully supports the JavaFX tools. The other solution is to add a main method to starts the application. That would avoid any potential problems like this, and doesn't cause any problems in the case where it's not needed.

您的主要方法应如下所示:

Your main method should look like this:

public static void main(String[] args){
    Application.launch(Login.class, args);
}

这将简单地将控制权传递给JavaFX以像通常那样处理.

That will simply pass control on to JavaFX to handle like it would normally.

这篇关于javafx fxml程序错误(无主要方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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