2

我正在尝试在 Java Netbeans IDE 中为我的桌面应用程序运行数据代码的修改,但问题是在某人有权修改数据之前,我想通过在另一个中提供用户名和密码来确保该人已获得授权JFrame 形式。如果验证了用户名和密码,那么只有修改任务才会成功;否则用户将收到身份验证不成功的错误。我试图在我的主表单中编写代码,但是当我想在身份验证表单的 button_click 事件中编写代码时,可以吗?我试过了,但它说你需要在这个类中声明那个类的变量,请帮忙..??

这是登录表单,我想验证用户是否在验证用户后修改数据我想允许他运行修改部分....

  private void loginActionPerformed(java.awt.event.ActionEvent evt) {
   if(evt.getSource()==login){
       if((tb_uid.getText().equals(""))||(tb_pwd.getText().equals(""))){
           JOptionPane.showMessageDialog(rootPane, "Please Enter Valid UserName and Password!");

       }else{
            DBUtil util = new DBUtil();
            String user1 = "";
            String pass1 = "";
            try {

                Connection con = util.getConnection();
                PreparedStatement stmt = con.prepareStatement("SELECT * FROM login where username=?");
                ResultSet res;
                String value1 = tb_uid.getText();
                String value2 = tb_pwd.getText();
                stmt.setString(1, "" + (value1));
                res = stmt.executeQuery();
                while (res.next()) {
                    user1 = res.getString("username");
                    pass1 = res.getString("password");
                }
                if (value1.equals(user1) && value2.equals(pass1)) {
                    JOptionPane.showMessageDialog(null, "AUTHENTICATION SUCCESSFUL!");



                     try {
                Connection con = util.getConnection();
                PreparedStatement stmtn = con.prepareStatement("update soil_det set[weight]=?,[note_state]=?,[dm_state]=?,[1]=?,[2]=?,[5]=?,[10]=?,[20]=?,[50]=?,[100]=?,[500]=?,[1000]=? FROM [CNV].[dbo].[soil_det] where rm_id=? and box_no =?");


                String rmn = (tf_rm_id.getText() == null || tf_rm_id.getText().equals("")) ? "0" : tf_rm_id.getText();
                String an = (txtRe1.getText().trim() == null || txtRe1.getText().equals("")) ? "0" : txtRe1.getText();
                String bn = (txtRs2.getText().trim() == null || txtRs2.getText().equals("")) ? "0" : txtRs2.getText();
                String cn = (txtRs5.getText().trim() == null || txtRs5.getText().equals("")) ? "0" : txtRs5.getText();
                String dn = (txtRs10.getText().trim() == null || txtRs10.getText().equals("")) ? "0" : txtRs10.getText();
                String en = (txtRs20.getText().trim() == null || txtRs20.getText().equals("")) ? "0" : txtRs20.getText();
                String fn = (txtRs50.getText().trim() == null || txtRs50.getText().equals("")) ? "0" : txtRs50.getText();
                String gn = (txtRs100.getText().trim() == null || txtRs100.getText().equals("")) ? "0" : txtRs100.getText();
                String hn = (txtRs500.getText().trim() == null || txtRs500.getText().equals("")) ? "0" : txtRs500.getText();
                String in = (txtRs1000.getText().trim() == null || txtRs1000.getText().equals("")) ? "0" : txtRs1000.getText();
                String bnn = (txtboxno.getText().trim() == null || txtboxno.getText().equals("")) ? "0" : txtboxno.getText();
                String bwn = (txtboxwgt.getText().trim() == null || txtboxwgt.getText().equals("")) ? "0" : txtboxwgt.getText();
                Object nsn = (cbnotstat.getSelectedItem() == null || cbnotstat.getSelectedItem().equals("")) ? "0" : cbnotstat.getSelectedItem();
                Object dsn = (cbdmnstat.getSelectedItem() == null || cbdmnstat.getSelectedItem().equals("")) ? "0" : cbdmnstat.getSelectedItem();


                stmtn.setString(1, "" + (bwn));
                stmtn.setString(2, "" + nsn);
                stmtn.setString(3, "" + dsn);
                stmtn.setInt(4, Integer.parseInt(an));
                stmtn.setInt(5, Integer.parseInt(bn));
                stmtn.setInt(6, Integer.parseInt(cn));
                stmtn.setInt(7, Integer.parseInt(dn));
                stmtn.setInt(8, Integer.parseInt(en));
                stmtn.setInt(9, Integer.parseInt(fn));
                stmtn.setInt(10, Integer.parseInt(gn));
                stmtn.setInt(11, Integer.parseInt(hn));
                stmtn.setInt(12, Integer.parseInt(in));
                stmtn.setString(13, "" + (rmn));
                stmtn.setInt(14, Integer.parseInt(bnn));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }


                        } 
                else {
                    JOptionPane.showMessageDialog(this, "AUTHERNTICATION UNSUCCESSFUL", "Error", JOptionPane.ERROR_MESSAGE);
                }

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());

    }
    this.dispose();
    }
   }
}

但主要问题是来自另一种形式的修改部分..

4

2 回答 2

3

这是一个 SSCCE,您可以对其进行测试,看看这是否是您想要做的。我首先创建您的登录 JFrame,要求用户输入他们的凭据。然后将该信息传递给另一个类,我将 Login 类的整个实例传递给新类(OtherClass)。我在 Login 中有访问器方法,这些方法将在 OtherClass 中用于检索已存储在字符串中的 JTextField 信息。如果您不传递整个 Login 类,即使您使用 getText() 将 JTextField 内容存储在字符串中,也无法使用访问器方法访问该信息。

