javafx - 为什么程序在 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 执行相同的输出
解决方案
推荐阅读
- python - 来自导入脚本的 Python 方法无法识别
- r - 比较 nlme 和 lme4 之间随机截距和斜率无关的脚本
- java - 如何自动关注列表的当前元素(Android App)
- php - 我没有收到来自报价单的任何电子邮件
- react-native - 找不到模块“eslint-plugin-prettier”
- c# - 在文本块中使用资源变量
- react-native - 任务':react-native-multi-image-selector:compileReleaseJavaWithJavac'在本机反应中执行失败
- python - 如何将列表(不是 2D)顺时针旋转 90 度?
- c# - ASP.NET 核心 Cors 策略
- spring-boot - Spring Boot 应用程序在运行一段时间后关闭