[英]JavaFX, closing error window using Button
I have a problem with my basic encrypter application. 我的基本加密器应用程序有问题。 I want to generate an error window if someone type a string in the keyTextField .
如果有人在keyTextField中键入字符串,我想生成一个错误窗口。 And also an event to close the error window using OK Button(Window graphic is loading from fxml file) I've tried making it as shown below, but without success, i was also using close() method.
还有一个事件,使用“确定”按钮关闭错误窗口(正在从fxml文件加载窗口图形),我尝试如下所示进行操作,但是没有成功,我也使用了close()方法。 What are best methods to deal with application control?
什么是处理应用程序控制的最佳方法? I am using only MainController and i think it is not good idea.
我只使用MainController,我认为这不是一个好主意。 Thank you in advance
先感谢您
`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();
}
}
}
});
}
}` }`
I hope, I understand the question correct, here is an example how to create an alert dialog 希望我理解正确的问题,这是一个如何创建警报对话框的示例
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("I have a great message for you!");
alert.showAndWait();
your application layer is not good. 您的应用程序层不好。
you need bind the button action in fxml file to a controller. 您需要将fxml文件中的button操作绑定到控制器。
Maybe something like this: 也许是这样的:
Error.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:
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();
}
}
And then use it 然后用
...
error = new ErrorController(node.getScene().getWindow());
...
try {
int key = Integer.parseInt(keyTextField.getText());
System.out.println(key);
} catch (NumberFormatException e) {
error.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.