import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;

public class Login extends JFrame
{
    private static final int FRAME_WIDTH = 250;
    private static final int FRAME_HEIGHT = 100;

    private JPanel labelPanel;
    private JPanel textPanel;

    private JTextField userName;
    private JTextField userPassword;

    private JButton loginButton;

    private String userNameString;
    private String userPasswordString;

    public Login()
    {
        super("Please Login");
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        labelPanel = new JPanel();
        labelPanel.setLayout(new GridLayout(2, 1));
        add(labelPanel, BorderLayout.WEST);
        JLabel userLabel = new JLabel("User Name: ");
        labelPanel.add(userLabel);
        JLabel passwordLabel = new JLabel("Password: ");
        labelPanel.add(passwordLabel);

        textPanel = new JPanel();
        textPanel.setLayout(new GridLayout(2, 1));
        add(textPanel, BorderLayout.CENTER);
        userName = new JTextField(30);
        userPassword = new JTextField(30);
        textPanel.add(userName);
        textPanel.add(userPassword);

        loginButton = new JButton("Login");
        FrameListener loginListener = new FrameListener();
        loginButton.addActionListener(loginListener);
        add(loginButton, BorderLayout.SOUTH);        
    }

    private class FrameListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            String actionCommand = e.getActionCommand();

            if(actionCommand.equals("Login"))
            {
                if(!(userName.getText().equals("")) && !(userPassword.getText().equals("")))
                {
                    userNameString = userName.getText();
                    userPasswordString = userPassword.getText();

                    //Access your database of credentials here


                    SwingUtilities.invokeLater(new Runnable() 
                    {
                        @Override
                        public void run() 
                        {
                            OtherClass mainProgram = new OtherClass(Login.this);
                            mainProgram.setVisible(true);
                        }
                    });
                    dispose();
                }
            }
        }
    }

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            @Override
            public void run() 
            {
                Login loginFrame = new Login();
                loginFrame.setVisible(true);
            }
        });
    }

    public String getUserName()
    { 
        return userNameString;
    }

    public String getUserPassword()
    {
        return userPasswordString;
    }
}

获取此信息的类需要:

import javax.swing.JFrame;

public class OtherClass extends JFrame
{
    final Login loginClass;

    private String userName;
    private String userPassword;

    public OtherClass(Login e)
    {
        loginClass = e;
        userName = loginClass.getUserName();
        userPassword = loginClass.getUserPassword();

        System.out.println("Your User Name is: " + userName);
        System.out.println("Your Password is: " + userPassword);
    }   
}
于 2013-01-16T05:02:26.580 回答
1

如果您只有几个 JTextField 可以传递(如在您的登录情况下),您也可以将每个值作为字符串传递给新的 JFrame 类的构造函数。这使得您不必使用访问器方法。如果你有很多字段要传递,它也会使构造函数的参数变得非常混乱。

import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;

public class Login extends JFrame
{
    private static final int FRAME_WIDTH = 250;
    private static final int FRAME_HEIGHT = 100;

    private JPanel labelPanel;
    private JPanel textPanel;

    private JTextField userName;
    private JTextField userPassword;

    private JButton loginButton;

    private String userNameString;
    private String userPasswordString;

    public Login()
    {
        super("Please Login");
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        labelPanel = new JPanel();
        labelPanel.setLayout(new GridLayout(2, 1));
        add(labelPanel, BorderLayout.WEST);
        JLabel userLabel = new JLabel("User Name: ");
        labelPanel.add(userLabel);
        JLabel passwordLabel = new JLabel("Password: ");
        labelPanel.add(passwordLabel);

        textPanel = new JPanel();
        textPanel.setLayout(new GridLayout(2, 1));
        add(textPanel, BorderLayout.CENTER);
        userName = new JTextField(30);
        userPassword = new JTextField(30);
        textPanel.add(userName);
        textPanel.add(userPassword);

        loginButton = new JButton("Login");
        FrameListener loginListener = new FrameListener();
        loginButton.addActionListener(loginListener);
        add(loginButton, BorderLayout.SOUTH);        
    }

    private class FrameListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            String actionCommand = e.getActionCommand();

            if(actionCommand.equals("Login"))
            {
                if(!(userName.getText().equals("")) && !(userPassword.getText().equals("")))
                {
                    userNameString = userName.getText();
                    userPasswordString = userPassword.getText();

                    //Access your database of credentials here

                    SwingUtilities.invokeLater(new Runnable() 
                    {
                        @Override
                        public void run() 
                        {
                            OtherClass mainProgram = new OtherClass(userNameString, userPasswordString); //Here we pass the String values instead of the whole class
                            mainProgram.setVisible(true);
                        }
                    });
                    dispose();
                }
            }
        }
    }

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            @Override
            public void run() 
            {
                Login loginFrame = new Login();
                loginFrame.setVisible(true);
            }
        });
    }
    //Note we no longer need accessor methods
}

而现在在构造函数中具有 String 参数的 OtherClass :

import javax.swing.JFrame;

public class OtherClass extends JFrame
{
    private String userName;
    private String userPassword;

    public OtherClass(String n, String p)
    {
        userName = n;
        userPassword = p;

        System.out.println("Your User Name is: " + userName);
        System.out.println("Your Password is: " + userPassword);
    }   
}

请记住,最好传递通过 if-else 控制结构的字符串值,而不是传递使用 userName.getText(); 获得的字符串值;检查仍将完成,但我认为传递检查的字符串是一个更好的主意。

于 2013-01-16T18:53:58.973 回答