我正在尝试在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();
}
}
}
但主要问题是修改部分来自另一种形式..
答案 0 :(得分:3)
这是一个SSCCE,您可以测试,看看这是否是您想要做的。我首先创建您的登录JFrame,要求用户输入其凭据。然后将该信息传递给另一个类,我将Login类的整个实例传递给新类(OtherClass)。我在Login中有访问器方法,它将在OtherClass中用于检索已存储在字符串中的JTextField信息。如果您没有传递整个Login类,即使使用getText()将字符串存储在Strings中,也无法使用访问器方法访问信息。
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);
}
}
答案 1 :(得分:1)
如果您只有几个JTextFields要传递(如在登录案例中),您也可以将每个值作为字符串传递给新的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()获得的字符串值;检查仍将完成,但我认为最好通过检查过的字符串。