用于关闭 JDialog 的按钮
我想在 JDialog 的底部添加一个按钮(JButton),按下该按钮应关闭 JDialog。问题是我不知道在该按钮的 ActionListener 中写什么。我不需要退出程序的按钮,只需关闭对话框即可。
JDialog 是通过显式调用 JDialog 的构造函数之一创建的,而不是通过调用 JOptionPane 中的方法之一创建的。
我非常惊讶我无法使用谷歌找到这个问题的答案。我预计一个经常遇到的问题会在很多编程网站上得到广泛讨论。很奇怪,事实并非如此。
I want to add a button (JButton) at the bottom of a JDialog which should close the JDialog when pressed. The problem is I don't know what to write in the ActionListener of that button. I don't want the button to exit the program, just close the dialog.
The JDialog is created by explicitly calling one of JDialog's constructors, not by calling one of the methods from JOptionPane.
I was extremely surprised that I was unable to find an answer to this using Google. I expected that a problem that is so often encoutered would be widely covered on a lot of programming sites. Pretty weird that it is not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅使用
dispose()
方法关闭dialog,父框架未关闭。 JVM 未终止的原因。close only dialolg using
dispose()
method parent frame not closed. reason that JVM not terminated.您可以让
ActionListener
调度WindowEvent.WINDOW_CLOSING
,如下所示 此处。You can have the
ActionListener
dispatch aWindowEvent.WINDOW_CLOSING
, as shown here.在
ActionListener
的actionPerformed()
方法中,您需要类似以下内容:如果您想永久摆脱对话框(将其从内存中释放),那么您将也 call:
...其中dialog是对话框的名称。如果
dialog
是局部变量,则需要将其设为最终变量才能以这种方式访问它(或者只需确保从 Java 8 开始它是“有效最终的”)。如果您要添加按钮作为 JDialog 子类的一部分(即,如果您有
class MyDialog extends JDialog
并且您要在MyDialog
中添加操作侦听器)你会想要:In the
actionPerformed()
method ofActionListener
you'll want something like:If you want to get rid of the dialog permanently (free it from memory) then you would also call:
...where dialog is the name of your dialog. If
dialog
is a local variable, you'll need to make it final to access it in this way (or just make sure it's "effectively final" from Java 8 onwards).If you're adding the button as part of a subclass of JDialog (i.e. if you've got
class MyDialog extends JDialog
and you're adding the action listener inMyDialog
) you'll want:除了其他答案之外,您还可以将其设置为对话框根窗格的默认按钮:
这样,每当按下 Enter 时,都会调用它的
ActionListener
。In addition to other answers, you can set it as the default button for the dialog root pane:
That way, its
ActionListener
will be called whenever Enter is pressed.我的做法略有不同,但它达到了目的。我有几节课。它们扩展了 JPanel,服务于不同的目的并使用 JDialogs 来获取用户的输入。
典型的类将包含必要的字段、保存和取消按钮。
public class EnterpriseGUI extends javax.swing.JPanel {...} 看起来像这样:
EnterpriseGUI 类有一个私有变量 jdialog 和一个设置 jdialog 的方法。还有“保存”按钮和“取消”按钮的 ActionEvent。
最后,我根据需要从任何其他类创建 EnterpriseGUI 的实例,并将其嵌入到 JDialog 中。
I have done this slightly differently and it serves the purpose. I have several classes. They extend JPanel, serve different purposes and use JDialogs to get users' inputs.
A typical class would contain necessary fields, save and cancel buttons.
public class EnterpriseGUI extends javax.swing.JPanel {...} would look like this:
Class EnterpriseGUI has a private variable jdialog and a method to set jdialog. Also ActionEvent for 'save' button and 'cancel' button.
Finally, I create an instance of EnterpriseGUI from any other class as needed and embed it into JDialog.