有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何关闭对话框窗口上的按钮点击

我已经使用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());
        }
      }
    });
  }
}

共 (1) 个答案

  1. # 1 楼答案

    正如我在评论中提到的(即第一条评论)setVisible(false)在点击确定按钮

    请再次检查我在action listener中添加语句的代码。刚才做了这件事以证明解决方案是正确的>;我没有关注这篇文章,希望你一定在评论中得到了答案

    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();
    }
    }