我无法从正确导出中导出此javafx程序。
曾多次尝试使用JAR和Javafx应用程序,但始终失败。我得到的最远的结果是导出为JAR,并单独添加了javafx库,当我运行jar时,它给出一个错误并要求检查控制台以获取进一步的消息。
vm
--module-path /Users/(my name)/javafx-sdk-12.0.1/lib --add-modules javafx.controls,javafx.fxml
下面的所有三个文件都位于一个名为package的程序包中:Math (很抱歉,复制了整个文件代码)
Main.java
package Math;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.*;
public class Main extends Application {
/**
* initialises program
* PrintStream section changes system output to file on boot
* setOnCloseRequest runs confirmation of closing application
*/
@Override
public void init() {
final Logger LOGGER = Logger.getLogger(Main.class.getName());
Handler consoleHandler;
Handler fileHandler;
try {
//Creating consoleHandler and fileHandler
consoleHandler = new ConsoleHandler();
fileHandler = new FileHandler("./Log.log");
//Assigning handlers to LOGGER object
LOGGER.addHandler(consoleHandler);
LOGGER.addHandler(fileHandler);
//Setting levels to handlers and LOGGER
consoleHandler.setLevel(Level.ALL);
fileHandler.setLevel(Level.ALL);
LOGGER.setLevel(Level.ALL);
LOGGER.config("Configuration done.");
//Console handler removed
LOGGER.removeHandler(consoleHandler);
} catch (IOException exception) {
LOGGER.log(Level.SEVERE, "Error occur in FileHandler.", exception);
}
}
@Override
public void start(Stage primaryStage) throws Exception {
final Logger LOGGER = Logger.getLogger(Main.class.getName());
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Adding Two Numbers");
primaryStage.setScene(new Scene(root, 860, 600));
primaryStage.show();
primaryStage.setOnCloseRequest(event -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Quitting Application");
alert.setHeaderText("Are you sure you want to close the application?");
alert.setContentText(null);
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent()) {
if (result.get() == ButtonType.OK) {
LOGGER.log(Level.CONFIG, "User closed program");
Platform.exit();
} else {
event.consume();
}
}
});
}
}
Controller.java
package Math;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* fxml made using Scene Builder 2
* Controller class used to initialise floats, then update with field when submit pressed
* output also created after adding two numbers together
*/
public class Controller {
private int stage = 0;
private int tempSign = 0;
private int sign = 0;
private float firstNumber = 0;
private float secondNumber = 0;
private float mathResult = 0;
private int numericTestResult = 0; //1 is number 0 is not number
@FXML private TextArea mathOutput;
@FXML private TextArea userInput;
@FXML private Button plus;
@FXML private Button minus;
@FXML private Button multiply;
@FXML private Button divide;
private final Logger LOGGER = Logger.getLogger(Main.class.getName());
@FXML protected void plusButton() {
tempSign = 1;
plusColor();
signPressed();
}
@FXML protected void minusButton() {
tempSign = 2;
minusColor();
signPressed();
}
@FXML protected void multiplyButton() {
tempSign = 3;
multiplyColor();
signPressed();
}
@FXML protected void divideButton() {
tempSign = 4;
divideColor();
signPressed();
}
private void signPressed() {
if (stage == 0) {
if (userInput.getText().equals("")) {
if (mathOutput.getText().equals("")) {
LOGGER.log(Level.WARNING, "signPressed, s=0, no uI mO");
inputError();
resetSignColor();
}
else {
firstNumber = Float.parseFloat(mathOutput.getText());
sign = tempSign;
stage = 1;
}
}
else {
numericTest();
if (numericTestResult == 0) {
inputError();
resetSignColor();
}
else if (numericTestResult == 1) {
firstNumber = Float.parseFloat(userInput.getText());
sign = tempSign;
stage = 1;
userInput.clear();
}
}
}
else if (stage == 1) {
if (userInput.getText().equals("")) {
sign = tempSign;
}
else {
numericTest();
if (numericTestResult == 0) {
inputError();
resetSignColor();
}
else if (numericTestResult == 1){
secondNumber = Float.parseFloat(userInput.getText());
math();
displayAnswer();
sign = tempSign;
firstNumber = mathResult;
userInput.clear();
}
}
}
}
@FXML protected void equalsButton() {
if (stage == 0) {
if (!userInput.getText().equals("")) {
numericTest();
if (numericTestResult == 0) {
inputError();
}
else if (numericTestResult == 1) {
mathResult = Float.parseFloat(userInput.getText());
displayAnswer();
userInput.clear();
}
}
}
else if (stage == 1) {
if (userInput.getText().equals("")) {
mathResult = firstNumber;
displayAnswer();
stage = 0;
resetSignColor();
}
else {
numericTest();
if (numericTestResult == 0) {
inputError();
}
else if (numericTestResult == 1) {
secondNumber = Float.parseFloat(userInput.getText());
math();
displayAnswer();
stage = 0;
resetSignColor();
userInput.clear();
}
}
}
}
private void math() {
if (sign == 1) mathResult = firstNumber + secondNumber;
else if (sign == 2) mathResult = firstNumber - secondNumber;
else if (sign == 3) mathResult = firstNumber * secondNumber;
else if (sign == 4) mathResult = firstNumber / secondNumber;
else LOGGER.log(Level.SEVERE, "sign not 1~4 at math()");
}
private void numericTest() {
try {
Float.parseFloat(userInput.getText());
numericTestResult = 1;
}
catch (NumberFormatException inputNotNum) {
numericTestResult = 0;
LOGGER.log(Level.WARNING, "input not numeral");
}
}
private void displayAnswer() {
if (mathResult == (int)mathResult) {
mathOutput.setText(String.valueOf(Math.round(mathResult)));
}
else mathOutput.setText(String.valueOf(mathResult));
}
@FXML protected void clear() {
userInput.clear();
}
@FXML protected void allClear() {
userInput.clear();
mathOutput.clear();
stage = 0;
resetSignColor();
sign = 0;
firstNumber = 0;
secondNumber = 0;
}
private void plusColor() {
plus.setStyle("-fx-background-color: #adadad");
minus.setStyle("-fx-background-color: #eaeaea");
multiply.setStyle("-fx-background-color: #eaeaea");
divide.setStyle("-fx-background-color: #eaeaea");
}
private void minusColor() {
plus.setStyle("-fx-background-color: #eaeaea");
minus.setStyle("-fx-background-color: #adadad");
multiply.setStyle("-fx-background-color: #eaeaea");
divide.setStyle("-fx-background-color: #eaeaea");
}
private void multiplyColor() {
plus.setStyle("-fx-background-color: #eaeaea");
minus.setStyle("-fx-background-color: #eaeaea");
multiply.setStyle("-fx-background-color: #adadad");
divide.setStyle("-fx-background-color: #eaeaea");
}
private void divideColor() {
plus.setStyle("-fx-background-color: #eaeaea");
minus.setStyle("-fx-background-color: #eaeaea");
multiply.setStyle("-fx-background-color: #eaeaea");
divide.setStyle("-fx-background-color: #adadad");
}
private void resetSignColor() {
plus.setStyle("-fx-background-color: #eaeaea");
minus.setStyle("-fx-background-color: #eaeaea");
multiply.setStyle("-fx-background-color: #eaeaea");
divide.setStyle("-fx-background-color: #eaeaea");
}
private void inputError() {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Input Error");
alert.setHeaderText("Not a number");
alert.setContentText("Please put in a number");
alert.showAndWait();
LOGGER.log(Level.WARNING, "inputError");
}
}
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0"
prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="Math.Controller">
<TextArea id="inputOne" fx:id="userInput" layoutX="11.0" layoutY="10.0" prefHeight="117.0" prefWidth="535.0"
promptText="Number">
<font>
<Font name="Comic Sans MS" size="22.0"/>
</font>
</TextArea>
<Button fx:id="plus" layoutX="618.0" layoutY="82.0" mnemonicParsing="false" onAction="#plusButton" prefHeight="33.0"
prefWidth="51.0" text="+" textFill="#ff00bf">
<font>
<Font name="Comic Sans MS" size="28.0"/>
</font>
</Button>
<Button fx:id="minus" layoutX="669.0" layoutY="82.0" mnemonicParsing="false" onAction="#minusButton"
prefHeight="44.0" prefWidth="51.0" text="-" textFill="#ff00bf">
<font>
<Font name="Comic Sans MS" size="28.0"/>
</font>
</Button>
<Button fx:id="multiply" layoutX="618.0" layoutY="140.0" mnemonicParsing="false" onAction="#multiplyButton"
prefHeight="26.0" prefWidth="51.0" text="×" textFill="#ff00bf">
<font>
<Font name="Comic Sans MS" size="28.0"/>
</font>
</Button>
<Button fx:id="divide" layoutX="669.0" layoutY="140.0" mnemonicParsing="false" onAction="#divideButton"
prefHeight="59.0" prefWidth="51.0" text="÷" textFill="#ff00bf">
<font>
<Font name="Comic Sans MS" size="28.0"/>
</font>
</Button>
<Button layoutX="618.0" layoutY="199.0" mnemonicParsing="false" onAction="#equalsButton" prefHeight="48.0"
prefWidth="102.0" text="=" textFill="#ff00d0">
<font>
<Font name="Comic Sans MS" size="22.0"/>
</font>
</Button>
<TextArea fx:id="mathOutput" editable="false" layoutX="11.0" layoutY="195.0" prefHeight="128.0" prefWidth="534.0"
promptText="Result">
<font>
<Font name="Comic Sans MS" size="22.0"/>
</font>
</TextArea>
<Button layoutX="618.0" layoutY="24.0" mnemonicParsing="false" onAction="#allClear" prefHeight="59.0"
prefWidth="51.0" text="AC" textFill="#ff00bf">
<font>
<Font name="Comic Sans MS" size="17.0"/>
</font>
</Button>
<Button layoutX="668.0" layoutY="24.0" mnemonicParsing="false" onAction="#clear" prefHeight="59.0" prefWidth="52.0"
text="C" textFill="#ff00bf">
<font>
<Font name="Comic Sans MS" size="20.0"/>
</font>
</Button>
</Pane>
给出错误“无法启动,请检查控制台以获取可能的错误消息” 在MacOS 10.14.5 Beta(18F131a)上 IntelliJ 2019.1旗舰版