2015-12-21 32 views
0

我創建了兩個swing.JFrame s。 登錄GUI和用戶GUI。我要的是當它切換到登錄GUI用戶圖形界面,存在一種需要改變爲("you're logged in as" + username);在swing中聲明的運行方法.Jframe類

user JFrame的源代碼嘗試這種代碼的用戶界面一Jlabel

`loggedInAsLable.setText("you're logged in as" + username);` 
的方法

,這就是所謂的用戶jframe主要方法。但由於某些原因 它不起作用。

當Jframe變得可見時,我該如何運行一些方法?

public class CustomerServiceOfficerUI extends javax.swing.JFrame { 

private static Statement st; 
ResultSet rs; 
Connection con = null; 
Login loginUI = new Login(); // gets current user Id 
Employee cso = new CustomerServiceOfficer(); //creates new customer service officer object 

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

public void getCSOdetails() { 

    try { 
     Class.forName("com.mysql.jdbc.Driver"); 

     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/flyingcarsdb", "root", ""); 
     System.out.println("database connected"); 

    } catch (ClassNotFoundException | SQLException ex) { 
     System.out.println("Error: " + ex); 
    } 

    try { 
     // Retrieve customer service officer details 
     st = con.createStatement(); 
     String query = "select * FROM customerserviceofficer WHERE Id = '" + loginUI.getCurrentUserId() + "'"; 

     rs = st.executeQuery(query); 

     while (rs.next()) { 
      //Assign the details with setters 
      cso.setFname(rs.getString("Fname")); 
      cso.setEmail(rs.getString("Email")); 
     } 

    } catch (Exception ex) { 
     System.out.println("Error : " + ex); 
    } 
loggedInAsLable.setText("you're logged in as : " + cso.getId()); 
//this is where LABLE is changed, 'cso.getId()' returns the user ID 
    } 
+0

顯示您的代碼,在其中更改標籤 –

+0

首先,請勿爲此使用框架,請在您的登錄視圖中使用對話框。當用戶通過身份驗證並關閉對話框時,它應該返回調用者的會話信息。這是[Model-View-Controller]的基礎(http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) – MadProgrammer

+0

我已經添加了代碼。標籤在一種方法中被破壞。 'getCSOdetails()',並在'CustomerServiceOfficerUI'類的主方法中調用該方法 –

回答

1

如果你真的需要變爲可見時更新您的JFrame(如你的最後一句話建議),則可以使用的WindowListener的打電話給你getCSODetails()方法。我已經包含了三個激活事件 - 打開,激活和去鈍化;您可以刪除其中的任何一項,以便將更新限制爲適合您需求的特定事件。如果您只需在窗口打開後更新標籤,請刪除方法windowDeiconified()windowActivated()。 但是,請注意,getCSODetails()方法設計得相當糟糕,每當窗口變得可見/重點時調用它都會導致性能損失,並且GUI的響應性將受到數據庫性能的嚴重影響。我猜你所顯示的客戶信息在登錄會話期間沒有改變,所以最好執行一次查詢,緩存詳細信息,然後從緩存中顯示出來。

+0

謝謝。它在我刪除'windowDeiconified()'和'windowActivated()'方法時起作用。它有一點延遲來改變標記(1秒),但沒關係。我正在努力改進代碼,感謝您的指導。 –

0

試試這個:

public class CustomerServiceOfficerUI extends javax.swing.JFrame { 

    private static Statement st; 
    ResultSet rs; 
    Connection con = null; 
    Login loginUI = new Login(); // gets current user Id 
    Employee cso = new CustomerServiceOfficer(); //creates new customer service officer object 

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

    public void getCSOdetails() { 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 

      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/flyingcarsdb", "root", ""); 
      System.out.println("database connected"); 




      // Retrieve customer service officer details 
      st = con.createStatement(); 
      String query = "select * FROM customerserviceofficer WHERE Id = '" + loginUI.getCurrentUserId() + "'"; 

      rs = st.executeQuery(query); 

      while (rs.next()) { 
       //Assign the details with setters 
       cso.setFname(rs.getString("Fname")); 
       cso.setEmail(rs.getString("Email")); 
      } 

SwingUtilities.invokeLater(new Runnable() 
     { 

      public void run() 
      { 
       loggedInAsLable.setText("you're logged in as : " + cso.getId()); 
       loggedInAsLable.repaint(); 

      } 
     }); 

     } catch (Throwable ex) { 
      System.out.println("Error : " + ex); 
SwingUtilities.invokeLater(new Runnable() 
     { 

      public void run() 
      { 
       loggedInAsLable.setText("There is a problem with your code : " + ex); 
       loggedInAsLable.repaint(); 

      } 
     }); 

     } finally { 

     } 
    //this is where LABLE is changed, 'cso.getId()' returns the user ID 
     } 
+1

不需要「重繪」。 'invokeLater'應該足夠了。 –