2014-06-24 64 views
1

我有一個JFrame窗體類這樣如何在Jframe窗體類中調用另一個類?

public class LoginForm extends javax.swing.JFrame 

在此,我得到用戶的用戶名&密碼,然後將其發送到PHP服務器的驗證和 將得到響應,確定或無效的用戶。我有另一個類'公共類LoginTimer實現Runnable'。在這個類中,我有一些代碼要執行。我希望在'登錄表格'當我得到響應,確定後,控件將移動到第二類'LoginTimer'表示第二類將被調用
。請告訴我怎麼做?
================================================ =====================

private void sendGet(String username,String pwd) throws Exception 
{ 
     String url = "http://localhost/login.php?username="+username+ "&password="+pwd; 
     final String USER_AGENT = "Mozilla/5.0"; 
     URL obj = new URL(url); 
     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
     con.setRequestMethod("GET"); 
     con.setRequestProperty("User-Agent", USER_AGENT); 
     int responseCode = con.getResponseCode(); 
     System.out.println("\nSending 'GET' request to URL : " + url); 
     System.out.println("Response Code : " + responseCode); 
     BufferedReader in = new BufferedReader(
     new InputStreamReader(con.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 
     while ((inputLine = in.readLine()) != null) 
     { 
     response.append(inputLine); 
     } 
     in.close(); 
    //print result 
     String r=response.toString(); 
     System.out.println("String "+r); 
     if(r.equals("OK")) 
     { 
      System.out.println("you are a valid user"); 

     } 
     else 
     { 
      System.out.println("You are an invalid user"); 
     } 


} 

下面是我爲LoginTimer類代碼。在這裏,我得到可見窗口的名稱,然後線程啓動,並在運行()方法我打電話sendGet()方法發送窗口名稱到php服務器頁面。我希望當我在LoginForm類中得到OK響應時,LoginTimer類將被自動調用並執行。我的意思是,當用戶登錄&驗證後,窗口名稱將自動啓動。

public class LoginTimer implements Runnable 
{ 
LoginTimer lk1; 
String s3; 
static int arraySize=10; 
static int arrayGrowth=2; 
static String[] m=new String[arraySize]; 
static int count=0; 

    @Override 
    public void run() 
    { 
     for(int ck=0;ck<3;ck++) 
     {File file=new File("G:\\check.txt"); 
     Scanner scanner = null; 
     try 
     { 
     scanner = new Scanner(file); 
     } 
     catch (FileNotFoundException ex) 
     { 
     Logger.getLogger(LoginTimer.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     while(scanner.hasNext()) 
     { 
     String[] tokens = scanner.nextLine().split(":"); 
     String last = tokens[1]; 
     // System.out.println(last); 
     if(last!=null) 
     { 
      try 
      { 
       lk1.sendGet(last,m); 

      } 
      catch (Exception ex) 
      { 
       Logger.getLogger(LoginTimer.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
     } 
     try { 
      Thread.sleep(5000); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(LoginTimer.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     } 

    } 

public static void main(String[] args) 
{ 

    (new Thread(new LoginTimer())).start(); 
    final List<WindowInfo> inflList=new ArrayList<WindowInfo>(); 
    final List<Integer> order=new ArrayList<Integer>(); 
    int top = User32.instance.GetTopWindow(0); 
    while (top!=0) 
    { 
     order.add(top); 
     top = User32.instance.GetWindow(top, User32.GW_HWNDNEXT); 
    } 
    User32.instance.EnumWindows(new WndEnumProc() 
    { 
    public boolean callback(int hWnd, int lParam) 
    { 
     if (User32.instance.IsWindowVisible(hWnd)) 
     { 
     RECT r = new RECT(); 
     User32.instance.GetWindowRect(hWnd, r); 
     if (r.left>-32000) 
     {  // minimized 
      byte[] buffer = new byte[1024]; 
      User32.instance.GetWindowTextA(hWnd, buffer, buffer.length); 
      String title = Native.toString(buffer); 
      //lk1.getid(title); 
      if (m.length == count) 
     { 
       // expand list 
      m = Arrays.copyOf(m, m.length + arrayGrowth); 
      } 
      m[count]=Native.toString(buffer); 
      System.out.println("title===="+m[count]); 
      count++; 

      inflList.add(new WindowInfo(hWnd, r, title)); 
     } 

     } 
     return true; 
    } 
    }, 0); 

Collections.sort(inflList, new Comparator<WindowInfo>() 
{ 
    public int compare(WindowInfo o1, WindowInfo o2) 
    { 
    return order.indexOf(o1.hwnd)-order.indexOf(o2.hwnd); 
    } 
}); 
    for (WindowInfo w : inflList) 
    { 
    System.out.println(w); 
    } 


} 


public static interface WndEnumProc extends StdCallLibrary.StdCallCallback 
{ 
    boolean callback (int hWnd, int lParam); 
} 

public static interface User32 extends StdCallLibrary 
{ 
    final User32 instance = (User32) Native.loadLibrary ("user32", User32.class); 
    boolean EnumWindows (WndEnumProc wndenumproc, int lParam); 
    boolean IsWindowVisible(int hWnd); 
    int GetWindowRect(int hWnd, RECT r); 
    void GetWindowTextA(int hWnd, byte[] buffer, int buflen); 
    int GetTopWindow(int hWnd); 
    int GetWindow(int hWnd, int flag); 
    final int GW_HWNDNEXT = 2; 

} 
public static class RECT extends Structure 
{ 
    public int left,top,right,bottom; 
} 
public static class WindowInfo 
{ 
    int hwnd; 
    RECT rect; 
    String title; 
    public WindowInfo(int hwnd, RECT rect, String title) 
    { 
     this.hwnd = hwnd; this.rect = rect; this.title = title; 
    } 
    public String toString() 
    { 
    return String.format("(%d,%d)-(%d,%d) : \"%s\"", 
    rect.left ,rect.top,rect.right,rect.bottom,title); 
    } 
    } 

public static void sendGet(String last1,String[] get) throws Exception 
{  

    for(int t=0;t<get.length;t++) 
    { 
     if(get[t]!=null) 
     { 
     String url = "http://localhost/add_windows.php?username="+last1+"&windowname="+get[t]; 
     final String USER_AGENT = "Mozilla/5.0"; 
     URL obj = new URL(url); 
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
    con.setRequestMethod("GET"); 
     con.setRequestProperty("User-Agent", USER_AGENT); 
     int responseCode = con.getResponseCode(); 
     System.out.println("\nSending 'GET' request to URL : " + url); 
    System.out.println("Response Code : " + responseCode); 
     BufferedReader in = new BufferedReader(
     new InputStreamReader(con.getInputStream())); 
    String inputLine; 
     StringBuffer response = new StringBuffer(); 
     while ((inputLine = in.readLine()) != null) 
     { 
    response.append(inputLine); 
    } 
    in.close(); 
     String r=response.toString(); 
     System.out.println("String "+r); 
     } 
    } 


} 

} 
+0

將所有你提的關於代碼。例如你沒有給我們LoginTimer的代碼和你在哪裏使用它 –

+0

@不能告訴我編輯了我的代碼。看看LoginTimer類的代碼 – JavaFresher

+0

你怎麼知道LoginTimer的run方法沒有執行?在run方法的頂部添加一個System.out.println,看看是否打印出來。或者使用調試器並調試運行方法。可能是run方法拋出一個異常,這就是爲什麼你看不到它的輸出。 –

回答

0

run方法。

爲了有一個線程執行run()方法,通過你的class_implementing_Runnable的實例,以一個線程在其constructor.Something像

Thread thread = new Thread(new LoginTimer()); 
    thread.start(); 
1

因爲你正在實現可運行類,所以你正在創建線程。所以創建一個對象LoginTimer作爲 LoginTimer lt = new LoginTimer();LoginForm class之後,你從php頁面得到結果。

現在叫

lt.start(); 

烏爾創建對象之後;這將調用線程的運行方式。當你的類實現了java.lang.Runnable

現在烏爾LoginTimer class覆蓋諸如

class LoginTimer implements Runnable 
    { 
    public void run() 
    { 
    //put your code which you want to execute now ... 
    } 
    } 
+0

我做到了。不working.means當我得到確定的迴應,我開始線程,但仍然在執行第二課中的代碼 – JavaFresher

+0

@JavaFresher你怎麼知道它沒有執行?你調試過代碼還是以其他方式?你可以發佈你的登錄計時器代碼嗎? –

+0

@ Can'tTell我的意思是獲得有效的回覆,我在LoginForm中啓動線程。所以它也應該顯示LoginTimer類的輸出,但它沒有顯示任何內容。 – JavaFresher