我正在尝试从mssql获取一个值到一个类。在我的login gui框架中,变量传递完全正确,但是当我将那个类对象传递给另一个框架时,name(string ograd)变量返回null。
以下是teachermanagement.java文件:
package Model;
public class teachermanagement {
private int ogrID;
String ograd, ogrsoyad;
public teachermanagement(String ograd, String ogrsoyad) {
this.ograd = ograd;
this.ogrsoyad = ogrsoyad;
}
public int getogrID() {
return ogrID;
}
public void setogrID(int ogrID) {
this.ogrID = ogrID;
}
public String getograd() {
return ograd;
}
public void setograd(String ograd) {
this.ograd = ograd;
}
public String getogrsoyad() {
return ogrsoyad;
}
public void setogrsoyad(String ogrsoyad) {
this.ogrsoyad = ogrsoyad;
}
}
在logingui.java文件中,我通过单击按钮从mssql数据库中获取变量,效果很好。
package View;
import Helper.dbconnection;
import Model.teachermanagement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class logingui extends javax.swing.JFrame {
private dbconnection conn = new dbconnection();
public logingui() {
initComponents();
}
@SuppressWarnings("unchecked")
Generated Code
private void ogretmenadActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(ogretmenad.getText().length() == 0 | ogretmenpw.getText().length() == 0)
JOptionPane.showMessageDialog(null, "Lütfen tüm alanları doldurunuz.", "Uyarı", JOptionPane.INFORMATION_MESSAGE);
else {
try {
Connection con = conn.DBConn();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * from ogretmen");
while(rs.next()) {
if(ogretmenad.getText().equals(rs.getString("ograd")) && ogretmenpw.getText().equals(rs.getString("ogrsifre"))) {
teachermanagement tm = new teachermanagement();
tm.setogrID(rs.getInt("ogrID"));
tm.setograd(rs.getString("ograd"));
tm.setogrsoyad(rs.getString("ogrsoyad"));
//THIS IS WHERE I TRY TO PASS NAME VARIABLE TO ANOTHER FRAME
ogretmengui tgui = new ogretmengui(tm);
tgui.setVisible(true);
dispose();
}
}
} catch (SQLException ex) {
System.out.println(ex);
}
}
}
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(logingui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(logingui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(logingui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(logingui.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 logingui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextField ogretmenad;
private javax.swing.JPasswordField ogretmenpw;
// End of variables declaration
}
helper包中的dbconnection.java:
package Helper;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class dbconnection {
Connection c = null;
public dbconnection() {}
public Connection DBConn() {
try {
this.c = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; databaseName=schoolms", "sa", "1234");
return c;
} catch (SQLException ex) {
Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
}
return c;
}
}
这是我试图将变量传递给的teachergui框架:
package View;
import Model.teachermanagement;
public class ogretmengui extends javax.swing.JFrame {
static teachermanagement ogr = new teachermanagement();
public ogretmengui(teachermanagement ogr) {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("OKUL YÖNETİMİ SİSTEMİ");
setResizable(false);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
jLabel1.setText("Hoşgeldiniz, " + ogr.getograd());
jButton1.setText("Çıkış Yap");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(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()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(262, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
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(ogretmengui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ogretmengui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ogretmengui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ogretmengui.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() {
try {
ogretmengui frame = new ogretmengui(ogr);
frame.setVisible(true);
}
catch (Exception ex) {
System.out.println(ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration }
正如您在teachergui.java中看到的,我试图用从logingui框架中获取的变量更改标签文本,但它返回为null。当我尝试在logingui中打印变量时,它返回正确的值,但在teachergui框架中不是这样。任何帮助都将不胜感激。
ps:我隐藏了loginguiinnit组件来缩短代码,因为这个文件没有问题。
1条答案
按热度按时间j13ufse21#
创建太多teachermanagement对象并忽略传递到构造函数的参数:
代码1:
但忽略构造函数中的tm对象:
相反,不应在ogretmengui类中创建新的teachermanagement,而应使用参数设置字段:
您还可能遇到jframes不是模态的并且不阻塞程序流的问题(例如joptionpane或模态jdialog)。请看一下多个jframe的用法:好的还是坏的做法?。
另外,尽管与您的问题无关,但请考虑看一下这个java命名约定文档:类名应该以大写字母开头,变量应该以小写字母开头。同样,这与您的实际问题无关,但是通过遵循这些约定,您将使您的代码更易于其他人(我们)理解,这通常是一件好事。