简体   繁体   English

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

[英]How to call another class in Jframe form class?

I have a Jframe form class like this 我有一个这样的Jframe表单类

  public class LoginForm extends javax.swing.JFrame 

In this,i get username & password from user and then send it to php server for validation and will get the response as OK or Invalid User. 在这种情况下,我从用户那里获取用户名和密码,然后将其发送到php服务器进行验证,并将获得“确定”或“无效用户”的响应。 I have another class named ' public class LoginTimer implements Runnable' . 我还有一个名为“ 公共类LoginTimer实现Runnable”的类 In this class i have some code to execute. 在这个课上,我有一些代码要执行。 I want that in ' LoginForm ' when i got response as OK, the control will move to second class ' LoginTimer ' means second class will be 我希望在“ LoginForm ”中得到正确的响应后,控件将移至第二类“ LoginTimer ”,这意味着第二类将是
called. 调用。 please tell me how to do it?? 请告诉我该怎么做?
===================================================================== ================================================== ===================

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


 }

Below is my code for LoginTimer class. 下面是我的LoginTimer类的代码。 In this, I am getting names of visible windows and then thread starts and in run() method i call sendGet() method for sending window names to php server page. 在这种情况下,我获取可见窗口的名称,然后启动线程,并在run()方法中调用sendGet()方法,以将窗口名称发送到php服务器页面。 I want that when I got the OK response in LoginForm class,the LoginTimer class will be called and executed automatically.I mean, when user logged in & verified then sending of window names to php server will start automatically. 我希望当我在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);
      }
     }


 }

}

As u are implementing runnable class you are creating thread. 当您实现可运行类时,您正在创建线程。 So create an object of LoginTimer as LoginTimer lt = new LoginTimer(); 因此,将LoginTimer的对象创建为LoginTimer lt = new LoginTimer(); in LoginForm class after you get result from php page. 从PHP页面获取结果后,在LoginForm class

Now call 现在打电话

   lt.start(); 

after ur creation of object ; 在创造物体之后; which will call ur run method of thread. 这将调用您的线程运行方法。

Now in ur LoginTimer class override the run method like 现在在您的LoginTimer class重写run方法,例如

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

As your class implements java.lang.Runnable. 当您的类实现java.lang.Runnable时。

To have the run() method executed by a thread, pass an instance of your class_implementing_Runnable to a Thread in its constructor.Something like 要让run()方法由线程执行,请将class_implementing_Runnable的实例传递给其构造函数中的Thread。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM