0

I need to design a Login Screen that assigns users a level of access (privilegeLevel) when they sign in, so that they can access or not access buttons across the program.

Here is the Login Screen code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;


public class LoginScreen extends JFrame implements ActionListener
{
    // Setting up the original login screen components like buttons
    JTextField username = new JTextField("");
    JPasswordField password = new JPasswordField("");
    JButton logIn = new JButton();

    // Background setup
    ImageIcon LoginScreenFile = new ImageIcon("LoginScreen.png");
    JLabel backgroundLoginScreen = new JLabel(LoginScreenFile);

    public int privilegeLevel;


    public LoginScreen()
    {
        // Adds the background
        this.add(backgroundLoginScreen);
        // Sets the close operation to stop the program
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // Gets the screen size
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        // Calculates where to put the window
        int xPos = (screenSize.width - 520)/2;
        int yPos = (screenSize.height - 400)/2;
        // Sets location at calculated position ^
        this.setLocation(xPos, yPos);
        // Sets the size of the window
        this.setSize(520,400);
        this.pack();

        // Sets all the components visibility as true
        username.setVisible(true);
        password.setVisible(true);
        logIn.setVisible(true);

        // Sets the bounds for the components 
        username.setBounds(252, 154, 132, 25);
        password.setBounds(252, 194, 132, 25);
        logIn.setBounds(399, 194, 56, 25);

        // Sets the text for the components
        logIn.setText("Log In");

        // Adds the components to the background window
        add(username);
        add(password);
        add(logIn);

        // no layout manager assists in the positioning 
        this.setLayout(null);
        // sets the label containing all of the buttons etc as visible
        this.setVisible(true);
        // Adds an action listener to the buttons so functions can be done when it is pressed
        logIn.addActionListener(LoginScreen.this);



    }
    public void actionPerformed(ActionEvent e)
    {

        // if the button is pressed
        if(e.getSource() == logIn)
        {
            // gets the string that is in the text boxes
            String str_username = username.getText();
            char[] char_password = password.getPassword();
            String str_password = String.valueOf(char_password);

            // if the logins are correct, assign a privilege level and send the user to the correct Main Menu
            if(str_username.equalsIgnoreCase("StudentA") && str_password.equalsIgnoreCase("password"))
            {
                final int privilegeLevel = 2;
                new StudentMainMenu();
            }
            else if(str_username.equalsIgnoreCase("StudentB") && str_password.equalsIgnoreCase("password"))
            {
                final int privilegeLevel = 1;
                new StudentMainMenu();
            }
            else if(str_username.equalsIgnoreCase("Admin") && str_password.equalsIgnoreCase("password123"))
            {
                final int privilegeLevel = 3;
                new StudentMainMenu();
            }
            // otherwise display a message popout that tells the user they have not entered correct data
            else
            {
                JOptionPane.showMessageDialog(null, "Error - Wrong username/password combination", "ERROR ON LOGIN", JOptionPane.ERROR_MESSAGE);
                this.dispose();
                new LoginScreen();
            }
        // an else here to "catch" the user - however there is no way for this to get triggered
        }
        else
        {
            System.out.println("error");
        }

    }

    public static void main(String[] args)
    {
        new LoginScreen();

    }
}

and where it applies:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;

public class StudentMainMenu extends JFrame implements ActionListener
{
    // Setting up the original login screen components like buttons
    JButton questionsAnswers = new JButton();
    JButton calculator = new JButton();
    JButton diffIntCalc = new JButton();
    JButton seriesCalc = new JButton();
    JButton help = new JButton();
    JButton logOut = new JButton();
    JButton adminAccessButton = new JButton();

    int privilegeLevel = LoginScreen.privilegeLevel;


    // Background setup
    ImageIcon StudentMainMenuFile = new ImageIcon("StudentMainMenuResize.png");
    JLabel backgroundStudentMainMenu = new JLabel(StudentMainMenuFile);

