2012-04-01 41 views
2

我有一個主類(它基本上是一個NetBeans形式;拖放)從我的應用程序啓動的位置和另一個類(稱爲類2),我的函數駐留。我首先調用一個函數class2從我的主要方法和該方法有一個while循環遞增計數器。取決於該計數器,我調用我的主類的函數,並嘗試顯示計數器在文本字段中,也st進度條在中間但它不工作儘管它顯示正確的打印語句(計數器)。jTextfield和進度條不起作用

一些代碼我加入它給我的問題,因爲它既不更新也不進度更新textfield.Kindly幫我這究竟是爲什麼

我已編輯的代碼,但它仍然顯示沒有什麼:(

public class NewClass 
{ 
    public static int counter = 0; 
    public NewJFrame p = new NewJFrame(); 
    public void packet() 
    { 
     try 
     { 
      while (true) 
      { 
       //some code ,right now which i have omitted 
       counter = counter + 1; 
       counter2(counter); 
      } 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    public void counter2(int counter) 
    { 
     counter3(); 
    } 

    public void counter3() 
    { 

     p.progress(counter); 
    } 
} 

現在,這裏是從其中i調用函數存在於其它類的主要方法()的代碼被上面給出)

public class NewJFrame extends javax.swing.JFrame 
{ 

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

    @SuppressWarnings("unchecked") 
    public void progress(int y) 
    { 
     jProgressBar1.setIndeterminate(true); 
     jTextField1.setText(y+"packets processed"); 
     System.out.println(y); 
    } 

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) 
    {  
     NewClass m=new NewClass(); 
     m.packet(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) 
    {   
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     java.awt.EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new NewJFrame().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    private javax.swing.JProgressBar jProgressBar1; 
    private javax.swing.JTextField jTextField1; 
    // End of variables declaration 
} 
+2

你在count中創建'NewJFrame'類的New Object er3()'方法,每個'while'循環迭代。創建只有一個實例,並使用該實例更新值:-) – 2012-04-01 09:13:22

+0

我們再次去... – 2012-04-01 09:22:15

+0

@GagandeepBali哦,是的,你的權利,但它仍然沒有顯示任何內容:( – Xara 2012-04-01 09:22:40

回答

4

這裏有SwingWorker一個小例子將幫助您在運行更新JTextField

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

public class NewJFrame extends javax.swing.JFrame 
{ 

    /** Creates new form NewJFrame */ 
    public NewJFrame() 
    { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setLocationByPlatform(true); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BorderLayout()); 

     jTextField1 = new JTextField(10); 
     contentPane.add(jTextField1, BorderLayout.PAGE_START); 

     jProgressBar1 = new JProgressBar(0, 100);  
     contentPane.add(jProgressBar1, BorderLayout.CENTER); 

     jButton1 = new JButton("START"); 
     jButton1.addMouseListener(new MouseAdapter() 
     { 
      public void mouseClicked(MouseEvent me) 
      { 
       jProgressBar1.setIndeterminate(true); 
       jButton1MouseClicked(me); 
      } 
     }); 
     contentPane.add(jButton1, BorderLayout.PAGE_END); 

     setContentPane(contentPane); 
     pack(); 
     setVisible(true); 
    } 

    @SuppressWarnings("unchecked") 
    public void progress(final int y) 
    { 
     System.out.println("progress Method is working."); 
     /* 
     * This thing needs to be done on Event 
     * Dispatcher Thread. 
     */ 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       jTextField1.setText(y+"packets processed"); 
       System.out.println(y); 
      }   
     });   
    } 

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) 
    {  
     NewClass m=new NewClass(this); 
     m.execute(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) 
    {   
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     java.awt.EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new NewJFrame().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    public javax.swing.JProgressBar jProgressBar1; 
    private javax.swing.JTextField jTextField1; 
    // End of variables declaration 
} 

class NewClass extends SwingWorker<Void, Void> 
{ 
    public static int counter = 0; 
    // Added this variable to keep the instance. 
    private NewJFrame p; 
    private boolean flag = true; 

    public NewClass(NewJFrame frame) 
    { 
     p = frame; 
    } 

    public Void doInBackground() 
    { 
     while(flag) 
     { 
      counter = counter + 1; 
      counter2(counter); 
     } 
     return null; 
    } 

    public void done() 
    { 
     System.out.println("I am DONE"); 
    } 

    public void counter2(int counter) 
    { 
     counter3(); 
    } 

    public void counter3() 
    { 
     p.progress(counter); 
    } 
} 
+0

if(counter == 10)中的語句導致jProgressbar具有私人訪問權限的錯誤。我對這些語句發表了評論。現在會發生什麼,它只顯示數據包10的結果,數據包10處理的結果。我試圖把條件,如果(計數器> = 0),但不起作用.... – Xara 2012-04-01 11:05:26

+0

不,你只需要使用一個SwingWorker,如果你在'while'循環中做的所謂thingy是一項艱鉅的任務,這個例子只是爲了告訴你,while循環阻塞了你的EDT,只是爲了更新你需要的JProgressBar。在'SwingWorker Thread'內部的''while'循環內部執行這個任務,這樣你的'Swing'應用程序就可以和用戶交互,永遠不會凍結。當你刪除該條件時,你的while循環繼續,這會阻止你的EDT並且你的應用程序凍結。 – 2012-04-01 11:26:37

+0

@Zara:請觀看我的最新編輯,我使用了SwingWorker,它可以讓你在運行時更新你的'JTextField' :-) – 2012-04-01 14:00:43

3

您已經得到問題與Concurency in Swing

行代碼

jProgressBar1.setIndeterminate(true); 
jTextField1.setText(y+"packets processed"); 

應該

java.awt.EventQueue.invokeLater(new Runnable() { 

    @Override 
    public void run() { 
     jProgressBar1.setIndeterminate(true); 
     jTextField1.setText(y + "packets processed"); 
    } 
}); 
+0

通過這樣做,它首先要求我做出最後的決定;我做到了,但它仍然沒有顯示任何內容:( – Xara 2012-04-01 09:11:07

+0

+1,對於SwingWorker是需要小時的,在這種情況下:-) – 2012-04-01 09:44:36