2012-04-30 41 views
1

我从JFrame调用JDialog 但问题是当我退出JDialog框时,整个程序退出意味着JFrame窗口也退出。 我不仅可以退出JDialog框窗口。 我如何关闭JDialog盒子?我如何才能关闭JDialog盒子?

在这里输入的代码

package subtitlemakerofvedio; 

public class About extends javax.swing.JDialog { 

/** Creates new form About */ 
public About(java.awt.Frame parent, boolean modal) { 
    super(parent, modal); 
    initComponents(); 
} 

/** This method is called from within the constructor to 
* initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is 
* always regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    jLabel1 = new javax.swing.JLabel(); 
    jLabel2 = new javax.swing.JLabel(); 
    jLabel3 = new javax.swing.JLabel(); 
    jLabel4 = new javax.swing.JLabel(); 
    jLabel5 = new javax.swing.JLabel(); 
    Close_Button = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 
    setTitle("About: SubTitle Editor 4.8.1.1"); 

    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/about.png"))); // NOI18N 
    jLabel1.setText("jLabel1"); 

    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 15)); 
    jLabel2.setText("SubTitle Editor "); 

    jLabel3.setText("A simple subtitle editor of vedio "); 

    jLabel4.setText("<html>You can make bangla and english subtitle using this software"); 

    jLabel5.setText("Product version: 4.8.1.1"); 

    Close_Button.setText("Close"); 
    Close_Button.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      Close_ButtonActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(18, 18, 18) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
         .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE) 
         .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
         .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE) 
         .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
        .addContainerGap(22, Short.MAX_VALUE)) 
       .addGroup(layout.createSequentialGroup() 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(Close_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addContainerGap()))) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jLabel1) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(11, 11, 11) 
      .addComponent(jLabel2) 
      .addGap(18, 18, 18) 
      .addComponent(jLabel3) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(1, 1, 1) 
      .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(18, 18, 18) 
      .addComponent(Close_Button)) 
    ); 

    pack(); 
}// </editor-fold> 


/** 
* @param args the command line arguments 
*/ 
/* 
public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      About dialog = new About(new javax.swing.JFrame(), true); 
      dialog.addWindowListener(new java.awt.event.WindowAdapter() { 
       public void windowClosing(java.awt.event.WindowEvent e) { 
        System.exit(0); 
       } 
      }); 
      dialog.setVisible(true); 
     } 
    }); 
} 
*/ 
    // Variables declaration - do not modify 
    private javax.swing.JButton Close_Button; 
     private javax.swing.JLabel jLabel1; 
     private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
     private javax.swing.JLabel jLabel4; 
     private javax.swing.JLabel jLabel5; 
    // End of variables declaration 

    } 

现在JFrame的示例代码

/* 
* JFrame_of_subtitle.java 
* 
* Created on Apr 2, 2012, 5:58:46 AM 
*/ 

package subtitlemakerofvedio; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.Timer; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.event.TableModelEvent; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableModel; 


public class JFrame_of_subtitle extends javax.swing.JFrame 
{ 
    /** Creates new form JFrame_of_subtitle */ 
    public MediaPanel media_play; 
    public javax.swing.Timer time; 
    public JFrame_of_subtitle(MediaPanel media, javax.swing.Timer t) 
    { 
     super("Subtitle Editor 4.8.1.1"); 
     media_play=media; 
     time=t; 
     initComponents(); 
    } 

    private void initComponents() { 
      //some code....... 
    }    

    //this is for the JDialog box 
    private void About_menuitemActionPerformed(java.awt.event.ActionEvent evt) 
    { 
     // TODO add your handling code here: 
     //About a=new About(this, rootPaneCheckingEnabled); 

     About dialog = new About(new javax.swing.JFrame(), true); 
     dialog.addWindowListener(new java.awt.event.WindowAdapter() 
     { 
      @Override public void windowClosing(java.awt.event.WindowEvent e) 
      { 
       System.exit(0); 
      } 
     }); 

     dialog.setVisible(true); 
    } 
} 
+0

http://stackoverflow.com/questions/1301583/programatically-closing-currently-displayed-java-swing-jdialog-box – sarwar026

+0

你如何关闭dailogbox? –

+0

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); –

回答

4

嗯,你说的是窗子爬进去的关闭后退出程序。

dialog.addWindowListener(new java.awt.event.WindowAdapter() { 
    @Override 
    public void windowClosing(java.awt.event.WindowEvent e) { 
     System.exit(0); 
    } 
}); 

尝试设置:

dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

所以你的窗口将获得当它获得的关闭布置。

编辑:只需将我输入的第一个代码替换为第二个代码即可。

+0

他已经设置了默认的关闭操作,但是他做了一个system.exit(0) –

+0

,所以他只需要删除我认为的窗口监听器 –

+0

该代码是如此混乱,我没有看到它,对不起。 – SHiRKiT

1

也许尝试使用closeAndHide()而不是System.exit(0)。 System.exit(0)用于告诉一切关闭。