跨类的权限级别和控制访问

时间:2017-01-30 09:08:43

标签: java swing login static

我需要设计一个登录屏幕,在登录时为用户分配访问级别(privilegeLevel),以便他们可以访问或不访问整个程序中的按钮。

以下是登录屏幕代码:

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();

    }
}

以及适用的地方:

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();
}
}

在第二部分中第17行对非静态变量进行静态引用时出错,但我不相信这是主要问题。

0 个答案:

没有答案
相关问题