用于关闭 JDialog 的按钮

发布于 2024-11-28 09:48:30 字数 255 浏览 1 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小红帽 2024-12-05 09:48:30
import java.awt.event.*;
import javax.swing.*;

public class YourDialog extends JDialog implements ActionListener {

  JButton button;

  public YourDialog() {
     button = new JButton("Close");
     button.addActionListener(this);
     add(button);
     pack();
     setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
      dispose();
  }
}
  • 仅使用dispose()方法关闭dialog,父框架未关闭。 JVM 未终止的原因。

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

public class YourDialog extends JDialog implements ActionListener {

  JButton button;

  public YourDialog() {
     button = new JButton("Close");
     button.addActionListener(this);
     add(button);
     pack();
     setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
      dispose();
  }
}
  • close only dialolg using dispose() method parent frame not closed. reason that JVM not terminated.

忱杏 2024-12-05 09:48:30

您可以让 ActionListener 调度 WindowEvent.WINDOW_CLOSING,如下所示 此处

You can have the ActionListener dispatch a WindowEvent.WINDOW_CLOSING, as shown here.

梦言归人 2024-12-05 09:48:30

ActionListeneractionPerformed() 方法中,您需要类似以下内容:

dialog.setVisible(false);

如果您想永久摆脱对话框(将其从内存中释放),那么您将 call:

dialog.dispose(); 

...其中dialog是对话框的名称。如果 dialog 是局部变量,则需要将其设为最终变量才能以这种方式访问​​它(或者只需确保从 Java 8 开始它是“有效最终的”)。

如果您要添加按钮作为 JDialog 子类的一部分(即,如果您有 class MyDialog extends JDialog 并且您要在 MyDialog 中添加操作侦听器)你会想要:

MyDialog.this.setVisible(false);

In the actionPerformed() method of ActionListener you'll want something like:

dialog.setVisible(false);

If you want to get rid of the dialog permanently (free it from memory) then you would also call:

dialog.dispose(); 

...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 in MyDialog) you'll want:

MyDialog.this.setVisible(false);
百合的盛世恋 2024-12-05 09:48:30

除了其他答案之外,您还可以将其设置为对话框根窗格的默认按钮:

JButton myButton = new JButton("Close");
myButton.addActionListener(e -> {
        dispose(); // Or whatever else
        setVisible(false);
});
getRootPane().setDefaultButton(myButton);

这样,每当按下 Enter 时,都会调用它的 ActionListener

In addition to other answers, you can set it as the default button for the dialog root pane:

JButton myButton = new JButton("Close");
myButton.addActionListener(e -> {
        dispose(); // Or whatever else
        setVisible(false);
});
getRootPane().setDefaultButton(myButton);

That way, its ActionListener will be called whenever Enter is pressed.

橘和柠 2024-12-05 09:48:30

我的做法略有不同,但它达到了目的。我有几节课。它们扩展了 JPanel,服务于不同的目的并使用 JDialogs 来获取用户的输入。

典型的类将包含必要的字段、保存和取消按钮。

public class EnterpriseGUI extends javax.swing.JPanel {...} 看起来像这样:

设计器模式下的 JPanel 类

EnterpriseGUI 类有一个私有变量 jdialog 和一个设置 jdialog 的方法。还有“保存”按钮和“取消”按钮的 ActionEvent。

private JDialog jdialog;
public void setJDialog(JDialog jdialog) {
    this.jdialog = jdialog;
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // Do your stuff..
    jdialog.dispose();
} 
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // Discard your stuff..
    jdialog.dispose();
} 

最后,我根据需要从任何其他类创建 EnterpriseGUI 的实例,并将其嵌入到 JDialog 中。

Class OtherClass{
private JDialog dialog;
private EnterpriseGUI = new enterprisegui();
    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        this.dialog = new JDialog(this, "My dialog", true);
        this.dialog.setResizable(false);
        enterprisegui.setJDialog(dialog);
        this.dialog.getContentPane().add(enterprisegui);
        this.dialog.pack();

        Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();
        this.dialog.setLocation(new Double((Size.getWidth()/2) - 
                (dialog.getWidth()/2)).intValue(), new Double((Size.getHeight()/2) - 
                        (dialog.getHeight()/2)).intValue());
        this.dialog.setVisible(true);

    }  
}

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:

JPanel class in designer mode

Class EnterpriseGUI has a private variable jdialog and a method to set jdialog. Also ActionEvent for 'save' button and 'cancel' button.

private JDialog jdialog;
public void setJDialog(JDialog jdialog) {
    this.jdialog = jdialog;
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // Do your stuff..
    jdialog.dispose();
} 
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // Discard your stuff..
    jdialog.dispose();
} 

Finally, I create an instance of EnterpriseGUI from any other class as needed and embed it into JDialog.

Class OtherClass{
private JDialog dialog;
private EnterpriseGUI = new enterprisegui();
    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        this.dialog = new JDialog(this, "My dialog", true);
        this.dialog.setResizable(false);
        enterprisegui.setJDialog(dialog);
        this.dialog.getContentPane().add(enterprisegui);
        this.dialog.pack();

        Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();
        this.dialog.setLocation(new Double((Size.getWidth()/2) - 
                (dialog.getWidth()/2)).intValue(), new Double((Size.getHeight()/2) - 
                        (dialog.getHeight()/2)).intValue());
        this.dialog.setVisible(true);

    }  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文