2012-07-31 86 views
0

我正忙於將一個基本的Java文字處理器作爲個人項目,並且它包含一個彈出的JDialog。但是,當用戶單擊「取消」按鈕時,JDialog拒絕關閉,關閉它的唯一方法是使用框架上的關閉按鈕。同樣,當「確認」按鈕被激活時,任務完成,但JDialog保持打開狀態。誰能幫忙? 我的JDialog初始化代碼:JDialog拒絕關閉

package cword; 

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

    public class AlreadyExists extends JDialog 
    { 
     public static final long serialVersionUID = 1L; 
     public AlreadyExists(Frame owner, String pathname, String filename, boolean includedExtension) 
     { 
      super(owner); 
      initComponents(pathname, filename, includedExtension); 
     } 

     private void initComponents(final String pathname, String filename, final boolean includedExtension) 
     { 
      dialogPane = new JPanel(); 
      contentPanel = new JPanel(); 
      label1 = new JLabel(); 
      buttonBar = new JPanel(); 
      okButton = new JButton(); 
      cancelButton = new JButton(); 

      setTitle("Confirm Overwrite"); 
      Container contentPane = getContentPane(); 
      contentPane.setLayout(new BorderLayout()); 

      { 

       dialogPane.setLayout(new BorderLayout()); 

       { 
        contentPanel.setLayout(null); 

        label1.setText("File " + filename + " already exists. Are you sure you want to overwrite?"); 
        contentPanel.add(label1); 
        label1.setBounds(new Rectangle(new Point(0, 5), label1.getPreferredSize())); 

        { 
         Dimension preferredSize = new Dimension(); 
         for(int i = 0; i < contentPanel.getComponentCount(); i++) { 
          Rectangle bounds = contentPanel.getComponent(i).getBounds(); 
          preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width); 
          preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height); 
         } 
         Insets insets = contentPanel.getInsets(); 
         preferredSize.width += insets.right; 
         preferredSize.height += insets.bottom; 
         contentPanel.setMinimumSize(preferredSize); 
         contentPanel.setPreferredSize(preferredSize); 
        } 
       } 
       dialogPane.add(contentPanel, BorderLayout.CENTER); 

       { 
        buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0)); 
        buttonBar.setLayout(new GridBagLayout()); 
        ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80}; 
        ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0}; 

        okButton.setText("Confirm"); 
        buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 
         GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
         new Insets(0, 0, 0, 5), 0, 0)); 

        cancelButton.setText("Cancel"); 
        buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, 
         GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
         new Insets(0, 0, 0, 0), 0, 0)); 
       } 
       dialogPane.add(buttonBar, BorderLayout.SOUTH); 
      } 
      contentPane.add(dialogPane, BorderLayout.CENTER); 
      pack(); 
      setLocationRelativeTo(getOwner()); 
      setDefaultCloseOperation(2); 
      setModalityType(Dialog.ModalityType.APPLICATION_MODAL); 
      setVisible(true); 
      okButton.addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent ae) 
       { 
        write(pathname, includedExtension); 
        close(); 
       } 
      }); 
      cancelButton.addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent ae) 
       { 
        close(); 
       } 
      }); 
     } 

     private void write(String pathname, boolean includedExtension) 
       { 
      if(includedExtension) 
      { 
       try 
       { 
        BufferedWriter writer; 
        writer = new BufferedWriter(new FileWriter(pathname)); 
        writer.write(CWord.textArea1.getText()); 
        writer.close(); 
       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
      else if(!includedExtension) 
      { 
       try 
       { 
        BufferedWriter writer; 
        writer = new BufferedWriter(new FileWriter(pathname + ".txt")); 
        writer.write(CWord.textArea1.getText()); 
        writer.close(); 
       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     } 
     private void close() 
     { 
      dispose(); 
      dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); 
     } 

     private JPanel dialogPane; 
     private JPanel contentPanel; 
     private JLabel label1; 
     private JPanel buttonBar; 
     private JButton okButton; 
     private JButton cancelButton; 
    } 

而且在CWord.class稱此行之一:

new AlreadyExists(this, file.getAbsolutePath(), file.getName(), true); 

回答

1

OK,這是一個艱難的一個,但我終於想通了!

這是你的問題。您在之後將ActionListener添加到按鈕,您可以撥打setVisible(true)。由於這是一個模態對話框,因此setVisible(true)是EDT上的阻塞呼叫。這意味着setVisible(true)之後的代碼在關閉對話框之後纔會執行,因此ActionListeners不會被添加到按鈕中,直到對話框關閉。

當它是模態對話框時,您應該始終在執行GUI初始化後執行setVisible()。因此,只需將setVisible(true)移至按鈕上的addActionListener,然後您就可以開始使用了。

0

其實完全沒有必要實現自己的對話。

計劃你的代碼解決此相反:

int choice = JOptionPane.showConfirmDialog(parentComponent, "Are you sure you want to overwrite?", "File Already Exists", JOptionPane.OK_CANCEL_OPTION); 

if(choice == JOptionPane.OK_OPTION){ 
    // okay 
} else { 
    // cancelled 
} 

在這裏閱讀更多:How to Make Dialog - Java Tutorial # Custom Button Text

+0

但是,如何檢查它是否已被確認?而且,是否可以更改按鈕上的文字? – condorcraft110 2012-07-31 08:38:33

+0

是的,給我一下,我會編輯。 – rtheunissen 2012-07-31 08:38:59

+0

謝謝。但是,你可以改變JButton的文本嗎? – condorcraft110 2012-07-31 08:39:59