Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.*;
- import javax.swing.*;
- 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;
- public void start()
- {
- try
- {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- catch (Exception e)
- {
- }
- new Login().buildLogin();
- }
- private 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(Color.white);
- headingLabel.setVisible(true);
- usernameLabel.setText(" Username: ");
- usernameLabel.setForeground(Color.blue);
- usernameLabel.setVisible(true);
- passwordLabel.setText(" Password: ");
- passwordLabel.setForeground(Color.blue);
- passwordLabel.setVisible(true);
- // Buttons
- loginBtn.setText("Login");
- loginBtn.setVisible(true);
- loginBtn.addActionListener(new Validate1());
- // Panels
- northPanel.add(headingLabel);
- northPanel.setBackground(Color.blue);
- 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 Class
- static class Validate1 implements ActionListener
- {
- @SuppressWarnings("deprecation")
- public void actionPerformed(ActionEvent e)
- {
- String username = null;
- String password = null;
- try
- {
- username = usernameField.getText().toUpperCase().trim();
- password = passwordField.getText().toUpperCase().trim();
- if (username.equals("") && password.equals(""))
- {
- usernameLabel.setForeground(Color.red);
- passwordLabel.setForeground(Color.red);
- }
- else
- {
- if (username.equals(""))
- {
- usernameLabel.setForeground(Color.red);
- passwordLabel.setForeground(Color.blue);
- }
- else
- {
- if (password.equals(""))
- {
- usernameLabel.setForeground(Color.blue);
- passwordLabel.setForeground(Color.red);
- }
- else
- {
- usernameLabel.setForeground(Color.red);
- passwordLabel.setForeground(Color.red);
- new Login().validate2(username, password);
- }
- }
- }
- }
- catch (Exception validateError)
- {
- JOptionPane.showMessageDialog(null, "There was an error Class::Login Class Validate1", "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- //Validate2 Method
- private void validate2(String getUsername, String getPassword)
- {
- String username = null;
- String password = null;
- try
- {
- username = getUsername.toUpperCase();
- password = getPassword.toUpperCase();
- if (username.contains(" ") && password.contains(" "))
- {
- JOptionPane.showMessageDialog(null, "The username and password is invalid", "Error", JOptionPane.ERROR_MESSAGE);
- }
- else
- {
- if (username.contains(" "))
- {
- JOptionPane.showMessageDialog(null, "The username is invalid", "Error", JOptionPane.ERROR_MESSAGE);
- }
- else
- {
- if (password.contains(" "))
- {
- JOptionPane.showMessageDialog(null, "The password is invalid", "Error", JOptionPane.ERROR_MESSAGE);
- }
- else
- {
- usernameLabel.setForeground(Color.red);
- passwordLabel.setForeground(Color.red);
- System.out.println("All good!!");
- }
- }
- }
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null, "Error in Class::Login Method:valsidate2","Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- public class Main
- {
- public static void main(String[] args)
- {
- new Login().start();
- }
- }
- import java.io.*;
- public class FileData
- {
- public static void main(String[] args) throws IOException
- {
- String file_name = "PATH TO THE TEXT FILE WOULD BE HERE";
- try
- {
- ReadFile file = new ReadFile(file_name);
- String[] aryLines = file.OpenFile();
- for(int i = 0; i < aryLines.length; i++)
- {
- System.out.println(aryLines[i]);
- }
- }
- catch (IOException e)
- {
- //System.out.println(e.getMessage());
- System.out.println("Sorry, dude - no can do!!!n" + e.getMessage());
- }
- try
- {
- WriteFile data = new WriteFile(file_name , true );
- data.writeToFile("This is another line of text");
- }
- catch(IOException e)
- {
- System.out.println("Sorry, dude - no can do!!!n" + e.getMessage());
- }
- System.out.println("Text File Written To");
- }
- }
- import java.io.*;
- public class ReadFile
- {
- private String path;
- public ReadFile(String file_path)
- {
- path = file_path;
- }
- public String[] OpenFile() throws IOException
- {
- FileReader inFile = new FileReader(path);
- BufferedReader textReader = new BufferedReader(inFile);
- int numOfLines = readLines();
- String[] textData = new String[numOfLines];
- for(int i = 0; i < numOfLines; i++)
- {
- textData[i] = textReader.readLine();
- }
- textReader.close();
- return textData;
- }
- int readLines() throws IOException
- {
- FileReader file_to_read = new FileReader(path);
- BufferedReader bf = new BufferedReader(file_to_read);
- String aLine;
- int numOfLines = 0;
- while((aLine = bf.readLine()) != null)
- {
- numOfLines++;
- }
- bf.close();
- return numOfLines;
- }
- }
- import java.io.*;
- public class WriteFile
- {
- private String path;
- private boolean append_to_file = false;
- public WriteFile(String file_path)
- {
- path = file_path;
- }
- public WriteFile(String file_path, boolean append_value)
- {
- path = file_path;
- append_to_file = append_value;
- }
- public void writeToFile(String textLine) throws IOException
- {
- FileWriter write = new FileWriter(path, append_to_file);
- PrintWriter print_line = new PrintWriter(write);
- print_line.printf("%s" + "%n" , textLine);
- print_line.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement