我的基本加密器应用程序出了问题。如果有人在 keyTextField 中键入字符串,我想生成一个错误窗口。还有一个使用OK按钮关闭错误窗口的事件(窗口图形从fxml文件加载) 我尝试过如下所示,但是没有成功,我也使用了close()方法。处理应用程序控制的最佳方法是什么?我只使用MainController,我认为这不是一个好主意。提前谢谢
`package pl.gumisok.cipherController;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import pl.gumisok.cipherMain.CipherManager;
public class MainController implements Initializable {
CipherManager cipher;
@FXML
private ContentPaneController contentPaneController;
@FXML
private ControlPaneController controlPaneController;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
System.out.println(contentPaneController);
System.out.println(controlPaneController);
Button encryptButton = controlPaneController.getEncryptButton();
Button decryptButton = controlPaneController.getDecryptButton();
Button okButton = controlPaneController.getOkButton();
TextArea cleanTextArea = contentPaneController.getCleanTextArea();
TextArea cryptTextArea = contentPaneController.getCryptTextArea();
TextField keyTextField = controlPaneController.getKeyTextField();
encryptButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String wiadomosc = cleanTextArea.getText();
System.out.println(wiadomosc);
try {
int key = Integer.parseInt(keyTextField.getText());
System.out.println(key);
} catch (NumberFormatException e) {
System.out.println(e);
FXMLLoader fxmlLoader = new FXMLLoader(getClass()
.getClassLoader().getResource(
"pl/gumisok/cipherView/Error.fxml"));
Parent root;
try {
root = fxmlLoader.load();
Stage sstage = new Stage();
sstage.setOpacity(1);
sstage.setTitle("Error");
sstage.setScene(new Scene(root));
sstage.show();
okButton.setOnAction(x->sstage.hide());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
}`
答案 0 :(得分:0)
我希望,我理解这个问题是正确的, 这是一个如何创建警报对话框的示例
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("I have a great message for you!");
alert.showAndWait();
答案 1 :(得分:0)
你的申请层不好 您需要将fxml文件中的按钮操作绑定到控制器 也许是这样的:
Error.fxml:
<AnchorPane xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controllers.ErrorController">
<children>
<Label text="ERROR!" />
<Button text="close" onAction="#hide" layoutY="15"/>
</children>
</AnchorPane>
ErrorController.java:
public class ErrorController {
private static Stage stage;
private static Parent root;
public ErrorController(){}
public ErrorController(Window owner) throws IOException {
if (root == null)
root = FXMLLoader.load(ClassLoader
.getSystemResource("views/Error.fxml"));
if (stage == null)
stage = new Stage();
//stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(owner);
stage.setTitle("Error");
stage.setScene(new Scene(root));
}
public void show() {
stage.show();
}
public @FXML void hide() {
stage.hide();
}
}
然后使用它
...
error = new ErrorController(node.getScene().getWindow());
...
try {
int key = Integer.parseInt(keyTextField.getText());
System.out.println(key);
} catch (NumberFormatException e) {
error.show();
}