當前位置:首頁 » 編程語言 » java內嵌瀏覽器

java內嵌瀏覽器

發布時間: 2022-12-17 02:28:11

⑴ 用java編寫一個簡單的瀏覽器程序

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.event.*;
@SuppressWarnings("serial")
class Win3 extends JFrame implements ActionListener,Runnable
{
JButton button;
URL url;
JTextField text;
JEditorPane editPane;
byte b[]=new byte[118];
Thread thread;
public Win3()
{
text=new JTextField(20);
editPane=new JEditorPane();
editPane.setEditable(false);
button=new JButton("確定");
button.addActionListener(this);
thread=new Thread(this);
JPanel p=new JPanel();
p.add(new JLabel("輸入網址:"));
p.add(text);
p.add(button);
Container con=getContentPane();
con.add(new JScrollPane(editPane),BorderLayout.CENTER);
con.add(p,BorderLayout.NORTH);
setBounds(60,60,400,300);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editPane.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent e)
{
if(e.getEventType()==
HyperlinkEvent.EventType.ACTIVATED)
{
try{
editPane.setPage(e.getURL());
}
catch(IOException e1)
{
editPane.setText(""+e1);
}
}
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(!(thread.isAlive()))
thread=new Thread(this);
try{
thread.start();
}
catch(Exception ee)
{
text.setText("我正在讀取"+url);
}
}
public void run()
{
try {
int n=-1;
editPane.setText(null);
url=new URL(text.getText().trim());
editPane.setPage(url);
}
catch(MalformedURLException e1)
{
text.setText(""+e1);
return;
}
catch(IOException e1)
{
text.setText(""+e1);
return;
}
}
}
public class Example3
{
public static void main(String args[])
{
new Win3();
}
}

熱點內容
新年解壓糖 發布:2024-05-20 09:50:55 瀏覽:54
以太坊價值在哪裡存儲 發布:2024-05-20 09:46:34 瀏覽:641
cgipython配置 發布:2024-05-20 09:29:06 瀏覽:865
在我的世界伺服器中隱身 發布:2024-05-20 09:07:46 瀏覽:972
加西貝拉壓縮機好嗎 發布:2024-05-20 08:58:56 瀏覽:757
eve腳本航 發布:2024-05-20 08:56:59 瀏覽:591
取票人的密碼是什麼 發布:2024-05-20 08:21:43 瀏覽:963
天貓帳號密碼應輸入什麼 發布:2024-05-20 08:16:26 瀏覽:272
plsql異常處理 發布:2024-05-20 07:54:47 瀏覽:542
dreamweaver上傳網頁 發布:2024-05-20 07:51:24 瀏覽:462