首页 > 解决方案 > 为什么程序在 IDE 中运行和从命令提示符执行时看起来不同?

问题描述

//main.java

package sample;

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{
        MainController mc = new MainController();
        if (mc != null){
            System.out.println("It is not NULL");
        }else System.out.println("There is nothing");

        try {
            //BorderPane root = new BorderPane();
            Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root, 400, 400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


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

// MainController.java

package sample;

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

import java.util.Random;

public class MainController {

    @FXML                                                                  // members are accessible to markup connects scene builder with MainController
    private Label myMessage;

    public void generateRandom(ActionEvent event){
        Random rand = new Random();
        int myRand = rand.nextInt(50) + 1;                          // generate random number b/w 1 and 50
        myMessage.setText(Integer.toString(myRand));
        //System.out.println(Integer.toString(myRand));                      // convert integer to string

    }
}

// Main.fxml

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="352.0" prefWidth="443.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainController">
    <children>
        <Button fx:id="clickme" layoutX="228.0" layoutY="273.0" mnemonicParsing="false" onAction="#generateRandom" text="Click Here" />
        <Label fx:id="myMessage" layoutX="113.0" layoutY="130.0" prefHeight="106.0" prefWidth="186.0" />
    </children>
</AnchorPane>

// 应用程序.css

#clickme{
    -fx-font-size: 30px;
    -fx-background-color: rgba (255, 255, 255, .80);
    -fx-text-fill: blue;
    -fx-padding: 6 6 6 6;
    -fx-border-radius: 8;
    -fx-font-weight: bold;
}

问题是当我在 IntelliJ 中运行此代码时,这是 IDE 的输出输出

以及当我从命令提示符运行它时的 输出 从命令提示符输出

我不确定为什么会发生这种情况,我想知道如何修复它,以便当我从命令提示符运行代码时,我会得到与从 IDE 执行相同的输出

标签: javafx

解决方案


推荐阅读