Hi there!
I'm a beginner in Java. I've created 2 class files: 1) TestAssign.java 2) NewFrame.java How can I have different user to login? Now that I've only administrator who is able to login. But, I would like to have other user to login too such as end-user and general manager. How/where should I code it?
TestAssign.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestAssign extends JFrame{ public static void main(String[] args) { TestAssign frameTabel = new TestAssign(); } JButton blogin = new JButton("Login"); JPanel panel = new JPanel(); JLabel username = new JLabel("Username:"); JLabel password = new JLabel("Password:"); JTextField txtadmin = new JTextField(15); JPasswordField pass = new JPasswordField(15); TestAssign(){ super("Login Authentication"); setSize(400,300); setLocation(500,280); setResizable(false); panel.setLayout(null); username.setBounds(80, 65, 100, 20); password.setBounds(80, 110, 100, 20); txtadmin.setBounds(155, 65, 150, 20); pass.setBounds(155, 110, 150, 20); blogin.setBounds(160, 180, 80, 20); panel.add(blogin); panel.add(username); panel.add(password); panel.add(txtadmin); panel.add(pass); getContentPane().add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); actionlogin(); } public void actionlogin(){ blogin.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String paname = txtadmin.getText(); String ppaswd = pass.getText(); if(paname.equals("admin") && ppaswd.equals("1234")){ NewFrame regFace = new NewFrame(); regFace.setVisible(true); dispose(); }else{ JOptionPane.showMessageDialog(null, "Invalid Password / " + "Username"); txtadmin.setText(""); pass.setText(""); txtadmin.requestFocus(); } } }); } }
NewFrame.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class NewFrame extends JFrame{ public static void main(String[] args){ NewFrame frameTabel = new NewFrame(); } JLabel welcome = new JLabel("******Welcome to MYSOFT Sdn Bhd!******", SwingConstants.CENTER); JLabel dms = new JLabel("Delivery Management System"); JPanel panel = new JPanel(); NewFrame(){ super("Welcome!"); setSize(400,300); setLocation(500,280); setResizable(false); panel.setLayout(null); welcome.setBounds(60, 70, 300, 60); dms.setBounds(125, 100, 300, 60); panel.add(welcome); panel.add(dms); getContentPane().add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } }
Thanks in advance.
I just found out the solution. In the TestAssign.java, I'll need else-if statement and create a new frame for both of the manager and staff. For example, ManagerFrame and StaffFrame.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestAssign extends JFrame{ public static void main(String[] args) { TestAssign frameTabel = new TestAssign(); } JButton blogin = new JButton("Login"); JPanel panel = new JPanel(); JLabel username = new JLabel("Username:"); JLabel password = new JLabel("Password:"); JTextField txtuser = new JTextField(15); JPasswordField pass = new JPasswordField(15); TestAssign(){ super("Login Authentication"); setSize(400,300); setLocation(500,280); setResizable(false); panel.setLayout(null); username.setBounds(80, 65, 100, 20); password.setBounds(80, 110, 100, 20); txtuser.setBounds(155, 65, 150, 20); pass.setBounds(155, 110, 150, 20); blogin.setBounds(160, 180, 80, 20); panel.add(blogin); panel.add(username); panel.add(password); panel.add(txtuser); panel.add(pass); getContentPane().add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); actionlogin(); } public void actionlogin(){ blogin.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String paname = txtuser.getText(); String ppaswd = pass.getText(); if(paname.equals("admin") && ppaswd.equals("1234")){ NewFrame regFace = new NewFrame(); regFace.setVisible(true); dispose(); } else if(paname.equals("manager") && ppaswd.equals("4567")) { ManagerFrame manager = new ManagerFrame(); manager.setVisible(true); dispose(); } else if(paname.equals("staff") && ppaswd.equals("7890")) { EndUserFrame staff = new EndUserFrame(); staff.setVisible(true); dispose(); } else{ JOptionPane.showMessageDialog(null, "Invalid Password / " + "Username"); txtuser.setText(""); pass.setText(""); txtuser.requestFocus(); } } }); } }