gpt4 book ai didi

java - JAVA中访问另一个类变量

转载 作者:行者123 更新时间:2023-12-01 08:07:03 25 4
gpt4 key购买 nike

我的应用程序有以下登录屏幕; (我使用netbeans gui设计器)

public class loginscreen extends javax.swing.JFrame {

/**
* Creates new form loginscreen
*/
public loginscreen() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

Uname_Textfield = new javax.swing.JTextField();
Password_PasswordField = new javax.swing.JPasswordField();
Bağlan_Buton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);

Uname_Textfield.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Uname_TextfieldActionPerformed(evt);
}
});

Bağlan_Buton.setText("Bağlan");
Bağlan_Buton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Bağlan_ButonActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(Uname_Textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(Password_PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(Bağlan_Buton)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(11, 11, 11)
.addComponent(Uname_Textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Password_PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Bağlan_Buton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private void Uname_TextfieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void Bağlan_ButonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String user=Uname_Textfield.getText();
String pwd= new String (Password_PasswordField.getPassword());
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://192.168.131.10;" + "databaseName=ExampleDB;" + "user=" + user + ";" + "password=" + pwd + ";";
Connection con = DriverManager.getConnection(connectionUrl);
new ProgramPenceresi().setVisible(true);
dispose();
}
catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Kullanıcı Adı veya Şifre Yanlış!");
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(loginscreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(loginscreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(loginscreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(loginscreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new loginscreen().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton Bağlan_Buton;
private javax.swing.JPasswordField Password_PasswordField;
private javax.swing.JTextField Uname_Textfield;
// End of variables declaration
}

我使用此登录屏幕来识别数据库中的用户。我有另一个 SQLSP 类用于执行数据库中的存储过程。

如果用户使用真实的登录详细信息成功登录系统,屏幕上会出现另一个 java 框架。当用户与此屏幕交互并从 SQLSP 类调用一些 sqlsp 时,我必须使用 loginscreen.java 中的 uname 和密码登录到数据库

但是uname和密码存储在

private void Bağlan_ButonActionPerformed(java.awt.event.ActionEvent evt)

公共(public)类loginscreen扩展了javax.swing.JFrame

如何从另一个类访问 user 和 pwd 变量?

我试试

loginscreen logindetails = new loginscreen();
String username = logindetails.user;

但是没有帮助。我如何访问它们?

最佳答案

user 是类方法内的局部变量。局部变量不能在声明它们的方法之外的任何地方访问。

你真正想做的是在类中声明一个类变量:

private String username;

然后为该变量提供一个公共(public) getter:

public String getUsername()
{
return username;
}

然后在类中的适当位置设置用户名。类外部的代码可以使用 logindetails.getUsername() 来访问存储的用户名。

关于java - JAVA中访问另一个类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20751091/

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