您应该传递父组件作为第一个参数,而不是您创建的一些随机新框架。
假如你是从一个JFrame方法内推出此,您可以运行
String menuName = (String) JOptionPane.showInputDialog (
this, // parent component is the JFrame you are launching this from
"Insert new Menu Name"
);
在某些情况下,例如从onLostFocus
实施FocusListener
运行这个时候你需要运行这个毕竟时事有被处理。这可以通过在事件队列结尾处发布事件来完成。使用SwingUtilities.invokeLater
来做到这一点。
因此,如果您处于这种情况,请按如下所示启动对话框。假设你正在运行此框架类被称为MyFrame
:
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
String menuName = (String) JOptionPane.showInputDialog (
MyFrame.this, // parent component is the JFrame you are launching this from
"Insert new Menu Name"
);
// do stuff with menuName
}
});
等一下...我没有在第一地点,但在修订似乎您使用了前面的问题,完全重新编辑它问一个新的问题......这不是如何做的。对于一个新问题,你应该问一个新问题。不要编辑一个完全不同的!请不要再这样做。 –