jTextfield和progressbar不工作



我有一个主类(基本上是一个netbeans表单;拖放)和另一个类(称为class 2),前者是我的应用程序启动的地方,后者是我的函数所在的地方。我首先从主方法调用类2中的一个函数,该方法有一个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);
    }
}

现在,这里是我调用其他类中存在的函数的主要方法(上面给出的代码)

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
}

这里有一个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);
    }
}

您在Swing 中遇到Concrency问题

代码行

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

最新更新