gpt4 book ai didi

java - 使用 java swing 在彼此之上显示两个对话框

转载 作者:行者123 更新时间:2023-11-29 07:16:39 27 4
gpt4 key购买 nike

我有一种情况,我会显示一个对话框,用户必须在其中填写一些菜单,然后按“确定”。它工作正常,但现在我在这个对话框上有另一个按钮,如果用户想要添加一些特定的值,我希望弹出另一个对话框,用户在其中填写附加值,然后按下确定,这个对话框消失,用户回到主界面对话。

我已经试过了,但是每次我调用新对话框时,焦点都不会离开主对话框,我怎么能完成这样的任务。

有没有相关的例子或者做这些事情的正确方法是什么。

编辑:

public static class EdgeMenu extends JPopupMenu {
// private JFrame frame;
public MyMenu(final JFrame frame) {
super("My Menu");
// this.frame = frame;

this.addSeparator();
this.add(new EdgePropItem(frame));
}
}



//this shows the first dialog, another class because i have some other
//functions to be performed here

public static class EdgePropItem extends JMenuItem{
//...

public EdgePropItem(final JFrame frame) {
super("Edit Properties");
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EdgePropertyDialog dialog = new EdgePropertyDialog(frame, edge);
dialog.setVisible(true);
}

});
}

}

现在在其他对话框中,在按钮甚至监听器中我正在尝试调用另一个对话框:

 private void newDialogHandler(java.awt.event.ActionEvent evt) {
MyNewDialog rdialog = new MyNewDialog(edge);
rdialog.setVisible(true);
}

它看起来很好,但是之前的对话框没有离开焦点,只有当我在该对话框上按完成/完成时它才会消失,我想要的是新对话框进入焦点,同时在此处按确定, 焦点应该回到原来的主对话框,但它不起作用?

最佳答案

也许这段代码可以证明你的问题,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SuperConstructor extends JFrame {

private static final long serialVersionUID = 1L;

public SuperConstructor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setTitle("Super constructor");
Container cp = getContentPane();
JButton b = new JButton("Show dialog");
b.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
}
});
cp.add(b, BorderLayout.SOUTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
add(bClose, BorderLayout.NORTH);
pack();
setVisible(true);
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
SuperConstructor superConstructor = new SuperConstructor();
}
});
}

private class FirstDialog extends JDialog {

private static final long serialVersionUID = 1L;

FirstDialog(final Frame parent) {
super(parent, "FirstDialog");
setPreferredSize(new Dimension(200, 200));
setLocationRelativeTo(parent);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
JButton bNext = new JButton("Show next dialog");
bNext.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
SecondDialog secondDialog = new SecondDialog(parent, false);
}
});
add(bNext, BorderLayout.NORTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
private int i;

private class SecondDialog extends JDialog {

private static final long serialVersionUID = 1L;

SecondDialog(final Frame parent, boolean modal) {
//super(parent); // < --- Makes this dialog
//unfocusable as long as FirstDialog is visible
setPreferredSize(new Dimension(200, 200));
setLocation(300, 50);
setModal(modal);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle("SecondDialog " + (i++));
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
}

关于java - 使用 java swing 在彼此之上显示两个对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8976874/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com