我正在开发基本的javafx GUI,我需要创建更改背景颜色的按钮。我希望“Nightmode”按钮改变背景颜色,但它会导致“java.lang.NullPointerException”我知道,这个错误意味着什么,但我不知道为什么以及它来自何处。有什么建议吗?
这是我的代码:
Controler.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.AnchorPane;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private TextField primesAmount;
@FXML
private TextField noArgument;
@FXML
private TextArea showPrime;
@FXML
private Primes primes = new Primes();
@FXML
private AnchorPane background;
@FXML
private void handleAction(ActionEvent event) {
try {
primes.createTabel(Integer.parseInt(primesAmount.getText()));
} catch (NumberFormatException ex) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText("NOT AN INTEGEER");
alert.setContentText("Try again!");
alert.showAndWait();
} catch (IllegalArgumentException ex) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText(" illegal or inappropriate argument!");
alert.setContentText("Try again!");
alert.showAndWait();
} catch (NegativeArraySizeException ex) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText(" Out of range");
alert.setContentText("Try again!");
alert.showAndWait();
}
}
@FXML
private void handleAction2(ActionEvent event) {
try {
showPrime.setText(Integer.toString(primes.number(Integer.parseInt(noArgument.getText()))));
} catch (NumberFormatException ex) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText("NOT AN INTEGEER");
alert.setContentText("Try again!");
alert.showAndWait();
} catch (IllegalArgumentException ex) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText(" illegal or inappropriate argument!");
alert.setContentText("Try again!");
alert.showAndWait();
} catch (NegativeArraySizeException ex) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText(" Out of range");
alert.setContentText("Try again!");
alert.showAndWait();
}
catch (ArrayIndexOutOfBoundsException ex) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText(" Out of range");
alert.setContentText("Try again!");
alert.showAndWait();
}
}
@FXML
private void Exit(ActionEvent event) {
System.exit(1);
}
@FXML
private void Nightmode(ActionEvent event) {
background.setStyle("-fx-background-color: grey");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
主。的java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.swing.text.html.StyleSheet;
public class Main extends Application{
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Prime counter");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) { launch(args);}
}
CSS:
#button1 {
-fx-background-color: gold;
}
#button2 {
-fx-background-color: #4CAF50; /* Green */
-fx-border: none;
-fx-color: white;
-fx-text-align: center;
-fx-text-decoration: none;
-fx-display: inline-block;
-fx-font-size: 16px;
}
#background {
-fx-background-color: cornflowerblue;
}
sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="background" maxHeight="-Infinity" maxWidth="-Infinity"minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TextField fx:id="primesAmount" alignment="CENTER" layoutX="35.0" layoutY="90.0" text="Enter no. primes here" />
<Button id="button1" layoutX="215.0" layoutY="90.0" mnemonicParsing="false" onAction="#handleAction" stylesheets="@StyleSheet.css" text="O.K.!" />
<TextField fx:id="noArgument" layoutX="35.0" layoutY="134.0" />
<Button id="button2" fx:id="confirm" layoutX="215.0" layoutY="134.0" mnemonicParsing="false" onAction="#handleAction2" stylesheets="@StyleSheet.css" text="Confirm" />
<TextArea fx:id="showPrime" disable="true" editable="false" layoutX="225.0" layoutY="207.0" opacity="0.29" prefHeight="130.0" prefWidth="151.0">
<font>
<Font size="50.0" />
</font>
</TextArea>
<Button layoutX="525.0" layoutY="337.0" mnemonicParsing="false" onAction="#Exit" text="Exit" />
<Button layoutX="500.0" layoutY="38.0" mnemonicParsing="false" onAction="#Nightmode" text="Night mode" />
</children>
</AnchorPane>