    public StudentMainMenu()
    {
        // Adds the background
        this.add(backgroundStudentMainMenu);
        // Sets the close operation to stop the program
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // Gets the screen size
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        // Calculates where to put the window
        int xPos = (screenSize.width - 1000)/2;
        int yPos = (screenSize.height - 750)/2;
        // Sets location at calculated position ^
        this.setLocation(xPos, yPos);
        // Sets the size of the window
        this.setSize(1000,750);
        this.pack();

        // using this for testing purposes to see what the program is using for privilegeLevel
        JOptionPane.showMessageDialog(null, "privilegeLevel = " + privilegeLevel, "pLevel", JOptionPane.INFORMATION_MESSAGE);

        adminAccessButton.setVisible(false);

        if(privilegeLevel == 3)
        {
            adminAccessButton.setVisible(true);
        }


        // Sets all the components visibility as true
        questionsAnswers.setVisible(true);
        calculator.setVisible(true);
        diffIntCalc.setVisible(true);
        seriesCalc.setVisible(true);
        help.setVisible(true);
        logOut.setVisible(true);


        // Sets the bounds for the components 
        questionsAnswers.setBounds(333, 255, 334, 38);
        calculator.setBounds(333, 303, 334, 37);
        diffIntCalc.setBounds(333, 350, 334, 37);
        seriesCalc.setBounds(333, 396, 334, 37);
        help.setBounds(333, 443, 334, 37);
        logOut.setBounds(505, 490, 162, 37);
        adminAccessButton.setBounds(333, 490, 162, 37);

        // Sets the text for the components
        questionsAnswers.setText("Questions w/ Answers");
        calculator.setText("Calculator");
        diffIntCalc.setText("Differentiation & Integration Calculator");
        seriesCalc.setText("Series Calculator");
        help.setText("Help");
        logOut.setText("Log Out");
        adminAccessButton.setText("Admin Access");

        // Adds the components to the background window
        add(questionsAnswers);
        add(calculator);
        add(diffIntCalc);
        add(seriesCalc);
        add(help);
        add(logOut);
        add(adminAccessButton);

        // no layout manager assists in the positioning 
        this.setLayout(null);
        // sets the window containing all of the buttons etc as visible
        this.setVisible(true);

        // Adds an action listener to the buttons so functions can be done when they are pressed
        questionsAnswers.addActionListener(StudentMainMenu.this);
        calculator.addActionListener(StudentMainMenu.this);
        diffIntCalc.addActionListener(StudentMainMenu.this);
        seriesCalc.addActionListener(StudentMainMenu.this);
        help.addActionListener(StudentMainMenu.this);
        logOut.addActionListener(StudentMainMenu.this);
        adminAccessButton.addActionListener(StudentMainMenu.this);


    }

    public void actionPerformed(ActionEvent e)
    {
        // If Question and Answers is pressed
        if(e.getSource() == questionsAnswers)
        {
            this.dispose();
            new QuestionsAnswersMainNew();
        }
        // If Calculator is pressed
        else if(e.getSource() == calculator)
        {
            this.dispose();
            //new Calculator();
        }
        // If Diff / Int Calculator is pressed
        else if(e.getSource() == diffIntCalc)
        {
            this.dispose();
            //new DiffIntChoice();
        }
        // If Series Calculator is pressed
        else if(e.getSource() == seriesCalc)
        {
            this.dispose();
            //new SeriesCalc();
        }
        // If is Help pressed
        else if(e.getSource() == help)
        {
            this.dispose();
            //new Help();
        }
        // If is Log Out is pressed
        else if(e.getSource() == logOut)
        {
            // Popout aka Option Pane asks if the is sure about logging out
            int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to Log Out?", "Log Out?", JOptionPane.YES_NO_CANCEL_OPTION);
            // If the user picks yes
            if(reply == JOptionPane.YES_OPTION)
            {
                // Close the Main Menu, then go back to the Log in Screen
                this.dispose();
                new LoginScreen();
            }
            // Else - if they pick no or close the option pane
            else
            {
                // Do Nothing
            }
        }
        // Just here if something weird happens and to ensure a crash doesn't happen
        else
        {
            System.out.println("error");
        }

    }


public static void main(String[] args)
{
    new StudentMainMenu();
}
}

There is an error of making a static reference to a non-static variable on line 17 in the second part, but I don't believe this is the main issue.

Dipiks
  • 3,818
  • 2
  • 23
  • 39
T24HG
  • 11
  • 1
  • 1
    Wish new close reason should be added: "OP doesn't know how to use Debugger" – Andremoniy Jan 30 '17 at 09:10
  • There are a couple of errors in your program, 1) regarding the "static reference": `int privilegeLevel = LoginScreen.privilegeLevel;` you shouldn't be accessing variables from one class to another in this way, use `getters` and `setters` for this purpose. 2) You're using a `null-layout`, while pixel perfect GUIs might seem like the best way to make complex GUIs the more problems related to this you'll find in the future and the more time you'll spend trying to fix them, please head toward the use of [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)... – Frakcool Jan 30 '17 at 14:05
  • ... because Swing was designed to work with different OS, platforms, screen sizes and resolutions, see [null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and the answers in [this question](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more information about this. 3) You're calling `setVisible` on each component, if you use a Layout manager then it's not needed. 4) You're calling `setSize()` and `pack()`, you should use one or the other, I recommend `pack()` but for it to work you need to use a Layout manager... – Frakcool Jan 30 '17 at 14:10
  • ... which you should be using anyway, if you want a fixed size window, then override the `getPreferredSize()` method of your parent component. 5) You're extending `JFrame` and you shouldn't be doing that, `JFrame` is a rigid container, you should make your GUI based on `JPanel`s instead. Also see [the use of multiple JFrames, Good / Bad practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice), the consensus says it's a bad practice for many reasons detailed in the answers of that question... – Frakcool Jan 30 '17 at 14:16
  • And 6) For your program I would use a [`Card-Layout`](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – Frakcool Jan 30 '17 at 14:22

0 Answers0