我正在创建一个简单的JavaFX程序,通过将参数从一个阶段传递到另一个阶段来显示警告框/窗口。下面给出了我的Main
类,Controller
类,AlertBox
类及其所有FXML
代码的代码。我只想在display
类中使用两个参数 - 标题和消息来调用AlertBox
函数(在Controller
中定义)。我使用Scene Builder来构建程序的UI,但是在AlertBox
类中,我无法在显示功能中使用标签和按钮。我收到了这个错误:
Non-static method 'mess' cannot be referenced from static context
有人请告诉我如何解决它,以便我可以在各阶段之间进行沟通。
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{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Button fx:id="button" layoutX="274.0" layoutY="188.0" mnemonicParsing="false" text="Go to Scene 2" />
<Label fx:id="label" layoutX="279.0" layoutY="236.0" style="-fx-background-color: #eee;" text="This is Scene 1!" />
<CheckBox layoutX="156.0" layoutY="86.0" mnemonicParsing="false" text="CheckBox" />
<Button fx:id="alert" layoutX="274.0" layoutY="295.0" mnemonicParsing="false" text="Open Alert" />
</children>
</AnchorPane>
Controller.java
package sample;
import sample.AlertBox;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.*;
import javafx.scene.*;
public class Controller implements Initializable{
@FXML
Label label;
@FXML Button alert;
@Override
public void initialize(URL url, ResourceBundle rb){
alert.setOnAction(even ->
{
AlertBox.display("Title", "This is the message");
}
);
}
}
AlertBox.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class AlertBox implements Initializable{
@FXML Button winClose;
@FXML Label mess;
@FXML AnchorPane lay;
@FXML
public static void display(String title, String message){
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
mess.setText(message);
winClose.setOnAction(e -> window.close());
window.setScene(new Scene(lay));
window.showAndWait();
}
@Override
public void initialize(URL location, ResourceBundle resources) {}
}
AlertBox.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="lay" minHeight="250.0" minWidth="250.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.AlertBox">
<children>
<Button fx:id="winClose" layoutX="69.0" layoutY="179.0" mnemonicParsing="false" text="Close the Window" />
<Label fx:id="mess" layoutX="109.0" layoutY="120.0" prefHeight="17.0" prefWidth="68.0" />
</children>
</AnchorPane>