java将值从jframe传递给另一个jframe并使用它
##这是登录框架的代码##
public class Login_frame extends javax.swing.JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
public Login_frame() {
initComponents();
conn = javaconnect.ConnecrDB();
}
private void cmd_loginActionPerformed(java.awt.event.ActionEvent evt) {
String sql ="select * from userinfo where username =? and password =? ";
try{
pst = conn.prepareStatement(sql);
pst.setString(1,txt_username.getText());
pst.setString(2,txt_password.getText());
rs = pst.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "Username and Password is correct");
rs.close();
pst.close();
close();
Welcome_Screen w = new Welcome_Screen();
w.userName = this.txt_username.getText();
w.setVisible(true);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Invalid username or password");
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
}
private void txt_usernameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.setVisible(false);
Register_frame r = new Register_frame();
r.setVisible(true);
}
public void close(){
WindowEvent winClosingEvent= new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
}
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(Login_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login_frame.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 Login_frame().setVisible(true);
}
});
}
}
这是第二个Jframe的代码
public class Welcome_Screen extends javax.swing.JFrame {
static Object txt_username;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
String username="";
/**
* Creates new form Welcome_Screen
*/
public Welcome_Screen() {
initComponents();
conn = javaconnect.ConnecrDB();
Update_table();
Update_table2();
update_elements();
}
private void update_elements(){
try{
String sql ="select league from Teams where team_owner= '"+username+"' ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next())
{
String result =rs.getString("league");
league_txt.setText(result);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
}
private void Update_table2(){
try{
String sql ="select Player,Pos,Age from user_team ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
myTeam.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
}
private void Update_table(){
try{
String sql ="select * from Teams ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
league_table.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
pst.close();
}catch(Exception e){
}
}
}
`I have a problem regarding on how to pass value from one `JFrame` to another and use it to an `sql` query.
让我们更清楚一点。我想制作一个必须JFrame
的程序。第一个是登录框架。我想获取用户在用户名文本字段中输入的值,并使用它加载一个表,该表对于每个用户都是唯一的
更简单地说,我想替换这个查询中的userName
String sql ="select league from Teams where team_owner= '"+userName+"' ";
# 1 楼答案
你不能通过,因为你没有把它存放在任何地方。你也应该 完成此方法正文:
然后你可以在任何你想要的地方传递用户名
更好的是,您根本不需要使用listener,也不需要在类级别声明txt_用户名。然后,只要调用txt_用户名,就可以随时获得用户输入。getText()
您的代码看起来是由某个GUI工具生成的。我建议你从手写开始,直到你完全知道生成了什么
第二个答复
看来你改变了密码。我看不到任何组件();不再是方法了。我想你需要把它放回去。您声明(或netbeans为您做的)JtextField txt_username=new。。。用那种方法。如果将其移动到LoginFrame类的顶部,即连接和结果集所在的位置,则可以在Login_frame中的任何位置使用它,而不仅仅是在initComponents()中。顺便说一句,在java中,应该像LoginFrame那样命名类(不要使用underbar,但这只是惯例)
# 2 楼答案
如果有任何困惑,请告诉我。基本上,当你创建一个新的
Welcome_Screen
实例时,你的构造器如下所示update_elements();
被称为。。。然后你的userName
在你创建新框架的地方被赋予了它的值。这只是一个订单问题。不过你做的一切都对现在,每当使用
Welcome_Screen
进行new
调用时,构造函数上都有一个参数,您需要向它传递一个您选择的字符串。然而,在这一点上,这不是你在这个项目中的选择。你必须这样打电话才能让它工作