2013-07-08 172 views
1

我使用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()); 
     } 
     } 
    }); 
    } 
} 
+2

在動作監聽器寫this.setVisible(假):P –

+2

反正它是從來沒有好主意從構造函數中調用重寫方法,所以請爲同一個方法創建不同的方法 –

+3

1)不要擴展框架或其他頂層容器。而是創建並使用一個實例。 2)不要設置頂級容器的大小。而是佈置內容並調用'pack()'。 3)這不是一個對話框,而是一個框架。如果你想要一個對話框,使用'JDialog'! –

回答

1

正如我在我的評論中提到(即第一個評論)setVisible(false)正着手確定按鈕,點擊精絕

請檢查驗證碼再次在行動監聽器中添加語句的位置。剛纔這樣做是爲了證明該解決方案是正確的>我是不是下面這篇文章,希望你一定有你的答案在註釋本身

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(); 
} 
} 
相關問題