2012-12-19 55 views
2

我從來沒有理解SwingUtilities.invokeLater,這是我直到現在一直避免它的原因。但它對於使Swing線程安全非常重要。 請原諒我,因爲這是我第一次與這個。 我想經過一些數據的基礎上query.So關閉窗口,據我瞭解,在 任何UI必須更新用由SwingUtilities.invokeLater。這裏處理是我的工作:SwingUtilities.invokeLater:問題

主營:

public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 
    public void run() { 
    new forminsert().setVisible(true); 
forminsert f=new forminsert(); 
    } 
    }); 

    } 
    public forminsert() { 
    initComponents(); 
} 

public class forminsert extends javax.swing.JFrame {  
private void initComponents() { 

    jPanel1 = new javax.swing.JPanel(); 
    jTextField1 = new javax.swing.JTextField(); 
    jButton1 = new javax.swing.JButton(); 
    jButton2 = new javax.swing.JButton(); 
    jButton3 = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setUndecorated(true); 

    jPanel1.setBackground(new java.awt.Color(0, 0, 0)); 
    jPanel1.setBorder(javax.swing.BorderFactory.createMatteBorder(3, 3, 3, 3, new java.awt.Color(0, 204, 204))); 
       /////***REST CODE***/////   

} 

聽者添加按鈕

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)    {           
new Thread(new thread1()).start(); 

}           
public class thread1 implements Runnable 
{ 

    public void run() { 
    insert ins=new insert(); 
    ////code for inserting///  
    } 
} 

上瀏覽按鈕聽者

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)    {           
    JFileChooser ss=new JFileChooser(); 
    ////Code to choose the file//// 
       }           

這裏是問題,代碼退出。

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { 

forminsert f=new forminsert();f.call(); 
} 

public void call() 
{ 
java.awt.EventQueue.invokeLater(new Runnable() { 
    public void run() { 
jPanel1.setVisible(false); 
getRootPane().setVisible(false); 

    } 
    }); 

屏幕: screen short

請解釋我在哪裏,我錯了,我不希望使用System.exit,請原諒,我問太多questions.Thanks。

UPDATE:

這裏是工作代碼由@trashgod

package faltur; 

import java.io.File; 
import javax.swing.JFileChooser; 

public class insert extends javax.swing.JFrame { 

/** Creates new form insert */ 
public insert() { 
    initComponents(); 
} 


@SuppressWarnings("unchecked") 

private void initComponents() { 

    jPanel1 = new javax.swing.JPanel(); 
    jTextField1 = new javax.swing.JTextField(); 
    jButton1 = new javax.swing.JButton(); 
    jButton2 = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setUndecorated(true); 

    jPanel1.setForeground(new java.awt.Color(240, 240, 240)); 

    jButton1.setText("Browse.."); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jButton2.setText("exit"); 
    jButton2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton2ActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addContainerGap() 
        .addComponent(jButton1) 
        .addGap(31, 31, 31) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(jPanel1Layout.createSequentialGroup() 
        .addGap(153, 153, 153) 
        .addComponent(jButton2))) 
      .addContainerGap(99, Short.MAX_VALUE)) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(jPanel1Layout.createSequentialGroup() 
      .addGap(66, 66, 66) 
      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addGap(87, 87, 87) 
      .addComponent(jButton2) 
      .addContainerGap(94, Short.MAX_VALUE)) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
    ); 

    pack(); 
} 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)    {           
JFileChooser ss=new JFileChooser(); 
ss.showOpenDialog(jTextField1); 

File f=ss.getSelectedFile(); 

    path=f.toString(); 
    System.out.println(path); 

}           

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
    insert in=new insert(); 
    in.exit();/////////////CALLS exit()/////////////////modify this////// 
} 


public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new insert().setVisible(true); 
     } 
    }); 
} 
public void exit()////////////////////////MODIFY THIS/////////////////// 
{ 
jPanel1.getRootPane().setVisible(false); 

} 

private javax.swing.JButton jButton1; 
private javax.swing.JButton jButton2; 
private javax.swing.JPanel jPanel1; 
private javax.swing.JTextField jTextField1; 

public String path=""; 
} 

的要求,請運行,並告知如何修改exit()使其再次關閉window.Thanks樣本

+0

對於初學者,爲什麼**你不**在那裏使用'SwingUtilities.invokeLater'? –

+0

@MarkoTopolnik究竟在哪裏? –

+0

在你使用'invokeLater'的時候。 –

回答

6

由於您的框架的默認關閉操作是EXIT_ON_CLOSE,您可以通過dispatchEvent()發送WindowEvent.WINDOW_CLOSING,如here所示。給定一個JFrame f

private static final String EXIT = "Exit"; 
private Action exit = new AbstractAction(EXIT) { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      f.dispatchEvent(new WindowEvent(
       f, WindowEvent.WINDOW_CLOSING)); 
     } 
    }; 
private JButton b = new JButton(exit); 

參見How to Use Actions並遵循共同coding conventions

附錄:根據修改的代碼,這裏有一個方法退出該程序。注意類名通常以大寫字母開頭,而實例名稱則爲小寫字母。

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
    insert.dispatchEvent(new WindowEvent(
     insert, WindowEvent.WINDOW_CLOSING)); 
} 

public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      insert = new Insert(); 
      insert.setVisible(true); 
     } 
    }); 
} 

private static Insert insert; 
... 
+0

爲什麼getRootPane()。setVisible(false)它不工作在我的代碼? –

+0

我沒有看到你需要'getRootPane()',除了可能調用'setDefaultButton()';並且你不需要'Action'中任何出處的'invokeLater()'。如果您仍有問題,請編輯您的問題以包含[sscce](http://sscce.org/)。 – trashgod

3

好insted的使用foo.setVisible(false)的, 只是我把它改爲setVisible(false),和它的工作。

+1

我是否正確,您不想退出程序,只需關閉窗口? – trashgod

+0

@trashgod yup否則會使用System.exit –

+0

啊,我誤解了。我以爲你想避免'System.exit()',因爲它突然繞過監聽器等等。 – trashgod

相關問題