2

アプリケーションの開始場所からメイン クラス (基本的には netbeans フォームです。ドラッグ アンド ドロップ) と、関数が存在する別のクラス (クラス 2 と呼びます) があります。まず、メイン メソッドとそのメソッドから class2 の関数を呼び出します。カウンターをインクリメントするwhileループがあります。そのカウンターに応じて、メインクラスの関数を呼び出し、テキストフィールドにカウンターを表示しようとし、中間でプログレスバーを表示しようとしますが、印刷ステートメントを正しく表示しますが、機能しません(カウンタ)。

プログレスバーの更新もテキストフィールドの更新もしていないため、追加しているコードによって問題が発生しています。なぜこれが起こっているのか教えてください

コードを編集しましたが、まだ何も表示されません:(

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);
    }
}

ここに、上記の他のclass()コードにある関数を呼び出すメインメソッドがあります)

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
}
4

2 に答える 2

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);
    }
}
于 2012-04-01T09:52:03.583 に答える
3

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");
    }
});
于 2012-04-01T09:04:56.743 に答える