admin 管理员组

文章数量: 887026


2024年2月21日发(作者:视图第一视角和第三视角区别)

/*

**网页浏览器主程序

**

*/

import .*;

import .*;

import .*;

import ;

import ist;

import .*;

import .*;

@SuppressWarnings("serial")

public class WebBrowser extends JFrame

HyperlinkListener,ActionListener{

//建立工具栏用来显示地址栏

JToolBar bar=new JToolBar ();

//建立网页显示界面

JTextField jurl = new JTextField (60);

JEditorPane jEditorPane1 = new JEditorPane ();

JScrollPane scrollPane = new JScrollPane (jEditorPane1);

JFileChooser chooser=new JFileChooser ();

implements

JFileChooser chooser1=new JFileChooser ();

String htmlSource;

JWindow window = new JWindow ();

JButton button2=new JButton ("窗口还原");

Toolkit toolkit = aultToolkit();

//建立菜单栏

JMenuBar jMenuBar1 = new JMenuBar();

//建立菜单组

JMenu fileMenu = new JMenu ("文件(F)");

//建立菜单项

JMenuItem saveAsItem = new JMenuItem ("另存为(A)...");

JMenuItem exitItem=new JMenuItem ("退出(I)");

JMenu editMenu=new JMenu ("编辑(E)");

JMenuItem backItem=new JMenuItem ("后退");

JMenuItem forwardItem=new JMenuItem ("前进");

JMenu viewMenu=new JMenu ("视图(V)");

JMenuItem fullscreenItem=new JMenuItem ("全屏(U)");

JMenuItem sourceItem=new JMenuItem ("查看源码(C)");

JMenuItem reloadItem=new JMenuItem ("刷新(R)");

//建立工具栏

JToolBar toolBar = new JToolBar();

//建立工具栏中的按钮组件

JButton picSave = new JButton("另存为");

JButton picBack = new JButton("后退");

JButton picForward = new JButton("前进");

JButton picView = new JButton("查看源代码");

JButton picExit = new JButton("退出");

JLabel label=new JLabel ("地址");

JButton button=new JButton ("转向");

Box adress=HorizontalBox ();

//ArrayList对象,用来存放历史地址

@SuppressWarnings("rawtypes")

private ArrayList history=new ArrayList();

//整型变量,表示历史地址的访问顺序

private int historyIndex;

/**

**构造函数

**初始化图形用户界面

*/

public WebBrowser(){

setTitle ("web浏览器 11软件技术302班");

setResizable(false);

setDefaultCloseOperation(_ON_CLOSE);

//为jEditorPane1添加事件侦听

erlinkListener (this);

//为组件fileMenu设置热键‘F’

monic('F');

monic ('S');

//为“另存为”组件设置快捷键为ctrl+s

elerator

(_S,_MASK));

monic('Q');

elerator

(_E,_MASK));

//将菜单项saveAsItem加入到菜单组fileMenu中

(saveAsItem);

//在菜单项中添加隔离

(Stroke

(Stroke

arator ();

(exitItem);

monic ('B');

elerator

(_Z,_MASK));

monic('D');

elerator

(_P,_MASK));

monic('E');

(backItem);

(forwardItem);

monic('V');

monic('U');

elerator

(_U,_MASK));

monic('C');

elerator

(_C,_MASK));

monic('R');

elerator

(_R,_MASK));

Container contentPane=getContentPane ();

