java页面源码
自己写的石头剪子布代码,有图形界面,可以把自己选中的图片命名为.jpg放到工程目录下做背景图片。//////////////////////////// package HomeWork;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class StoneShearsSack { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new StoneWindow("Sample Game");
}
}
final class StoneWindow extends JFrame implements ActionListener,ItemListener{
JLabel win,draw,lose,score,count;
JTextArea text;
JComboBox list;
JButton fight,clear;
int swin,sdraw,slose,sscore;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
String str1,str2,str3;
Random rdm = new Random();
int flag,random ,flag1, count1;
//final ImageIcon con = new ImageIcon("a.jpg") ;
public StoneWindow(String s){
setTitle("Sample Game");
fight = new JButton("出招");
clear = new JButton("清除分数");
//final ImageIcon con = new ImageIcon("b.jpg") ;
text = new JTextArea(){
final ImageIcon con = new ImageIcon("a.jpg");
Image mg = con.getImage();
{setOpaque(false);}
public void paintComponent(Graphics g){
g.drawImage(mg, 0, 0, this);
super.paintComponent(g);
}
};
text.setForeground(Color.black);
Font f = new Font("华文行楷",Font.BOLD,50);
text.setFont(f);
list = new JComboBox();
list.addItem("石头");
list.addItem("剪子");
list.addItem("布");
panel2.setBackground(Color.blue);
panel2.add(list);
panel2.add(fight);
panel2.add(clear);
panel2.setPreferredSize(new Dimension(900,50));
list.addItemListener(this);
fight.addActionListener(this);
clear.addActionListener(this);
win = new JLabel("赢:" + swin);
draw = new JLabel(" 和:" + sdraw);
lose = new JLabel(" 输:" + slose);
score = new JLabel(" 得分:" + sscore);
count = new JLabel(" 总局:" + count1);
Font f1 = new Font("宋体",1,30);
win.setFont(f1);
draw.setFont(f1);
lose.setFont(f1);
score.setFont(f1);
count.setFont(f1);
win.setForeground(Color.cyan);
draw.setForeground(Color.green);
lose.setForeground(Color.blue);
score.setForeground(Color.black);
count.setForeground(Color.pink);
panel1.setBackground(Color.red);
panel1.add(win);
panel1.add(draw);
panel1.add(lose);
panel1.add(score);
panel1.add(count);
panel1.setPreferredSize(new Dimension(900,50));
add(panel1,BorderLayout.NORTH);
add(text,BorderLayout.CENTER);
add(panel2,BorderLayout.SOUTH);
setBounds(100,100,900,450);
validate();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == fight){
if(list.getSelectedItem() == "石头"){
flag = 0;
}
else if(list.getSelectedItem() == "剪子"){
flag = 1;
}
else if(list.getSelectedItem() == "布"){
flag = 2;
}
str1 = (String)list.getSelectedItem();
text.setText("");
text.append("\n");
text.append(" 你 出: " + str1 + "\n");
text.append(" 电脑出: " + this.random() + "\n");
text.append(" 结 果: " + this.result() + "\n");
this.finalresult();
}
if(e.getSource() == clear){
swin = 0;
sdraw = 0;
slose = 0;
sscore = 0;
count1 = 0;
text.setText("");
win.setText(" 赢:" + swin);
draw.setText(" 和:" + sdraw);
lose.setText(" 输:" + slose);
score.setText(" 得分:" + sscore);
count.setText(" 总局:" + count1);
}
}
public String random(){
random = rdm.nextInt(3);
if(random == 0)
str2 = "石头";
else if(random == 1)
str2 = "剪子";
else if(random == 2)
str2 = "布";
return str2;
}
public String result(){
if(random == flag){
str3 ="打平";
flag1 = 0;
}
else if(random < flag &&flag == 1){
str3 = "你输了";
flag1 = 1;
}
else if(random <flag && random ==1){
str3 = "你输了";
flag1 = 1;
}
else if(random ==2 && flag == 0){
str3 = "你输了";
flag1 = 1;
}
else{
str3 = "你赢了";
flag1 = 2;
}
return str3;
}
public void finalresult(){
if(flag1 == 0){
sdraw ++;
draw.setText(" 平:" + sdraw);
score.setText(" 得分:" + sscore);
}
if(flag1 == 1){
slose ++;
sscore = sscore - 10;
lose.setText(" 输:" + slose);
score.setText(" 得分:" + sscore);
}
if(flag1 == 2) {
swin ++;
sscore = sscore + 10;
win.setText(" 赢:" + swin);
score.setText(" 得分:" + sscore);
}
count1 = swin + slose + sdraw;
count.setText(" 总局:" + count1);
}
public void itemStateChanged(ItemEvent e){
}
}
㈡ java程序读取一个url页面的源代码
传入一个url,返回源代码; public static String getHTML(String url){// 获取指定URL的网页,返回网页内容的字符串,然后将此字符串存到文件即可 try { URL newUrl = new URL(url); URLConnection connect = newUrl.openConnection(); connect.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); DataInputStream dis = new DataInputStream(connect.getInputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(dis,"UTF-8")); String html = ""; String readLine = null; while((readLine = in.readLine()) != null) { html = html + readLine; } in.close(); return html; }catch (MalformedURLException me){ System.out.println("MalformedURLException" + me); }catch (IOException ioe){ System.out.println("ioeException" + ioe); } return null; }
㈢ 怎么用java程序抓取网页源代码
用httpclient吧,网上很多例子的。下面有一个你看看。
importorg.apache.http.HttpResponse;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.util.EntityUtils;
publicStringgetWeChatUserInfo(Stringtoken,Stringopenid){
StringurlNameString="https://api.weixin.qq.com/sns/userinfo?access_token=TOKEN&openid=OPENID";
urlNameString=urlNameString.replace("TOKEN",token);
urlNameString=urlNameString.replace("OPENID",openid);
Stringresult="";
try{
//根据地址获取请求
HttpGetrequest=newHttpGet(urlNameString);//这里发送get请求
//获取当前客户端对象
HttpClienthttpClient=newDefaultHttpClient();
//通过请求对象获取响应对象
HttpResponseresponse=httpClient.execute(request);
//判断网络连接状态码是否正常(0--200都数正常)
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
result=EntityUtils.toString(response.getEntity(),"utf-8");
}
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnresult;
//....result是用户信息,站内业务以及具体的json转换这里也不写了...
}
㈣ java 获取网页源代码---有效防止乱码
前段时间做过这类功能,如何有效防止乱码,我们必须先知道一个网页的编码方式,是utf-8,还是gbk。
1.HttpURLConnection.getContentType();直接读取,效率高,但有很多时候读不到。只是text/html就完事了,没有charset.
2.使用第三方的HttpClient,执行效率较高。但读取网页头header也只适用部分站,很多网站服务段不设置,结果就读成了null.
3.最没有效率的判断方法就是使用inputStreamReader先把正页的html源码读取出来,之后截取charset后面编码。得到编码之后重新再读取一遍。但是效率很低。
做个总结:
/**
* 取得页面编码
*
* @param url
* @return
*/
public String getCharset(String url) throws Exception {
// log.info("进入读页面的关键词:" + keyword);
String charset = "";
int c;
HttpURLConnection httpurlcon = null;
// log.info("url:"+url);
// log.info("charset:"+charset);
log.info("url:" + url);
URL httpurl = new URL(url);
// System.out.println(url+str);
httpurlcon = (HttpURLConnection) httpurl.openConnection();
// google需要身份
httpurlcon.setRequestProperty("User-agent", "Mozilla/4.0");
charset = httpurlcon.getContentType();
log.info("charset1:" + charset);
// 如果可以找到
if (charset.indexOf("charset=") != -1)
charset = charset.substring(charset.indexOf("charset=")
+ "charset=".length(), charset.length());
// 否则读取response.Header头
else {
charset = this.getContentCharset();
log.info("charset2:" + charset);
}
// 如果charset还是为空,那么直接读网页来截取
if (charset == null) {
charset = this.readPageCharset(url);
log.info("charset31:" + charset);
}
return charset;
}
㈤ java获取网页源码的问题
先知道一个网页的编码方式,是utf-8,还是gbk。
1.HttpURLConnection.getContentType();直接读取,效率高,但有很多时候读不到。只是text/html就完事了,没有charset.
2.使用第三方的HttpClient,执行效率较高。但读取网页头header也只适用部分站,很多网站服务段不设置,结果就读成了null.
3.最没有效率的判断方法就是使用inputStreamReader先把正页的html源码读取出来,之后截取charset后面编码。得到编码之后重新再读取一遍。但是效率很低。
做个总结:
/**
* 取得页面编码
*
* @param url
* @return
*/
public String getCharset(String url) throws Exception {
// log.info("进入读页面的关键词:" + keyword);
String charset = "";
int c;
HttpURLConnection httpurlcon = null;
// log.info("url:"+url);
// log.info("charset:"+charset);
log.info("url:" + url);
URL httpurl = new URL(url);
// System.out.println(url+str);
httpurlcon = (HttpURLConnection) httpurl.openConnection();
// google需要身份
httpurlcon.setRequestProperty("User-agent", "Mozilla/4.0");
charset = httpurlcon.getContentType();
log.info("charset1:" + charset);
// 如果可以找到
if (charset.indexOf("charset=") != -1)
charset = charset.substring(charset.indexOf("charset=")
+ "charset=".length(), charset.length());
// 否则读取response.Header头
else {
charset = this.getContentCharset();
log.info("charset2:" + charset);
}
// 如果charset还是为空,那么直接读网页来截取
if (charset == null) {
charset = this.readPageCharset(url);
log.info("charset31:" + charset);
}
return charset;
}
㈥ 如何通过java,javascript获得某一指定URL的网页源码
javascript获取网页源码,测试了能通过的,你试下!
你把& l t ; & g t;的空格删了,因为网络不允许那些字符。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>test</title>
</head>
<body>
关于网络页面http://ir..com/phoenix.zhtml?c=188488&p=irol-homeprofile源码内容如下:<br />
<div id="t1"></div>
<script type="text/javascript">
function createXmlHttpRequest()
{
try {return new XMLHttpRequest();}
catch(e)
{
return new ActiveXObject("Microsoft.XMLHTTP");}
}
var xmlHttp = createXmlHttpRequest();
xmlHttp.open("get", "http://ir..com/phoenix.zhtml?c=188488&p=irol-homeprofile",false);
xmlHttp.send();
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
str = xmlHttp.responseText;//str即为返回的html内容
var str1 = new RegExp(">","g");
var str2 = new RegExp("<","g");
var str3 = new RegExp("\r","g");
str=str.replace(str2,"& l t ;"); //把空格删了,网络会把那些字符替换掉,不得已打空格
str=str.replace(str1,"& g t ;"); //把空格删了,网络会把那些字符替换掉,不得已打空格
str=str.replace(str3,"<br />");
document.getElementById("t1").innerHTML = str;
}
</script>
</body>
</html>
㈦ java如何获取带有css js 执行完后的网页源代码
只能抓取静态的页面源代码,因为很多事件和样式是动态绑定和执行的,所以不可能获取到执行完后的代码的。
public
String
getHtmlContent(String
htmlurl)
{
URL
url;
String
temp;
StringBuffer
sb
=
new
StringBuffer();
try
{
url
=
new
URL(htmlurl);
BufferedReader
in
=
new
BufferedReader(new
InputStreamReader(url.openStream(),
"gbk"));
while
((temp
=
in.readLine())
!=
null)
{
sb.append(temp);
}
in.close();
}
catch
(final
MalformedURLException
me)
{
me.getMessage();
}
catch
(final
IOException
e)
{
e.printStackTrace();
}
return
sb.toString();
}
㈧ java 得到网页源码为啥跟浏览器查看到的网页源码不一样呢
从浏览器查看到的源码是页面运行过之后的html静态文件(这个就是最终源码了),并不是开发时候的页面源码。
㈨ Java访问指定URL并获取网页源代码
1.编写useSourceViewer 类的基本框架,该类仅包括无返回值的main ()方法,该方法从参数中获取URL,通过输入缓冲和输出缓冲将该URL 原码输出。
2.编写useSourceViewer 类,代码如下:
import java.net.*;
import java.io.*;
public class useSourceViewer
{
public static void main (String[] args)
{
if (args.length > 0)
{
try
{
//读入URL
URL u = new URL(args[0]);
InputStream in = u.openStream( );
// 为增加性能存储输入流
in = new BufferedInputStream(in);
// 将输入流连接到阅读器
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read( )) != -1)
{
System.out.print((char) c);
}
Object o = u.getContent( );
System.out.println("I got a " + o.getClass().getName( ));
}
catch (MalformedURLException e)
{
System.err.println(args[0] + " is not a parseable URL");
}
catch (IOException e)
{
System.err.println(e);
}
} // end if
} // end main
} // end SourceViewer}