摆动框架上的 javafx 警报对话框
javafx alert dialog on swing frame
我正在开发一个基于 swing 和 javafx 8 的应用程序。在此创建一个 swing 框架和 jbutton 使用,jbutton 动作代码在 javafx 8 中完成意味着使用场景。在此警报对话框中 create.but 问题是,如果我单击警报对话框以外的任何地方,然后隐藏在 swing 后面的警报对话框 frame.i 希望 javafx 警报对话框保持在我 create.if 我单击确定的 swing 框架上警报然后动作是转到秋千架。
请建议......
这是我的代码:
final JFXPanel fxPanel = new JFXPanel();
Platform.runLater(new Runnable() {
public void run() {
initFX(fxPanel);
}
private void initFX(JFXPanel fxPanel) {
// TODO Auto-generated method stub
Scene scene = createScene();
fxPanel.setScene(scene);
}
private Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root);
Alert cn=new Alert(AlertType.CONFIRMATION);
cn.initStyle(StageStyle.UNDECORATED);
cn.setTitle(null);
cn.setHeaderText(null);
cn.setContentText(ProjectProps.rb.getString("msg.play.confirm"));
DialogPane dp=cn.getDialogPane();
dp.getStylesheets().add(getClass().getResource("login.css").toExternalForm());
dp.setStyle("-fx-border-color: black; -fx-border-width: 5px; ");
Optional<ButtonType> result=cn.showAndWait();
if(result.get()==ButtonType.OK){
System.out.println("ok button clicked:"+result.get());
k=0;
} else{
System.out.println("cancel clicked");
k=1;
}
return (scene);
}
});
我不确定你到底想做什么。您绝对需要在 swing 应用程序中使用 JavaFX 警报吗? JDialog 运行良好,Alert 和 Dialogs 在 JavaFX 上下文中也运行良好。
您是否尝试过使用 cn.initModality(Modality.APPLICATION_MODAL)
设置警报应用程序模式?
也许您可以在这个 post 中找到答案:How to make a JFrame Modal in Swing java
这是 post 中的建议代码:
final JDialog frame = new JDialog(parentFrame, frameTitle, true);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
我会创建一个新的 JDialog 并将您的主框架 (parentFrame) 设置为参数,并将您的 JFXPanel 添加到它的内容中。
frame.getContentPane.add(fxPanel)
您可以使用 fxPanel.setScene(yourScene)
将 FX 内容添加到您的 JDialog。
建议示例:
public class Main {
public static void main(String[] args) {
// create parent
JFrame parent = new JFrame("MainFrame");
parent.setSize(500, 500);
// center of screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
parent.setLocation(dim.width/2-parent.getSize().width/2, dim.height/2-parent.getSize().height/2);
parent.setVisible(true);
createDialog(parent);
}
private static void createDialog(JFrame parent) {
// create dialogs JFX content
BorderPane layout = new BorderPane();
layout.setTop(new Button("HELLO"));
layout.setBottom(new Button("IT'S ME"));
Scene scene = new Scene(layout);
JFXPanel dlgContent = new JFXPanel();
dlgContent.setScene(scene);
dlgContent.setPreferredSize(new Dimension(200, 200));
dlgContent.setVisible(true);
// create dialog and set content
JDialog dlg = new JDialog(parent, "Dialog", true);
dlg.setLocationRelativeTo(parent);
dlg.getContentPane().add(dlgContent);
dlg.pack();
dlg.setVisible(true);
}
}
希望能帮到你。
我正在开发一个基于 swing 和 javafx 8 的应用程序。在此创建一个 swing 框架和 jbutton 使用,jbutton 动作代码在 javafx 8 中完成意味着使用场景。在此警报对话框中 create.but 问题是,如果我单击警报对话框以外的任何地方,然后隐藏在 swing 后面的警报对话框 frame.i 希望 javafx 警报对话框保持在我 create.if 我单击确定的 swing 框架上警报然后动作是转到秋千架。 请建议......
这是我的代码:
final JFXPanel fxPanel = new JFXPanel();
Platform.runLater(new Runnable() {
public void run() {
initFX(fxPanel);
}
private void initFX(JFXPanel fxPanel) {
// TODO Auto-generated method stub
Scene scene = createScene();
fxPanel.setScene(scene);
}
private Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root);
Alert cn=new Alert(AlertType.CONFIRMATION);
cn.initStyle(StageStyle.UNDECORATED);
cn.setTitle(null);
cn.setHeaderText(null);
cn.setContentText(ProjectProps.rb.getString("msg.play.confirm"));
DialogPane dp=cn.getDialogPane();
dp.getStylesheets().add(getClass().getResource("login.css").toExternalForm());
dp.setStyle("-fx-border-color: black; -fx-border-width: 5px; ");
Optional<ButtonType> result=cn.showAndWait();
if(result.get()==ButtonType.OK){
System.out.println("ok button clicked:"+result.get());
k=0;
} else{
System.out.println("cancel clicked");
k=1;
}
return (scene);
}
});
我不确定你到底想做什么。您绝对需要在 swing 应用程序中使用 JavaFX 警报吗? JDialog 运行良好,Alert 和 Dialogs 在 JavaFX 上下文中也运行良好。
您是否尝试过使用 cn.initModality(Modality.APPLICATION_MODAL)
设置警报应用程序模式?
也许您可以在这个 post 中找到答案:How to make a JFrame Modal in Swing java
这是 post 中的建议代码:
final JDialog frame = new JDialog(parentFrame, frameTitle, true);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
我会创建一个新的 JDialog 并将您的主框架 (parentFrame) 设置为参数,并将您的 JFXPanel 添加到它的内容中。
frame.getContentPane.add(fxPanel)
您可以使用 fxPanel.setScene(yourScene)
将 FX 内容添加到您的 JDialog。
建议示例:
public class Main {
public static void main(String[] args) {
// create parent
JFrame parent = new JFrame("MainFrame");
parent.setSize(500, 500);
// center of screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
parent.setLocation(dim.width/2-parent.getSize().width/2, dim.height/2-parent.getSize().height/2);
parent.setVisible(true);
createDialog(parent);
}
private static void createDialog(JFrame parent) {
// create dialogs JFX content
BorderPane layout = new BorderPane();
layout.setTop(new Button("HELLO"));
layout.setBottom(new Button("IT'S ME"));
Scene scene = new Scene(layout);
JFXPanel dlgContent = new JFXPanel();
dlgContent.setScene(scene);
dlgContent.setPreferredSize(new Dimension(200, 200));
dlgContent.setVisible(true);
// create dialog and set content
JDialog dlg = new JDialog(parent, "Dialog", true);
dlg.setLocationRelativeTo(parent);
dlg.getContentPane().add(dlgContent);
dlg.pack();
dlg.setVisible(true);
}
}
希望能帮到你。