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}