[英]passing a value from a jframe and use it to another
##這是loginframe的代碼##
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);
}
});
}
}
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+"' ";
讓我知道這是否令人困惑。 基本上發生的是當您創建一個Welcome_Screen
的新實例時,您的構造函數如下所示。 update_elements();
被稱為...然后在創建新框架的地方給了您的userName
值。 這只是訂單的問題。 你雖然做得對。
String username="";
/**
* Creates new form Welcome_Screen
*/
public Welcome_Screen(String userName) {
initComponents();
conn = javaconnect.ConnecrDB();
//this. keyword is very important otherwise the
//local variable won't set the frame variable.
this.userName = userName;
Update_table();
Update_table2();
update_elements();
}
同樣,現在只要在使用Welcome_Screen
進行new
調用時,構造函數上就有一個參數,就需要將其傳遞給您選擇的字符串。 但是,這不是您目前在此程序中的選擇。 您必須像這樣撥打電話才能正常工作。
//I lied either should be fine.
Welcome_Screen w = new Welcome_Screen(userName);
Welcome_Screen w = new Welcome_Screen(yourTextField.getText());
您無法通過它,因為您不會將其存儲在任何地方。 您應該完成以下方法體:
String username;
private void txt_usernameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
username = ((JTextField)evt.getSource()).getText();
}
然后,您可以在任意位置傳遞用戶名。
更好的是,您根本不需要使用偵聽器,而在類級別上聲明txt_username。 然后,您隨時可以通過調用txt_username.getText()獲得用戶輸入。
您的代碼看起來是由某些GUI工具生成的。 我建議從手寫開始,直到您確切生成了什么為止。
好吧,看來您更改了代碼。 我看不到initComponents(); 方法了。 我認為您需要將其放回原處。 您在該方法中聲明了JtextField txt_username = new ...(或netbeans為您完成了此操作)。 如果將其移至LoginFrame類的頂部,即連接和結果集的正確位置,則可以在Login_frame內部的任何位置(不僅在initComponents()中)使用此方法。 BTW。 在Java中,您應該命名類似LoginFrame的類(不要使用下划線,但這僅是約定)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.