Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package busTicketSellingSystem;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import static java.rmi.server.ObjID.read;
- import static java.rmi.server.UID.read;
- import java.util.Scanner;
- import javax.swing.*;
- public class FrmLoginPage extends JFrame implements ActionListener {
- JFrame frmLogin;
- private JLabel lblUsername, lblPassword, lblName;
- JButton btnLogin, btnSignUp, btnClear;
- JTextField txtUsername;
- JPasswordField txtPassword;
- private JLabel lblLogo;
- private static final String LOGO = "C:\\Users\\User\\Desktop\\Java Programming\\BusTicketSellingSystem\\Pics/bus.png";
- private JPanel pnlButton, pnlLogo;
- public FrmLoginPage () {
- frmLogin = new JFrame();
- frmLogin.setMinimumSize(new Dimension(800, 700));
- frmLogin.setLocationRelativeTo(null);
- frmLogin.setTitle("Login");
- frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frmLogin.setLayout(new GridBagLayout());
- GridBagConstraints constraint = new GridBagConstraints();
- pnlButton = new JPanel();
- pnlButton.setLayout(new GridBagLayout());
- lblUsername = new JLabel("Username:");
- lblUsername.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
- lblPassword = new JLabel("Password:");
- lblPassword.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
- lblName = new JLabel("Login Page");
- lblName.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
- txtUsername = new JTextField(100);
- txtUsername.setFont(new Font(Font.SANS_SERIF, Font.LAYOUT_LEFT_TO_RIGHT, 20));
- txtPassword = new JPasswordField(100);
- txtPassword.setFont(new Font(Font.SANS_SERIF, Font.LAYOUT_LEFT_TO_RIGHT, 20));
- btnLogin = new JButton("Login");
- btnLogin.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
- btnSignUp = new JButton("Sign Up");
- btnSignUp.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
- btnClear = new JButton("Clear");
- btnClear.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
- pnlLogo = new JPanel();
- pnlLogo.setLayout(new GridBagLayout());
- lblLogo = new JLabel("", new ImageIcon(getScaledImage(new ImageIcon(LOGO).getImage(), 300, 250)), JLabel.CENTER);
- constraint.gridx = 2;
- constraint.gridy = 0;
- frmLogin.add(pnlLogo, constraint);
- constraint.insets = new Insets(0, 0, 40, 110);
- constraint.gridx = 0;
- constraint.gridy = 0;
- constraint.anchor = GridBagConstraints.CENTER;
- constraint.gridwidth = 1;
- constraint.gridheight = 1;
- pnlLogo.add(lblLogo, constraint);
- constraint.gridx = 0;
- constraint.gridy = 2;
- constraint.gridwidth = 1;
- constraint.insets = new Insets(0, 50, 40, 10);
- constraint.anchor = GridBagConstraints.LINE_START;
- frmLogin.add(lblUsername, constraint);
- constraint.gridy = 3;
- frmLogin.add(lblPassword, constraint);
- constraint.gridx = 2;
- constraint.gridy = 2;
- constraint.anchor = GridBagConstraints.CENTER;
- constraint.fill = GridBagConstraints.HORIZONTAL;
- constraint.weightx = 1;
- constraint.ipady = 10;
- constraint.insets = new Insets(0, 0, 40, 50);
- frmLogin.add(txtUsername, constraint);
- constraint.gridy = 3;
- frmLogin.add(txtPassword, constraint);
- constraint.gridx = 0;
- constraint.gridy = 4;
- constraint.ipady = 0;
- constraint.ipadx = 0;
- constraint.insets = new Insets(0, 0, 0, 0);
- constraint.fill = GridBagConstraints.HORIZONTAL;
- constraint.anchor = GridBagConstraints.CENTER;
- constraint.gridwidth = 0;
- frmLogin.add(pnlButton, constraint);
- constraint.gridx = 0;
- constraint.gridy = 1;
- constraint.insets = new Insets(0, 0, 0, 30);
- constraint.fill = GridBagConstraints.NONE;
- constraint.anchor = GridBagConstraints.CENTER;
- constraint.weightx = 0;
- constraint.weighty = 0;
- constraint.gridwidth = 1;
- constraint.ipadx = 60;
- constraint.ipady = 15;
- pnlButton.add(btnLogin, constraint);
- constraint.gridx = 1;
- constraint.gridy = 1;
- constraint.ipadx = 60;
- constraint.ipady = 15;
- pnlButton.add(btnClear, constraint);
- constraint.gridx = 2;
- constraint.gridy = 1;
- constraint.insets = new Insets(0, 0, 0, 0);
- constraint.ipadx = 60;
- constraint.ipady = 15;
- pnlButton.add(btnSignUp, constraint);
- frmLogin.setVisible(true);
- btnLogin.addActionListener(this);
- btnSignUp.addActionListener(this);
- btnClear.addActionListener(this);
- }
- private static Image getScaledImage(Image srcImg, int w, int h) {
- BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.BITMASK);
- Graphics2D g2 = resizedImg.createGraphics();
- g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
- g2.drawImage(srcImg, 0, 0, w, h, null);
- g2.dispose();
- return resizedImg;
- }
- public void actionPerformed(ActionEvent p){
- Object pressed = p.getSource();
- if (pressed == btnClear){
- txtUsername.setText("");
- txtPassword.setText("");
- txtUsername.requestFocus();
- }
- if (pressed == btnLogin){
- txtUsername.setText("");
- txtPassword.setText("");
- }
- if (pressed == btnSignUp){
- txtUsername.setText("");
- txtPassword.setText("");
- }
- File loginf = new File("C:\\Users\\User\\Desktop\\Java Programming\\BusTicketSellingSystem\\Registration.txt");
- try{
- Scanner read = new Scanner(loginf);
- read.useDelimiter(",");
- boolean login = false;
- while(read.nextLine() !=null){
- String user = read.next();
- String pass = read.next();
- read.next();
- if(txtUsername.getText().equals(user) && txtPassword.getText().equals(pass)){
- login = true;
- break;
- }
- }
- if(login)
- new FrmMainMenu();
- else {
- JOptionPane.showMessageDialog(null, "Incorrect username or password");
- txtUsername.setText("");
- txtPassword.setText("");
- }
- read.close();
- }
- catch (FileNotFoundException e){
- JOptionPane.showMessageDialog(null, "Can't find a text file");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement