Java Login Form Part 3

Published: 28 September 2014
on channel: Christian Morgan
1,773
3

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
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.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class Login {


private static JFrame frame;
private static JPanel northPanel;
private static JPanel centerPanel;
private static JPanel southPanel;
private static JLabel headingLabel;
private static JLabel usernameLabel;
private static JLabel passwordLabel;
private static JTextField usernameField;
private static JPasswordField passwordField;
private static JButton loginBtn;

/*
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
*/

public void start(){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){

}
new Login().buildLogin();
}

private static void buildLogin(){

// Build objects
frame = new JFrame();
northPanel = new JPanel();
centerPanel = new JPanel();
southPanel = new JPanel();
headingLabel = new JLabel();
usernameLabel = new JLabel();
passwordLabel = new JLabel();
usernameField = new JTextField();
passwordField = new JPasswordField();
loginBtn = new JButton();

// Labels
headingLabel.setText("Login");
headingLabel.setForeground(new Color(255,255,255));
headingLabel.setVisible(true);

usernameLabel.setText(" Username: ");
usernameLabel.setForeground(new Color(50,50,50));
usernameLabel.setVisible(true);

passwordLabel.setText(" Password: ");
passwordLabel.setForeground(new Color(50,50,50));
passwordLabel.setVisible(true);

// Buttons
loginBtn.setText("Login");
loginBtn.setVisible(true);
loginBtn.addActionListener(new Validate1());

// Panels
northPanel.add(headingLabel);
northPanel.setBackground(new Color(200,90,90));
centerPanel.add(usernameLabel);
centerPanel.add(usernameField);
centerPanel.add(passwordLabel);
centerPanel.add(passwordField);
centerPanel.setLayout(new GridLayout(2,2));
southPanel.add(loginBtn);

// JFrame
frame.getContentPane().add(BorderLayout.NORTH,northPanel);
frame.getContentPane().add(BorderLayout.CENTER,centerPanel);
frame.getContentPane().add(BorderLayout.SOUTH,southPanel);
frame.setSize(300,150);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);


}


// Validate1
static class Validate1 implements ActionListener {
public void actionPerformed(ActionEvent e) {

String username = null;
String password = null;

try{
username = usernameField.getText().trim();
password = passwordField.getText().trim();

if(username.equals("") && password.equals("")){
usernameLabel.setForeground(new Color(255,0,0));
passwordLabel.setForeground(new Color(255,0,0));
} else {
if(username.equals("")){
usernameLabel.setForeground(new Color(255,0,0));
passwordLabel.setForeground(new Color(50,50,50));
} else {
if(password.equals("")){
usernameLabel.setForeground(new Color(50,50,50));
passwordLabel.setForeground(new Color(255,0,0));
} else {
usernameLabel.setForeground(new Color(50,50,50));
passwordLabel.setForeground(new Color(50,50,50));
}
}
}

}catch(Exception validateError){

}

}
}

}


On this page of the site you can watch the video online Java Login Form Part 3 with a duration of hours minute second in good quality, which was uploaded by the user Christian Morgan 28 September 2014, share the link with friends and acquaintances, this video has already been watched 1,773 times on youtube and it was liked by 3 viewers. Enjoy your viewing!