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