如何在Jframe表单类中调用另一个类?

时间:2014-06-24 09:41:38

标签: java swing jframe httpurlconnection

我有一个像这样的Jframe表单类

  public class LoginForm extends javax.swing.JFrame 

在此,我得到用户名&来自用户的密码,然后将其发送到php服务器进行验证 将获得响应为OK或无效用户。我有另一个名为' 公共类的类,LoginTimer实现了Runnable' 。在这个类中,我有一些代码要执行。我希望在' LoginForm '当我得到响应时,控件将转到第二类' 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 类的代码。在这里,我得到可见窗口的名称,然后线程启动,并在 run()方法我调用 sendGet()方法将窗口名称发送到php服务器页面。我希望当我在 LoginForm 类中得到OK响应时, LoginTimer 类将被自动调用并执行。我的意思是,当用户登录&验证然后将窗口名称发送到php服务器将自动启动。

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);
      }
     }


 }

}

2 个答案:

答案 0 :(得分:1)

当你正在实现runnable类时,你正在创建线程。因此,创建LoginTimer的对象为         LoginTimer lt = new LoginTimer();     从php页面获得结果后,在LoginForm class中。

现在致电

   lt.start(); 

创建对象后;这将调用ur run方法的线程。

现在你的LoginTimer class覆盖了像

这样的运行方法
   class LoginTimer implements Runnable
   {
   public void run()
   {
   //put your code which you want to execute now ... 
   }
   }

答案 1 :(得分:0)

由于您的类实现了java.lang.Runnable。

要让线程执行run()方法,请将class_implementing_Runnable的实例传递给构造函数中的Thread.Something like

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