0

Hello all,
I'm creating a login application using swing but ive run into problems already,
I need help on making the if else statement work so the background is red.
Your help is very much appreciated,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyWindowListener extends WindowAdapter {

    public void windowClosing(WindowEvent e) {
        System.out.println("Closing window!");
        System.exit(0);
    }
}

class MyActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        System.out.println();
    }
}

public class password {

   public static void main(String[] args) {
        JFrame frame = new JFrame("SimpleSwingExample");
        JPanel jp = new JPanel();
        //jp.setBackground(Color.green);
        //frame.setContentPane(jp);
        frame.add(jp);
        
        JPanel logpnl = new JPanel();
        logpnl.setLayout(new BoxLayout(logpnl, BoxLayout.X_AXIS));
        //Container c = frame.getContentPane();
        JLabel label = new JLabel("login");
        JTextField text = new JTextField(15);
        logpnl.add(label);
        logpnl.add(text);

        JPanel passpnl = new JPanel();
        // Container c2 = frame.getContentPane();
        passpnl.setLayout(new BoxLayout(passpnl, BoxLayout.X_AXIS));
        JLabel label2 = new JLabel("password");
        JPasswordField pass = new JPasswordField(8);
        pass.setEchoChar('*');
        passpnl.add(label2);
        passpnl.add(pass);
        frame.getContentPane().add(passpnl, "West");
        pass.addActionListener(new ALL());

        JPanel bttnpnl = new JPanel();
        bttnpnl.setLayout(new BoxLayout(bttnpnl, BoxLayout.Y_AXIS));
        JButton button = new JButton("Login");

        bttnpnl.add(button);
        frame.getContentPane().add(logpnl, "North");
        frame.getContentPane().add(bttnpnl, "South");

// register an event handler for frame events
        frame.addWindowListener(new MyWindowListener());
        frame.setSize(550, 520);
        frame.setVisible(true);

        //frame.pack();
    }
}

class ALL implements ActionListener {
private static String password = "pass";
JFrame frame;

        public void actionPerformed(ActionEvent e) {
            JPasswordField input = (JPasswordField) e.getSource();
            char[] passy = input.getPassword();
            String p = new String(passy);
            if (p.equals(password)) {
                JOptionPane.showMessageDialog(null, "correct");
            }
            else {
                frame.getContentPane().setBackground(Color.red);
            }
        }
     }
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by quuba
0

Hello ethio
Look at the example:
http://download.oracle.com/javase/tutorial/uiswing/examples/misc/InputVerificationDialogDemoProject/src/misc/InputVerificationDialogDemo.java
Use this example as a pattern to work.
Particularly start your application in same way.
You can copy as a whole two methods:
public static void main(String[] args)
private static void createAndShowGUI()
In the last method change only name InputVerificationDialogDemo to Password.
Your class Password should extends JPanel.
//
Is no need if-else statement. Use javax.swing.event.CaretListener applied to JPasswordField in opposition to ActionListener.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.