将变量从Java类传递到另一个

时间:2017-06-04 12:36:09

标签: java swing

我试图从下面的Class中传递从名为username的JTextField获取的用户名变量,我尝试了很多东西并且一直都失败了:(

第一堂课:

    package com.cashdrw;
// GUI Login interface

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

public class ldapLoginPost implements ActionListener {
    public ldapLoginPost() {
    }

    JFrame frmCashdrawerTool;
    JPanel panel;
    JButton button;
    JTextField username;
    JPasswordField password;
    JLabel usernameLbl;
    JLabel passwordLbl;
    JLabel resultLbl;
    int result;
    private JLabel label;
    private JLabel label_1;
    private JLabel label_2;
    private JLabel lblNewLabel;

    public static void main (String[] args){
          ldapLoginPost gui = new ldapLoginPost();            

            gui.go();
        }
        public void go(){
            frmCashdrawerTool = new JFrame();
            frmCashdrawerTool.setTitle("Cashdrawer Tool");
            frmCashdrawerTool.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/photo.jpg")));
            frmCashdrawerTool.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frmCashdrawerTool.setResizable(false);         

            button = new JButton("Login");
            button.setFont(new Font("Tahoma", Font.BOLD, 14));
            button.setBounds(153, 165, 92, 25);
            button.addActionListener(this);
            username = new JTextField(20);
            username.setBounds(152, 95, 151, 22);
            username.setText("");
            password = new JPasswordField(20);
            password.setBounds(152, 130, 151, 22);
            password.setText("");
            resultLbl = new JLabel(" ");
            resultLbl.setFont(new Font("Tahoma", Font.PLAIN, 14));
            resultLbl.setBounds(129, 203, 174, 22);
            usernameLbl = new JLabel("Username");
            usernameLbl.setFont(new Font("Tahoma", Font.BOLD, 14));
            usernameLbl.setBounds(65, 97, 75, 16);
            passwordLbl = new JLabel("Password");
            passwordLbl.setFont(new Font("Tahoma", Font.BOLD, 14));
            passwordLbl.setBounds(65, 132, 75, 16);
            panel = new JPanel();
            panel.setBackground(Color.WHITE);
            frmCashdrawerTool.getContentPane().add(BorderLayout.CENTER, panel);
            panel.setLayout(null);
            panel.add(usernameLbl);
            panel.add(username);
            username.requestFocusInWindow();

            label = new JLabel("");
            label.setBounds(353, 16, 0, 0);
            panel.add(label);
            panel.add(passwordLbl);
            panel.add(password);

            label_1 = new JLabel("");
            label_1.setBounds(349, 43, 0, 0);
            panel.add(label_1);

            label_2 = new JLabel("");
            label_2.setBounds(354, 43, 0, 0);
            panel.add(label_2);
            panel.add(button);
            panel.add(resultLbl);

            lblNewLabel = new JLabel("");
            lblNewLabel.setIcon(new ImageIcon(getClass().getResource("/images/rsz_vf.jpg")));
            lblNewLabel.setBounds(0, 0, 391, 73);
            panel.add(lblNewLabel);
            frmCashdrawerTool.setSize(397,273);
            frmCashdrawerTool.setVisible(true);
        }


         public void actionPerformed(ActionEvent event) {
                char[] passwordVar = password.getPassword();
                String passString = new String (passwordVar);
                String currentUser = (username.getText());
            if (currentUser.isEmpty() || passString.isEmpty()) {
               result = 2;
            }else if (currentUser.equalsIgnoreCase("xxx")){
                String[] inputLine;
                inputLine = new String[2];
                inputLine[0] = currentUser;
                inputLine[1] = passString;
                ldapConnPost connect = new ldapConnPost();
                result = connect.setUpConnection(inputLine);
            }else {
                result = 3;
            }

             if (result ==1) {
                username.setText("");
                password.setText("");
                resultLbl.setForeground(Color.GREEN);
                resultLbl.setText("Authenticated Successfully");
                frmCashdrawerTool.setVisible(false);
                frmCashdrawerTool.dispose();
                String[] inputLine;
                inputLine = new String[1];
                inputLine[0] = "INFO  --- " + currentUser + "  Was Granted Access!";
                logger obj= new logger();
                obj.writer(inputLine);
                cashDrw gui = new cashDrw();            
                gui.go();
             }
             if (result == 2){
                 JOptionPane.showMessageDialog(null, "Please Enter Your Data!","ERROR",JOptionPane.ERROR_MESSAGE);
                 System.out.println("Connection Class not called");
             } 
             if (result == 3){
                 username.setText("");
                 password.setText("");
                 JOptionPane.showMessageDialog(null, "You Are not allowed to use this tool!","ERROR",JOptionPane.ERROR_MESSAGE); 
                 System.out.println("Connection Class not called");
                 String[] inputLine;
                 inputLine = new String[1];
                 inputLine[0] = "ERROR --- " + currentUser + "  Was Denied Access!";
                 logger obj= new logger();
                 obj.writer(inputLine);
             } 

             if (result == 0) {
                 JOptionPane.showMessageDialog(null, "Authentication Failed!","ERROR",JOptionPane.ERROR_MESSAGE); 
                 password.setText("");
                 String[] inputLine;
                 inputLine = new String[1];
                 inputLine[0] = "ERROR --- " + currentUser + "  LDAP Authentication Failed!";
                 logger obj= new logger();
                 obj.writer(inputLine);
         }
        }
}

到下面的课程,以便我可以在那里使用它:

package com.cashdrw;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class cashDrw extends JFrame implements ActionListener {
    public cashDrw() {
    }

    JFrame frmCashdrawerTool;
    JPanel panel;
    JButton btnUpdate;
    JTextField userid;
    JTextField storeid;
    JLabel useridLbl;
    JLabel storeidLbl;
    JLabel resultLbl;
    int cashdrwresult;
    private JLabel label;
    private JLabel label_1;
    private JLabel label_2;
    private JLabel lblNewLabel;

    public static void main (String[] args) {

       }
        public void go() {
            frmCashdrawerTool = new JFrame();
            frmCashdrawerTool.setTitle("Cashdrawer Tool");
            frmCashdrawerTool.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/photo.jpg")));
            frmCashdrawerTool.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frmCashdrawerTool.setResizable(false);

            btnUpdate = new JButton("Update");
            btnUpdate.setFont(new Font("Tahoma", Font.BOLD, 14));
            btnUpdate.setBounds(153, 165, 92, 25);
            btnUpdate.addActionListener(this);
            userid = new JTextField(20);
            userid.setBounds(152, 95, 151, 22);
            userid.setText("");
            storeid = new JTextField(20);
            storeid.setBounds(152, 130, 151, 22);
            storeid.setText("");
            resultLbl = new JLabel(" ");
            resultLbl.setFont(new Font("Tahoma", Font.PLAIN, 14));
            resultLbl.setBounds(129, 203, 174, 22);
            useridLbl = new JLabel("User ID");
            useridLbl.setFont(new Font("Tahoma", Font.BOLD, 14));
            useridLbl.setBounds(65, 97, 75, 16);
            storeidLbl = new JLabel("Store ID");
            storeidLbl.setFont(new Font("Tahoma", Font.BOLD, 14));
            storeidLbl.setBounds(65, 132, 75, 16);
            panel = new JPanel();
            panel.setBackground(Color.WHITE);
            frmCashdrawerTool.getContentPane().add(BorderLayout.CENTER, panel);
            panel.setLayout(null);
            panel.add(useridLbl);
            panel.add(userid);
            userid.requestFocusInWindow();

            label = new JLabel("");
            label.setBounds(353, 16, 0, 0);
            panel.add(label);
            panel.add(storeidLbl);
            panel.add(storeid);

            label_1 = new JLabel("");
            label_1.setBounds(349, 43, 0, 0);
            panel.add(label_1);

            label_2 = new JLabel("");
            label_2.setBounds(354, 43, 0, 0);
            panel.add(label_2);
            panel.add(btnUpdate);
            panel.add(resultLbl);
            frmCashdrawerTool.setSize(397,273);
            frmCashdrawerTool.setVisible(true);

            lblNewLabel = new JLabel("");
            lblNewLabel.setIcon(new ImageIcon(getClass().getResource("/images/rsz_vf.jpg")));
            lblNewLabel.setBounds(0, 0, 391, 73);
            panel.add(lblNewLabel);
            frmCashdrawerTool.setSize(397,273);
            frmCashdrawerTool.setVisible(true);

        }


         public void actionPerformed(ActionEvent event) {
                String currentStore = (storeid.getText());
                String currentUser = (userid.getText());
            if (currentUser.isEmpty() || currentStore.isEmpty()) {
                cashdrwresult = 2;
            }else{
              String[] inputLine;
              inputLine = new String[2];
              inputLine[0] = currentUser;
              inputLine[1] = currentStore;
              cashDrwsql join = new cashDrwsql();
              cashdrwresult = join.cashDrwAtion(inputLine);
            }
             if (cashdrwresult ==1) {
                 JOptionPane.showMessageDialog(null, "Action Was Performed Successfully","INFO",JOptionPane.PLAIN_MESSAGE);
                 userid.setText("");
                 storeid.setText("");
                 String[] inputLine;
                 inputLine = new String[1];
                 inputLine[0] = "INFO  --- Drawer: " + currentUser + " of Store: " + currentStore + " was unlocked!";
                 logger obj= new logger();
                 obj.writer(inputLine);
               //resultLbl.setForeground(Color.GREEN);
               //resultLbl.setText("Done!");

             }
             if (cashdrwresult == 2){
                 resultLbl.setForeground(Color.RED);
                // resultLbl.setText("Please Enter Required Data");
                 userid.setText("");
                 storeid.setText("");
                 JOptionPane.showMessageDialog(null, "Please Enter Required Data!","ERROR",JOptionPane.ERROR_MESSAGE);
              System.out.println("Connection Class not called");
              String[] inputLine;
              inputLine = new String[1];
              inputLine[0] = "ERROR  --- No Action Was taken, Please Enter Required Data!";
              logger obj= new logger();
              obj.writer(inputLine);
             } 

             if (cashdrwresult == 0) {
            //   resultLbl.setForeground(Color.RED);
            //  resultLbl.setText("Action Was Not Performed");
                 JOptionPane.showMessageDialog(null, "Action Was not performed, Refer Back to Support Team!","ERROR",JOptionPane.ERROR_MESSAGE);
                 userid.setText("");
                 storeid.setText("");
                 String[] inputLine;
                 inputLine = new String[1];
                 inputLine[0] = "ERROR  --- No Action Was taken for User: " + currentUser + " of Store: " + currentStore;
                 logger obj= new logger();
                 obj.writer(inputLine);
         }
        }
}

你能帮我知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

你需要去第二类“cashDrw”。创建一个名为username的JTextField变量。然后创建一个函数并将其称为“setUsername”。并将其公开,以便其他类可以访问它。并赋予功能一个参数。 JTextField用户名;例如。然后输入: this.username = username; 然后转到第一个类并调用“setUsername”函数。并将用户名传递给它!!