(Stroke

(Stroke

(Stroke

(Stroke

(Stroke

//设置大小

ferredSize(new Dimension(100,500));

(scrollPane, );

//在工具栏中添加按钮组件

(picSave);

arator();

(picBack);

(picForward);

arator();

(picView);

arator();

(picExit);

(bar,);

(toolBar,);

(fullscreenItem);

(sourceItem);

arator ();

(reloadItem);

(fileMenu);

(editMenu);

(viewMenu);

setJMenuBar (jMenuBar1);

(label);

(jurl);

(button);

(adress);

//为组件添加事件监听

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

ionListener(this);

}

/**

**实现监听器接口的actionPerformed函数

*/

@SuppressWarnings("unchecked")

public void actionPerformed(ActionEvent e) {

String url = "";

//点击转向按钮

if (rce() == button){

//获得地址栏的内容

url=t ();

//url不为“”,并且以“”开头

if( ()>0&&With ("")){

try {

//JEditorPane组件显示url的内容链接

e (url);

//将url的内容添加到ArrayList对象history中

(url);

//historyIndex的数值设为history对象的长度-1

historyIndex=()-1;

//重新布局

table(false); //add by copy editor :)

date ();

}

catch(Exception ex) {

//如果链接显示失败,则弹出选择对话框“无法打开该搜索页”

ssageDialog (,"无法打开该搜索页","网页浏览器",_MESSAGE);

}

}

//url不为“”,并且不以“”开头

else if( ()>0&&!With ("")) {

//在url前面添加“”

url=""+url;

try {

e (url );

(url);

historyIndex=()-1;

table(false); //add by copy editor :)

date ();

}

catch(Exception ex) {

ssageDialog (,"无法打开该搜索页","网页浏览器",_MESSAGE);

}

}

//没有输入url,即url为空

else if( ()==0){

ssageDialog (,"请输入链接地址","网页浏览器",_MESSAGE);

}

}

//输入地址后点击回车

else if (rce() == jurl){

url=t ();

if( ()>0&&With ("")) {

try {

e (url);

(url);

historyIndex=()-1;

table(false); //add by copy editor :)

date ();

imumSize (ferredSize ());

}

catch(Exception ex) {

ssageDialog (,"无法打开该搜索页","网页浏览器",_MESSAGE);

}

}

else if( ()>0&&!With ("")) {

url=""+url;

try {

e (url );

(url);

historyIndex=()-1;

table(false); //add by copy editor :)

date ();

}

catch(Exception ex) {

ssageDialog (,"无法打开该搜索页","网页浏览器",_MESSAGE);

}

}

else if( ()==0){

ssageDialog (,"请输入链接地址","网页浏览器",_MESSAGE);

}

}

//另存为...

else if (rce() == picSave||rce() == saveAsItem){

url = t ().toString ().trim();

if( ()>0&&!With ("")) {

url=""+url;

}

if(! ("")) {

//保存文件

saveFile(url);

}

else {

ssageDialog (,"请输入链接地址","网页浏览器",_MESSAGE);

}

}

//退出

else if (rce() == exitItem ||rce() == picExit){

(0);

}

//后退

else if (rce() == backItem ||rce() == picBack){

historyIndex--;

if(historyIndex < 0)

historyIndex = 0;

url = t();

try{

//获得history对象中本地址之前访问的地址

url = (String)(historyIndex);

e(url);

t(ng());

table(false); //add by copy editor :)

date ();

}

catch(Exception ex){

}

}

//前进

else if (rce() == forwardItem ||rce() == picForward){

historyIndex++;

if(historyIndex >= ())

historyIndex = ()-1;

url = t();

try{

//获得history对象中本地址之后访问的地址

url = (String)(historyIndex);

e(url);

t(ng());

table(false); //add by copy editor :)

date ();

}

catch(Exception ex){

}

}

//全屏

else if (rce() == fullscreenItem){

boolean add_button2=true;

//获得屏幕大小

Dimension size = aultToolkit().getScreenSize();

Container content = tentPane();

(bar,"North");

(scrollPane,"Center");

//button2为点击“全屏”后的还原按钮

if(add_button2==true) {

(button2);

}

//为button2添加事件

ionListener (new ActionListener () {

public void actionPerformed (ActionEvent evt) {

bled (true);

(bar);

(toolBar);

(scrollPane);

ible (false);

ferredSize (new Dimension (100,500));

getContentPane ().add (scrollPane,);

getContentPane ().add (bar,);

getContentPane ().add (toolBar,);

(button2);

pack();

}

});

e (size);

ible (true);

}

//查看源文件

else if (rce() == sourceItem ||rce() == picView){

url = t ().toString ().trim ();

if( ()>0&&!With ("")) {

url=""+url;

}

if( ! ("")) {

//根据url,获得源代码

getHtmlSource (url);

//生成显示源代码的框架对象

ViewSourceFrame vsframe = new ViewSourceFrame (htmlSource);

nds (0,0,800,500);

ible(true);

}

else {

ssageDialog (,"请输入链接地址","网页浏览器",_MESSAGE);

}

}

//刷新

else if (rce() == reloadItem){

url=t ();

if( ()>0&&With ("")) {

try {

e (url);

table(false); //add by copy editor :)

date ();

}

catch(Exception ex) {

}

}

else if( ()>0&&!With ("")) {

url=""+url;

try {

e (url );

table(false); //add by copy editor :)

date ();

}

catch(Exception ex) {

}

}

}

}

/*

**保存文件

*/

void saveFile (final String url) {

final String linesep = perty ("tor");

rentDirectory (new File ("."));

logType (_DIALOG);

logTitle ("另存为...");

if(veDialog (this) != E_OPTION)

return;

t ();

Thread thread = new Thread () {

public void run () {

try {

source = new URL (url);

InputStream in = new BufferedInputStream (ream ());

BufferedReader br=new BufferedReader (new InputStreamReader

(in));

File fileName = ectedFile ();

FileWriter out = new FileWriter (fileName);

BufferedWriter bw = new BufferedWriter (out);

String line;

while((line = ne ()) != null) {

(line);

e ();

}

();

();

();

String dMessage = url + " 已经被保存至"+ linesep

+olutePath ();

String dTitle = "另存为";

int dType = ATION_MESSAGE;

ssageDialog

null,dMessage,dTitle,dType);

}

catch(medURLException muex) {

ssageDialog ((Component)null,ng (),"网页浏览器",_MESSAGE);

}

catch(Exception ex) {

ssageDialog ((Component) null,ng (),"网页浏览器",_MESSAGE);

}

}

};

();

}

/*

**获得源代码

*/

void getHtmlSource (String url) {

String linesep,htmlLine;

linesep = perty ("tor");

htmlSource ="";

try {

((Component)

source = new URL (url);

InputStream in = new BufferedInputStream (ream ());

BufferedReader br = new BufferedReader ( new InputStreamReader (in));

while((htmlLine = ne ())!=null) {

htmlSource = htmlSource +htmlLine+linesep;

}

}

catch(medURLException muex) {

ssageDialog (,ng (),"网页浏览器",_MESSAGE);

}

catch(Exception e) {

ssageDialog (,ng (),"网页浏览器",_MESSAGE);

}

}

/**

**实现监听器接口的hyperlinkUpdate函数

*/

public void hyperlinkUpdate (HyperlinkEvent e) {

/* if(ntType () == TED) {

String url = t();

if( ()>0&&With ("")) {

try {

e (url);

date ();

}

catch(Exception ex) {

}

}

else if( ()>0&&!With ("")) {

url=""+url;

try {

e (url );

date ();

}

catch(Exception ex) {

}

}

}

*/

// Revised by copy editor :) Now with new code to help hyperlink

}

try {

if (ntType() == TED)

e(());

} catch (Exception ex) {

}

tackTrace();

/*生成一个IE对象*/

public static void main (String [] args){

try{

kAndFeel(

ssPlatformLookAndFeelClassName()

);

}

catch(Exception e){

}

WebBrowser webBrowser = new WebBrowser ();

();

ible(true);

}

}


本文标签: 地址 对象 网页 显示 组件