I made a program that checks a password that you enter. This is different than most because i want to enter it using buttons. If you get it right the program display "Correct" if you get it wrong it displays "Incorrect". The problem is when i run the program it always say the password is incorrect. Please help me fix this problem. You can find the problem in the enterListener new the bottom.
Thanks.
//============================================EnterListener class EnterListener implements ActionListener { public void actionPerformed(ActionEvent evt) { Object source = (JButton)evt.getSource(); // The password is 8897 // Here is the problem if (source.equals(buttonOrder)) { _displayField.setText("Correct"); } else { _displayField.setText("Incorrect, try again"); } } }
Here is the full program.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Password2 extends JFrame { //=========================================== private static final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20); //=================================================== private JTextField _displayField; private boolean _startNumber = true; private boolean code = true; //================================================ public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception unused) { ; } Password2 window = new Password2(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } //=========================================================== public Password2() { _displayField = new JTextField("Enter Password", 12); _displayField.setHorizontalAlignment(JTextField.RIGHT); _displayField.setFont(BIGGER_FONT); ActionListener enterListener = new EnterListener(); JButton clearButton = new JButton("Clear"); clearButton.setFont(BIGGER_FONT); clearButton.addActionListener(new ClearListener()); JButton enterButton = new JButton("Enter"); enterButton.setFont(BIGGER_FONT); enterButton.addActionListener(enterListener); ActionListener numListener = new NumListener(); String buttonOrder = "123456789 0 "; JPanel buttonPanel = new buttonPanel(); buttonPanel.setLayout(new GridLayout(5, 3, 2, 2)); for (int i = 0; i <buttonOrder.length(); i++) { String keyTop = buttonOrder.substring(i, i+1); JButton b = new JButton(keyTop); if (keyTop.equals(" ")) { b.setEnabled(false); } else { b.addActionListener(numListener); b.setFont(BIGGER_FONT); } buttonPanel.add(b); } JPanel clearPanel = new clearPanel(); clearPanel.setLayout(new FlowLayout()); clearPanel.add(clearButton); clearPanel.add(enterButton); JPanel content = new JPanel(); content.setLayout(new BorderLayout(5, 5)); content.add(_displayField, BorderLayout.NORTH ); content.add(buttonPanel , BorderLayout.CENTER); content.add(clearPanel ,BorderLayout.SOUTH ); content.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); this.setContentPane(content); this.pack(); this.setTitle("Password"); this.setResizable(false); this.setLocationRelativeTo(null); } //================================================ class buttonPanel extends JPanel { public void paintComponent(Graphics comp) { super.paintComponent(comp); Graphics2D comp2D = (Graphics2D)comp; comp2D.setColor(Color.green); setBackground(Color.black); int width = getSize().width; int height = getSize().height; Font currentFont = new Font("Serif" , Font.BOLD+Font.ITALIC, 36); comp2D.setFont(currentFont); comp2D.drawString("Password", width - 200, height - 5); } } //========================================================== class clearPanel extends JPanel { public void paintComponent(Graphics comp) { super.paintComponent(comp); Graphics2D comp2D = (Graphics2D)comp; comp2D.setColor(Color.red); setBackground(Color.blue); } } //========================================================= private void actionClear() { _startNumber = true; _displayField.setText("0"); } //////////////////////////////////// inner listener class NumListener class NumListener implements ActionListener { public void actionPerformed(ActionEvent e) { String digit = e.getActionCommand(); if (_startNumber) { _displayField.setText(digit); _startNumber = false; } else { _displayField.setText(_displayField.getText() + digit); } } } //============================================EnterListener class EnterListener implements ActionListener { public void actionPerformed(ActionEvent evt) { Object source = (JButton)evt.getSource(); // The password is 8897 // Here is the problem if (source.equals(buttonOrder)) { _displayField.setText("Correct"); } else { _displayField.setText("Incorrect, try again"); } } } //////////////////////////////////// inner listener class ClearListener class ClearListener implements ActionListener { public void actionPerformed(ActionEvent e) { actionClear(); } } }