监视文本字段填充的数量进度

时间:2016-10-02 16:41:13

标签: java multithreading nullpointerexception

我有一个java应用程序,其注册表单包含一些JTextField个。

当我填写了一些文本字段时,进度条应该显示填充的文本字段数量的百分比。

所以我创建了一个接收进度条和对象(将被转换为JTextField)的参数的类,如下所示。

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ProgressMonitor extends Thread{

    JProgressBar progBar ;
    Object[] component ;

    public ProgressMonitor(JProgressBar progBar, Object... component){
        this.component = component;
        this.progBar = progBar;
    }

    @Override
    public void run() {
        progMon(progBar, component);
        try {
            sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(ProgressMonitor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void progMon(JProgressBar progBar, Object... component){
        int items = component.length;
        float incVal = 100/items;
        float progVal = 0;

        for (int i = 0; i < component.length ; i++) {
            if(component[i].getClass().equals(JTextField.class)){
                if(!((JTextField)component[i]).getText().trim().equals("")){
                    progVal += incVal;
                }
            }else if(component[i].getClass().equals(JTextArea.class)){
                if(!((JTextArea)component[i]).getText().trim().equals("")){
                    progVal += incVal;
                }
            }else if(component[i].getClass().equals(ButtonGroup.class)){
                if(((ButtonGroup)component[i]).getSelection() != null){
                    progVal += incVal;
                }
            }



        }
        System.out.println(Math.round(progVal));
        progBar.setValue(Math.round(progVal));
    }

}

但是当我构建这个类的对象时,

new ProgressMonitor(jProgressBar1, jTextField1, jTextField2).start();

我收到了错误,

Exception in thread "Thread-1" java.lang.NullPointerException

我称之为方法的课程如下:

public class progress extends javax.swing.JFrame {

    /**
     * Creates new form progress
     */
    public progress() {
        initComponents();

    }

    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextField2.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(124, 124, 124)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 432, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(212, 212, 212)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE))))
                .addContainerGap(110, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 137, Short.MAX_VALUE)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32))
        );

        pack();
    }                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                new progress().setVisible(true);
            }

        });

        new ProgressMonitor(jProgressBar1, jTextField1, jTextField2).start();
    }


    public static javax.swing.JProgressBar jProgressBar1;
    public static javax.swing.JTextField jTextField1;
    public static javax.swing.JTextField jTextField2;

}

0 个答案:

没有答案