我有一个这样的Jframe表单类
public class LoginForm extends javax.swing.JFrame
在这种情况下,我从用户那里获取用户名和密码,然后将其发送到php服务器进行验证,并将获得“确定”或“无效用户”的响应。我还有一个名为“公共类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);
}
}
}
}
当您的类实现java.lang.Runnable时。
要让run()方法由线程执行,请将class_implementing_Runnable的实例传递给其构造函数中的Thread。
Thread thread = new Thread(new LoginTimer());
thread.start();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句