进度条不起作用 [英] Progress bar not working

查看:53
本文介绍了进度条不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,我在运行进度条时遇到问题,请检查我的代码并告诉我问题出在哪里:

Good Evening, i have a problem in running a progress bar, please check my code and tell me where is the problem:

/**
** @author Islam */

public class Register extends javax.swing.JFrame implements Runnable {

static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
private Pattern pattern;
private Matcher matcher;
private static final String EMAIL_PATTERN =
        "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
        + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
boolean isValidPassord = true;
Thread th;

public Register() {
    initComponents();
    passwordTextF.addKeyListener(new passwordListener());
    passwordMeter = new JProgressBar();
    passwordMeter.setMinimum(MY_MINIMUM);
    passwordMeter.setMaximum(MY_MAXIMUM);
    th = new Thread(this);
    th.start();
}

public void updateBar(int newValue) {
    passwordMeter.setValue(newValue);
    passwordMeter.setStringPainted(true);
    System.out.println(newValue);
}

public void run() {
    for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
        updateBar(i);
        try {
            Thread.sleep(100);

        } catch (InterruptedException ex) {
            Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

class passwordListener implements KeyListener {

    public void keyPressed(KeyEvent e) {
        passwordLabel.setForeground(Color.BLACK);

    }

    public void keyReleased(KeyEvent e) {

        //  Register.this.validatePassword(passwordTextF.getText());
        if (!isValidPassord) {
            //  new PasswordMeterHandler().start();
        }

    }

    public void keyTyped(KeyEvent e) {
    }
}

public boolean validateEmail(String mailFromForm) {

    pattern = Pattern.compile(EMAIL_PATTERN);
    matcher = pattern.matcher(mailFromForm);
    return matcher.matches();

}



// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    int i = 0;
    this.validateEmail(nameTextF.getText());
    this.validatePassword(passwordTextF.getText());
    if (!matcher.matches()) {
        EMailLabel.setForeground(Color.red);
    }
    if (!isValidPassord) {
        passwordLabel.setForeground(Color.red);
    }


}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
}

public static void main(String args[]) {


    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            Register frame = new Register();
            frame.setVisible(true);




        }
    });
}
// Variables declaration - do not modify
private javax.swing.JLabel EMailLabel;
private javax.swing.JTextField EMailTextF;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JComboBox countryCombo;
private javax.swing.JComboBox genderCombo;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel nameLabel;
private javax.swing.JTextField nameTextF;
private javax.swing.JLabel passwordLabel;
private javax.swing.JProgressBar passwordMeter;
private javax.swing.JPasswordField passwordTextF;
private javax.swing.JLabel strengthLabel;
private javax.swing.JLabel userNameLabel;
private javax.swing.JTextField userNameTextF;
// End of variables declaration
}

推荐答案

让我们从这样一个事实开始:在您的示例中,进度条从未添加到任何内容中,然后继续违反最重要的 Swing 规则之一- 永远,永远不要在事件调度线程之外创建或修改任何 UI 元素.

Let's start with the fact that, in your example, the progress bar is never added to anything, then move onto the violation of one of the most important Swing rules - NEVER, EVER create or modify ANY UI element out side of the Event Dispatching Thread.

KeyListener 不是跟踪文本组件更改的合适方法,它们不会考虑用户是否将文本粘贴到字段中.

KeyListener is not an appropriate method for tracking changes to text components, they do not take into account if the user pastes text into the field.

您应该使用 DocumentListener 来监视对基础文档的更改,如果要更改进入该字段的内容,则应该使用 DocumentFilter...

You should use a DocumentListener to monitor changes to the underlying document and a DocumentFilter if you want to change the content going into the field...

public class TestPasswordField {

    public static void main(String[] args) {
        new TestPasswordField();
    }

    public TestPasswordField() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JPasswordField password;
        private JProgressBar progressBar;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            password = new JPasswordField(10);
            progressBar = new JProgressBar(0, 10);
            password.getDocument().addDocumentListener(new DocumentListener() {

                protected void updateProgress() {
                    progressBar.setValue(password.getPassword().length);
                }

                @Override
                public void insertUpdate(DocumentEvent e) {
                    updateProgress();
                }

                @Override
                public void removeUpdate(DocumentEvent e) {
                    updateProgress();
                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                    updateProgress();
                }
            });

            add(password, gbc);
            gbc.gridy++;
            add(progressBar, gbc);
        }

    }

}

这篇关于进度条不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