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!");

class MyActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

public class password {

   public static void main(String[] args) {
        JFrame frame = new JFrame("SimpleSwingExample");
        JPanel jp = new JPanel();
        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);

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

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


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 {

Hello ethio
Look at the example:
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 article has been dead for over six months. Start a new discussion instead.