我怎么只能关闭JDialog盒子?

时间:2012-04-30 13:28:55

标签: java swing jframe jdialog

我从JFrame调用JDialog 但问题是当我退出JDialog框时,整个程序退出意味着JFrame窗口也退出。 我不仅可以退出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);
    }
}

2 个答案:

答案 0 :(得分: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);

所以当你的窗口关闭时,你的窗口会被处理掉。

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

答案 1 :(得分:1)

也许尝试使用closeAndHide()而不是System.exit(0)。 System.exit(0)用于告诉所有内容关闭。