【问题标题】:How to close dialog window on the button click [duplicate]如何关闭按钮上的对话框窗口单击[重复]
【发布时间】:2013-07-08 11:51:23
【问题描述】:

我使用awtswing 创建了对话框。在这种形式中,我喜欢JTextFieldokButton,即JButton。如何在单击okButton 时隐藏对话框窗口? 这是我班级的代码:

public class QueueDialog extends JFrame implements ActionListener {
  private static final long SerialVersionUID = 1L;
  private static JTextField field = new JTextField(15);
  private Sender sender;
  private String incommingMessagesFolderUrl = "/etc/dlp/templates";

  public QueueDialog() throws Exception {

    sender = new Sender();

    // field.setSize(60, 15);
    JButton okButton = new JButton("ok");
    final JLabel label = new JLabel("Enter the name of queue:");
    GridBagLayout gbag = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setLayout(gbag);

    gbc.insets = new Insets(2, 0, 2, 0);
    gbc.gridy = 0;
    gbc.gridx = 0;
    gbag.setConstraints(label, gbc);
    gbc.gridy = 1;
    gbc.gridx = 0;
    gbag.setConstraints(field, gbc);
    gbc.gridy = 2;
    gbc.gridx = 0;
    gbag.setConstraints(okButton, gbc);

    add(okButton);
    add(field);
    add(label);
    setTitle("Queue name");
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    setSize(400, 200);
    setLocationRelativeTo(null);
    setVisible(true);

    okButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("ok")) {
          // label.setText(field.getText());
          send(field.getText());
        }
      }
    });
  }
}

【问题讨论】:

  • 在行动监听器中写 this.setVisible(false) :P
  • 无论如何,从构造函数内部调用被覆盖的方法从来都不是一个好主意,所以请为相同的方法创建一个不同的方法
  • 1) 不要扩展框架或其他顶级容器。而是创建并使用一个实例。 2)不要设置顶级容器的大小。而是布局内容并致电pack()。 3)那不是一个对话框,而是一个框架。如果您想要一个对话框,请使用 JDialog!
  • 添加@trashgod(或任何人 - @ 很重要)以通知他们有新评论。既然你似乎无视我的建议,祝你好运..

标签: java swing button dialog awt


【解决方案1】:

正如我在评论中提到的(即第一条评论)setVisible(false) 在点击 Ok button

时工作得非常好

请再次检查此代码我在动作监听器中添加语句的位置。刚刚这样做是为了证明解决方案是正确的>我没有关注这篇文章,希望你一定在评论中得到了答案

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class QueueDialog extends JFrame implements ActionListener {
  private static final long SerialVersionUID = 1L;
  private static JTextField field = new JTextField(15);
  //private Sender sender;
  private String incommingMessagesFolderUrl = "/etc/dlp/templates";

  public QueueDialog() throws Exception {

   // sender = new Sender();

    // field.setSize(60, 15);
    JButton okButton = new JButton("ok");
    final JLabel label = new JLabel("Enter the name of queue:");
    GridBagLayout gbag = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setLayout(gbag);

   // gbc.insets = new Insets(2, 0, 2, 0);
    gbc.gridy = 0;
    gbc.gridx = 0;
    gbag.setConstraints(label, gbc);
    gbc.gridy = 1;
    gbc.gridx = 0;
    gbag.setConstraints(field, gbc);
    gbc.gridy = 2;
    gbc.gridx = 0;
    gbag.setConstraints(okButton, gbc);

    add(okButton);
    add(field);
    add(label);
    setTitle("Queue name");
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    setSize(400, 200);
    setLocationRelativeTo(null);
    setVisible(true);

    okButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("ok")) {
            System.out.println("Hello");
            setVisible(false);


          // label.setText(field.getText());
         // send(field.getText());
        }
      }
    });
  }

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}



public static void main(String[] args) throws Exception {
    QueueDialog diag = new QueueDialog();
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 2016-12-06
    • 2013-04-06
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多