-2

こんばんは、進行状況バーの実行に問題があります。コードを確認して、どこに問題があるか教えてください:

/**
** @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
}
4

2 に答える 2

0

JFrame に JProgressBar を追加し、setVisible を呼び出して、コンストラクターの外部でスレッドを開始します (スレッドの早期終了のため)。

public class Register extends JFrame {

static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
private JProgressBar passwordMeter;

public Register() {
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    setBounds(50, 50, 640, 100);
    passwordMeter = new JProgressBar();
    passwordMeter.setMinimum(MY_MINIMUM);
    passwordMeter.setMaximum(MY_MAXIMUM);
    add(passwordMeter, BorderLayout.CENTER);
}

public void start() {
    new PasswordMeterHandler().start();
}

class PasswordMeterHandler extends Thread {

    public void updateBar(final int newValue) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                passwordMeter.setValue(newValue);
                passwordMeter.setStringPainted(true);
            }
        });
        //passwordMeter.setValue(newValue);
        //passwordMeter.setStringPainted(true);
        System.out.println(newValue);
    }

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

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            Register frame = new Register();
            frame.setVisible(true);
            frame.start();
        }
    });
}
}
于 2013-01-07T18:08:10.517 に答える
0

あなたの例では、プログレスバーが何にも追加されないという事実から始めましょう。次に、最も重要なSwingルールの1つに違反することに移りましょう-イベントディスパッチスレッドの外でUI要素を絶対に作成または変更しない.

KeyListenerは、テキスト コンポーネントの変更を追跡するための適切な方法ではありません。ユーザーがテキストをフィールドに貼り付けた場合は考慮されません。

DocumentListener基になるドキュメントへの変更を監視するには を使用DocumentFilterし、フィールドに入るコンテンツを変更する場合は を使用する必要があります...

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

    }

}
于 2013-01-08T00:22:31.717 に答える