Hello there
I have been given an assignment at Uni to write program (obviously thus im here hehe)
The user (customer wishing to purchase a computer) should be able to login by typing his
user name and password and pressing the “Login” button. The layout of this first screen
of the program should look like the one displayed in Figure 1. Note, that the characters
in the password box should not be displayed while the user is typing them. Asterisks (*)
should be displayed instead.
When the application frame is resized the login screen should still look as in Figure 1, i.e.
relative positioning of components should be maintained.
2. When the user types the valid user name and password and presses the “login” button,
the background changes to green and the string “Welcome to ECSE501 Computers” is
displayed using a JLabel. For simplicity, the correct user name and password can be
hardcoded.
When the user types an incorrect user name or password, the background of the window
becomes red, the message “Incorrect login name/password” is displayed and the user is
given a chance to re-enter a username and password.
What i have so far is
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ecse501.object.oriented.development;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
*
* @author boss
*/
public class ECSE501OBJECTORIENTEDDEVELOPMENT {
/**
* @param args the command line arguments
*/
private static String password = "pass";
private static String user = "Maciek";
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome to ECSE Computers");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
// frame.setResizable(false);// IF this option enabled nothing displays... You have to manually resize
// window in order to show things (??)
JLabel label = new JLabel("Password");
JLabel label1 = new JLabel("Username");
JPanel panel1 = new JPanel();
JPanel panel = new JPanel();
JButton label2 = new JButton("Log in");
JButton panel2 = new JButton();
frame.add(panel2);
frame.add(panel);
frame.add(panel1);
JPasswordField pass = new JPasswordField(10);
pass.setEchoChar('*');
pass.addActionListener(new AL());
JTextField uss = new JTextField(10);
uss.addActionListener(new AL());
JButton log = new JButton();
log.addActionListener(new AL());
panel.add(label, BorderLayout.WEST);
panel.add(pass, BorderLayout.WEST);
panel1.add(uss, BorderLayout.WEST);
panel1.add(label1, BorderLayout.WEST);
panel2.add(label2, BorderLayout.WEST);
panel2.add(log, BorderLayout.WEST);
}
static class AL implements ActionListener {
@Override
public void actionPerformed (ActionEvent e) {
JPasswordField input = (JPasswordField) e.getSource();
char[] passy = input.getPassword();
String p = new String(passy);
JTextField input1 = (JTextField) e.getSource();
String usr = input1.getName();
String u = usr;
if(p.equals(password)){
class frame extends JFrame
{
frame()
{
getContentPane().setBackground(Color.green);
}
}
}
else{
JOptionPane.showMessageDialog(null, "Incorrect");
class frame extends JFrame
{
frame()
{
getContentPane().setBackground(Color.RED);
}
}
}
if(u.equals(user)){
class frame extends JFrame
{
frame()
{
getContentPane().setBackground(Color.green);
}
}
}
else{
JOptionPane.showMessageDialog(null, "Incorrect");
class frame extends JFrame
{
frame()
{
getContentPane().setBackground(Color.RED);
}
}
}
}
// rest to come
}
}
Not a lot but working on it all time.
And the problem is...
1.Content is not properly displayed, only one text field displayed instead of 2 (username and password)
2. Probably because the content is not properly displayed the background color does not change, but still that is the 2nd problem
3. It doesnt show any errors in NetBeans but when i compile it nothing happens and then bunch of unknown to me errors are coming up.
I have added attachment of how the display should roughly look like
thank you
ps. forgot to mention I am not allowed to use automatic generated code like the one you find in netbeans to create GUI therefore i have to create gui by my self (so it makes task bit harder)