gpt4 book ai didi

java - 如何链接 2 个类以共享文本字段及其属性

转载 作者:行者123 更新时间:2023-11-30 07:19:53 27 4
gpt4 key购买 nike

我正在尝试在 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();
}
}
}

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

最佳答案

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

关于java - 如何链接 2 个类以共享文本字段及其属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14347230/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com