/** */
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: Geong</p>
* @author Will he
* @date 2008-4-29
* @version 1.0
*/
package thinkingInJava;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AccessWebSite extends JFrame ... {
JPanel p1;
JTextField txtContent;
JButton btnOK;
JFileChooser fc;
JLabel lblInformation;
JTextField txtCount;
int AccessCount=0;//计数器
int defaultCount =50;
public AccessWebSite()...{
p1=new JPanel();
p1.setBackground(Color.ORANGE);
txtContent=new JTextField("请输入访问的路径...",22);
txtCount = new JTextField("请输入访问次数",8);
btnOK=new JButton("确定");
btnOK.setEnabled(false);//为避免出错,默认确定按钮不可用
lblInformation=new JLabel("说明:本软件仅完成指定网络地址的访问");
lblInformation.setForeground(Color.blue);
lblInformation.setAutoscrolls(true);
p1.add(txtContent);
p1.add(txtCount);
p1.add(btnOK);
p1.add(lblInformation);
//对文本框的鼠标监听
txtContent.addMouseListener(new MouseListener()...{
public void mouseReleased(MouseEvent e)...{}
public void mousePressed(MouseEvent e)...{}
//当鼠标移出时,如果文本框为空,则让确定按钮不可用
public void mouseExited(MouseEvent e)...{
if(txtContent.getText().equals("")||txtCount.getText().equals(""))...{
btnOK.setEnabled(false);
}else...{
btnOK.setEnabled(true);
}
}
public void mouseEntered(MouseEvent e)...{}
//单击鼠标的时候,如果文本框里为提示的文字,则清空文本框
public void mouseClicked(MouseEvent e)...{
if(txtContent.getText().equals("请输入访问的路径..."))...{
txtContent.setText("HTTP://");
}
if(txtCount.getText().equals("请输入访问次数"))...{
txtCount.setText(String.valueOf(defaultCount));
}
}
}
);
//对文本框的按键监听
txtContent.addKeyListener(new KeyListener()...{
public void keyTyped(KeyEvent e)...{
}
public void keyReleased(KeyEvent e)...{
}
public void keyPressed(KeyEvent e)...{
if(txtContent.getText().equals(""))...{
btnOK.setEnabled(false);
}else...{
btnOK.setEnabled(true);
}
}
}
);
//对确定按钮的监听
btnOK.addActionListener(new ActionListener()...{
AccessThread t;
public void actionPerformed(ActionEvent e)...{
int count = Integer.parseInt(txtCount.getText().toString());
String path=txtContent.getText().toString();
AccessCount=0;//初始化访问计数器
t=new AccessThread(path,count);
t.start();
}
class AccessThread extends Thread...{
String tmppath;
int tmpcount=0;
public AccessThread(String path,int count)...{
this.tmppath=path;
this.tmpcount =count;
}
public void run()...{
try...{
URL url = new URL(tmppath);
for(int i=0;i<tmpcount;i++ )...{
/**//*使当前执行线程休眠(临时停止执行)指定的毫秒数*/
this.sleep(100);
URLConnection urlcon = url.openConnection();
InputStream IS= urlcon.getInputStream();
IS.close();
AccessCount++;
}
lblInformation.setText("共访问了此地址"+AccessCount+"次");
}catch(Exception ex)...{
System.out.println(ex);
}finally...{
this.stop();
}
}
}
}
);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(p1);
this.setTitle("访问网站");
this.setResizable(false);
this.setSize(500,100);
this.setLocation(300,200);
this.setVisible(true);
}
public static void main(String[] args)...{
new AccessWebSite();
}
}
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: Geong</p>
* @author Will he
* @date 2008-4-29
* @version 1.0
*/
package thinkingInJava;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AccessWebSite extends JFrame ... {
JPanel p1;
JTextField txtContent;
JButton btnOK;
JFileChooser fc;
JLabel lblInformation;
JTextField txtCount;
int AccessCount=0;//计数器
int defaultCount =50;
public AccessWebSite()...{
p1=new JPanel();
p1.setBackground(Color.ORANGE);
txtContent=new JTextField("请输入访问的路径...",22);
txtCount = new JTextField("请输入访问次数",8);
btnOK=new JButton("确定");
btnOK.setEnabled(false);//为避免出错,默认确定按钮不可用
lblInformation=new JLabel("说明:本软件仅完成指定网络地址的访问");
lblInformation.setForeground(Color.blue);
lblInformation.setAutoscrolls(true);
p1.add(txtContent);
p1.add(txtCount);
p1.add(btnOK);
p1.add(lblInformation);
//对文本框的鼠标监听
txtContent.addMouseListener(new MouseListener()...{
public void mouseReleased(MouseEvent e)...{}
public void mousePressed(MouseEvent e)...{}
//当鼠标移出时,如果文本框为空,则让确定按钮不可用
public void mouseExited(MouseEvent e)...{
if(txtContent.getText().equals("")||txtCount.getText().equals(""))...{
btnOK.setEnabled(false);
}else...{
btnOK.setEnabled(true);
}
}
public void mouseEntered(MouseEvent e)...{}
//单击鼠标的时候,如果文本框里为提示的文字,则清空文本框
public void mouseClicked(MouseEvent e)...{
if(txtContent.getText().equals("请输入访问的路径..."))...{
txtContent.setText("HTTP://");
}
if(txtCount.getText().equals("请输入访问次数"))...{
txtCount.setText(String.valueOf(defaultCount));
}
}
}
);
//对文本框的按键监听
txtContent.addKeyListener(new KeyListener()...{
public void keyTyped(KeyEvent e)...{
}
public void keyReleased(KeyEvent e)...{
}
public void keyPressed(KeyEvent e)...{
if(txtContent.getText().equals(""))...{
btnOK.setEnabled(false);
}else...{
btnOK.setEnabled(true);
}
}
}
);
//对确定按钮的监听
btnOK.addActionListener(new ActionListener()...{
AccessThread t;
public void actionPerformed(ActionEvent e)...{
int count = Integer.parseInt(txtCount.getText().toString());
String path=txtContent.getText().toString();
AccessCount=0;//初始化访问计数器
t=new AccessThread(path,count);
t.start();
}
class AccessThread extends Thread...{
String tmppath;
int tmpcount=0;
public AccessThread(String path,int count)...{
this.tmppath=path;
this.tmpcount =count;
}
public void run()...{
try...{
URL url = new URL(tmppath);
for(int i=0;i<tmpcount;i++ )...{
/**//*使当前执行线程休眠(临时停止执行)指定的毫秒数*/
this.sleep(100);
URLConnection urlcon = url.openConnection();
InputStream IS= urlcon.getInputStream();
IS.close();
AccessCount++;
}
lblInformation.setText("共访问了此地址"+AccessCount+"次");
}catch(Exception ex)...{
System.out.println(ex);
}finally...{
this.stop();
}
}
}
}
);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(p1);
this.setTitle("访问网站");
this.setResizable(false);
this.setSize(500,100);
this.setLocation(300,200);
this.setVisible(true);
}
public static void main(String[] args)...{
new AccessWebSite();
}
}