0
我在新的JavaFX應用程序中使用JFoenix。我已經成功創建了我自己的消息對話框。從JFoenix Dialog獲取響應
package Dialog;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXDialogLayout;
import com.jfoenix.controls.events.JFXDialogEvent;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
public class OK_Message extends JFXDialog {
private StackPane Container;
private JFXDialogLayout Content;
String DialogText;
String Headline;
JFXButton btn;
boolean OK = false;
private BooleanProperty okval;
public OK_Message(
StackPane Container,
String Headline,
String DialogText
){
this.Container = Container;
this.DialogText = DialogText;
this.Headline = Headline;
}
public void ShowDialog(){
setDialogContainer(Container);
setContent(getDialogContent());
setTransitionType(JFXDialog.DialogTransition.TOP);
setOverlayClose(false);
setFocusTraversable(true);
setOnDialogOpened((JFXDialogEvent event) -> {
Platform.runLater(()->{
btn.requestFocus();
});});
setOnKeyPressed((KeyEvent e)->{
e.consume();
if(e.getCode()== KeyCode.ENTER){close();}
else if(e.getCode()== KeyCode.SPACE){btn.requestFocus();}
else{}
});
show();
}
private JFXDialogLayout getDialogContent(){
Content = new JFXDialogLayout();
Content.setHeading(HeadLine());
Content.setBody(Body());
// Platform.runLater(()->{b.requestFocus();});
Content.setActions(getButton());
return Content;
}
private Label HeadLine(){
Label l = new Label(Headline);
l.setFont(new Font(18));
return l;
}
private GridPane Body(){
Label l = new Label(DialogText);
l.setFont(new Font(14));
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(8);
GridPane.setConstraints(l, 0, 0, 1, 1, HPos.LEFT, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS, new Insets(0, 0, 0, 0));
grid.getChildren().addAll(l);
return grid;
}
private JFXButton getButton(){
btn = new JFXButton("OK");
btn.setButtonType(JFXButton.ButtonType.FLAT);
btn.setPrefWidth(50);
btn.setTextFill(Color.WHITE);
btn.setOnAction((ActionEvent event) -> {
CloseDialog();
});
btn.setStyle("-fx-background-color:#FFFFFF");
btn.focusedProperty().addListener(new ChangeListener<Boolean>(){
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if(newValue){
btn.setStyle("-fx-background-color:red");
}
}
});
return btn;
}
private void CloseDialog(){
close();
}
}
現在我可以打電話給我的任何地方對話:是這樣的:
Dialog.OK_Message message =
new Dialog.OK_Message((StackPane)app_setup.getParent(),
"Message",
"Changes will be affected after restart.");
message.ShowDialog();
我無法找到一個方法來檢測是否已單擊按鈕或沒有。或者用戶按下什麼鍵。如果用戶點擊了確定按鈕,我想做點什麼。或者如果使用按了一些鍵我想做其他事情?我怎樣才能聽到上面的對話框?