辛辛苦苦写的课程设计不能用。进来吧!(大虾们,虾之大者,为国为民啊)

SANBENREN 2008-04-29 11:38:11
本人天资愚钝,花了三天时间写了个网页浏览器,却不能用!!运行的了,就是打不开网页啊!!救救俺吧!!太又挫败感了,都不感往下进行了!!


/*
**网页浏览器主程序
**WebBrower.java
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileView;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.net.*;
import java.util.*;


public class WebBrowser extends JFrame implements HyperlinkListener,
ActionListener{

//建立工具栏来显示地址栏
JToolBar bar=new JToolBar();
//建立网址输入转向
JLabel label=new JLabel("地址");
JTextField jurl=new JTextField();
JButton button=new JButton("转向");
//建立一个盒子来放网址输入转向
Box adress=Box.createHorizontalBox();

//建立网页来显示页面
JEditorPane jEditorPanel=new JEditorPane();
JScrollPane scrollPane=new JScrollPane(jEditorPanel);


JFileChooser chooser=new JFileChooser();
JFileChooser chooser1=new JFileChooser();
String htmlSource;
JWindow window=new JWindow(WebBrowser.this);

JButton button2=new JButton("窗口还原");
Toolkit toolkit=Toolkit.getDefaultToolkit();


//建立菜单栏
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("退出");
JButton picReload=new JButton("刷新");

//建立一个数组列表用来存放历史地址
private ArrayList<String> history=new ArrayList<String>();
//整型变量,用来表示历史地址的访问顺序
private int historyIndex;


/*
** 构造函数
**初始化图形用户界面
*/

public WebBrowser(){

//对窗口进行设置
setTitle("网页浏览器");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


//为jEditorPane1添加时间监听
jEditorPanel.addHyperlinkListener(this);

//设置各个菜单项

//为fileMenu设置键盘助记符
fileMenu.setMnemonic('F');
//为saveAsItem设置键盘助记符
saveAsItem.setMnemonic('S');
//为另存为设置快捷键为Ctrl+s
saveAsItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_S,InputEvent.CTRL_MASK));
//为exitItem设置键盘助记符
exitItem.setMnemonic('Q');
//为其设置快捷键
exitItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_Q,InputEvent.CTRL_MASK));
//将菜单组中的各种Item加入到文件菜单中
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);

//为eidtMenu设置键盘助记符
editMenu.setMnemonic('E');
//为eidtMenu中的各个操作设置快捷键
backItem.setMnemonic('B');
backItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_B,InputEvent.CTRL_MASK));
forwardItem.setMnemonic('D');
forwardItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_D,InputEvent.CTRL_MASK));
//将编辑菜单中的各个操作加入其中
editMenu.add(backItem);
editMenu.addSeparator();
editMenu.add(forwardItem);

//为viewMenu设置键盘助记符
viewMenu.setMnemonic('V');
//为视图菜单中的各个操作设置快捷键
fullscreenItem.setMnemonic('U');
fullscreenItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_U,InputEvent.CTRL_MASK));
sourceItem.setMnemonic('C');
sourceItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_C,InputEvent.CTRL_MASK));
reloadItem.setMnemonic('R');
reloadItem.setAccelerator(KeyStroke.getKeyStroke
(KeyEvent.VK_R,InputEvent.CTRL_MASK));
//将视图中的各个操作加入到视图菜单中
viewMenu.add(fullscreenItem);
viewMenu.addSeparator();
viewMenu.add(sourceItem);
viewMenu.addSeparator();
viewMenu.add(reloadItem);

//将各个菜单放入JMenuBar中
jMenuBar1.add(fileMenu);
jMenuBar1.add(editMenu);
jMenuBar1.add(viewMenu);
//将所有的菜单项放入frame中
setJMenuBar(jMenuBar1);


//在工具栏中添加按钮
toolBar.add(picSave);
toolBar.addSeparator();
toolBar.add(picBack);
toolBar.addSeparator();
toolBar.add(picForward);
toolBar.addSeparator();
toolBar.add(picView);
toolBar.addSeparator();
toolBar.add(picExit);
toolBar.addSeparator();
toolBar.add(picReload);
//构造地址栏
adress.add(label);
adress.add(jurl);
adress.add(button);
bar.add(adress);


//建立一个contaniner用来装各种组件
Container contentPane=getContentPane();

//将各种操作加入到容器中
contentPane.add(toolBar,BorderLayout.NORTH);
contentPane.add(bar,BorderLayout.CENTER);
contentPane.add(scrollPane,BorderLayout.SOUTH);

//设置网页显示区的大小
scrollPane.setPreferredSize(new Dimension(100,500));

//为组建添加事件监听
saveAsItem.addActionListener(this);
exitItem.addActionListener(this);
backItem.addActionListener(this);
forwardItem.addActionListener(this);
fullscreenItem.addActionListener(this);
sourceItem.addActionListener(this);
reloadItem.addActionListener(this);
picSave.addActionListener(this);
picExit.addActionListener(this);
picBack.addActionListener(this);
picForward.addActionListener(this);
picView.addActionListener(this);
picReload.addActionListener(this);
button.addActionListener(this);
jurl.addActionListener(this);
}




...全文
106 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
SANBENREN 2008-04-29
  • 打赏
  • 举报
回复
//接第二部分后面


//保存文件的函数
void saveFile(final String url){
final String linesep=System.getProperty("line.separator");
chooser1.setCurrentDirectory(new File("."));
chooser1.setDialogType(JFileChooser.APPROVE_OPTION);
chooser1.setDialogTitle("另存为");
if(chooser1.showSaveDialog(this)!=JFileChooser.APPROVE_OPTION)
return;
this.repaint();
Thread thread=new Thread(){
public void run(){
try{
java.net.URL source=new URL(url);
InputStream in=new
BufferedInputStream(source.openStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
File fileName=chooser1.getSelectedFile();
FileWriter out=new FileWriter(fileName);
BufferedWriter bw=new BufferedWriter(out);
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
}
bw.flush();
bw.close();
out.close();
String dMessage=url+"已经被保存至"+linesep
+fileName.getAbsolutePath();
String dTitle="另存为";
int dType=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component)null,dMessage,dTitle,dType);
}catch(java.net.MalformedURLException muex){
JOptionPane.showMessageDialog((Component)null,muex.toString(),"网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
catch(Exception ex){
JOptionPane.showMessageDialog((Component)null,
ex.toString(),"网页浏览器",JOptionPane.ERROR_MESSAGE);
}
}
};
thread.start();
}


//获得源代码的函数
void getHtmlSource(String url){
String linesep,htmlLine;
linesep=System.getProperty("line.separator");
htmlSource="";
try{
java.net.URL source=new URL(url);
InputStream in=new BufferedInputStream(source.openStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(in));
while((htmlLine=br.readLine())!=null){
htmlSource=htmlSource+htmlLine+linesep;
}

}
catch(java.net.MalformedURLException muex){
JOptionPane.showMessageDialog(WebBrowser.this, muex.toString(),"网页浏览器"
,JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(WebBrowser.this, e.toString(),"网页浏览器",
JOptionPane.ERROR_MESSAGE);
}

}


//实现监听器接口的hyperlinkUpdate函数
public void hyperlinkUpdate(HyperlinkEvent e){
try{
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
jEditorPanel.setPage(e.getURL());
}catch(Exception ex){
ex.printStackTrace(System.err);
}
}



//主函数生成一个IE对象
public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception e){}
WebBrowser webBrowser=new WebBrowser();
webBrowser.pack();
webBrowser.setResizable(true);
webBrowser.setVisible(true);
}
}


SANBENREN 2008-04-29
  • 打赏
  • 举报
回复

/*显示源代码框架*/

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileView;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.util.*;

public class ViewSourceFrame extends JFrame implements ActionListener{

JPanel contentPane;
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
Border border1;


JButton closebutton=new JButton();
JButton savebutton=new JButton();
JScrollPane jScrollpanel=new JScrollPane();
JTextArea jTextAreal=new JTextArea();

String htmlSource;

/*构造函数,初始化图形界面*/
public ViewSourceFrame(String htmlSource){

this.htmlSource=htmlSource;
enableEvents(AWTEvent.WINDOW_FOCUS_EVENT_MASK);
setSize(new Dimension(600,500));
setTitle("源代码");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

contentPane=(JPanel)getContentPane();
contentPane.setLayout(new BorderLayout());

panel2.setLayout(new FlowLayout());

savebutton.setText("保存");
closebutton.setText("退出");

savebutton.addActionListener(this);
closebutton.addActionListener(this);

jScrollpanel.getViewport().add(jTextAreal,null);
border1=BorderFactory.createEmptyBorder(4,4,4,4);
panel1.setLayout(new BorderLayout());
panel1.setBorder(border1);
panel1.add(jScrollpanel,BorderLayout.CENTER);
contentPane.add(panel1,BorderLayout.CENTER);

panel2.add(savebutton);
panel2.add(closebutton);

contentPane.add(panel2,BorderLayout.SOUTH);
this.jTextAreal.setEditable(true);
this.jTextAreal.setText(this.htmlSource);
//设置光标的位置,将其移动到文本区第0个位置
this.jTextAreal.setCaretPosition(0);
}

//实现监听器接口的actionPerformed函数
public void actionPerformed(ActionEvent e){
String url="";
if(e.getSource()==closebutton){
dispose();
}
else if(e.getSource()==savebutton){
JFileChooser fc=new JFileChooser();
int returnVal=fc.showSaveDialog(ViewSourceFrame.this);
File saveFile=fc.getSelectedFile();
try{
FileWriter writeOut=new FileWriter(saveFile);
writeOut.write(jTextAreal.getText());
writeOut.close();
}catch(Exception ex){
PrintWriter pw=new PrintWriter(System.out,true);
pw.println("保存失败");
}
}
}

}
SANBENREN 2008-04-29
  • 打赏
  • 举报
回复
//接上面


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

public void actionPerformed(ActionEvent e){

String url="";
//点击转向按钮
if(e.getSource()==button){
//获得地址栏的内容
url=jurl.getText();

//当url以“http://”开头时
if(url.length()>0&&url.startsWith("http://")){
try{
//JEditorPane组件显示url内容链接
jEditorPanel.setPage(url);
//将url的内容添加到ArrayList对象history中
history.add(url);
//historyIndex的数值设为history对象的长度-1
historyIndex=history.size()-1;
//将界面设置为不可编辑状态
jEditorPanel.setEditable(false);
//重新布局
jEditorPanel.revalidate();
}
catch(Exception ex){
//如果链接显示失败,则弹出对话框“无法打开网页”
JOptionPane.showMessageDialog(WebBrowser.this, "无法打开该网页","网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
}
//url不为"",并且不以"http://"开头
else if(url.length()>0&&!url.startsWith("http://")){
//在url前面添加"http//:"
url=url+"http://";
try{
//JEditorPane组件显示url内容链接
jEditorPanel.setPage(url);
//将url的内容添加到ArrayList对象history中
history.add(url);
//historyIndex的数值设为history对象的长度-1
historyIndex=history.size()-1;
//将界面设置为不可编辑状态
jEditorPanel.setEditable(false);
//重新布局
jEditorPanel.revalidate();
}
catch(Exception ex){
//如果链接显示失败,则弹出对话框“无法打开网页”
JOptionPane.showMessageDialog(WebBrowser.this, "无法打开该网页","网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
}
//没有输入网址
else if(url.length()==0){
JOptionPane.showMessageDialog(WebBrowser.this, "无法打开该网页","网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
}


//输入地址后按回车键
else if(e.getSource()==jurl){
//获得地址栏的内容
url=jurl.getText();

//当url以“http://”开头时
if(url.length()>0&&url.startsWith("http://")){
try{
//JEditorPane组件显示url内容链接
jEditorPanel.setPage(url);
//将url的内容添加到ArrayList对象history中
history.add(url);
//historyIndex的数值设为history对象的长度-1
historyIndex=history.size()-1;
//将界面设置为不可编辑状态
jEditorPanel.setEditable(false);
//重新布局
jEditorPanel.revalidate();
jurl.setMaximumSize(jurl.getPreferredSize());
}
catch(Exception ex){
//如果链接显示失败,则弹出对话框“无法打开网页”
JOptionPane.showMessageDialog(WebBrowser.this, "无法打开该网页","网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
}
//url不为"",并且不以"http://"开头
else if(url.length()>0&&!url.startsWith("http://")){
//在url前面添加"http//:"
url=url+"http://";
try{
//JEditorPane组件显示url内容链接
jEditorPanel.setPage(url);
//将url的内容添加到ArrayList对象history中
history.add(url);
//historyIndex的数值设为history对象的长度-1
historyIndex=history.size()-1;
//将界面设置为不可编辑状态
jEditorPanel.setEditable(false);
//重新布局
jEditorPanel.revalidate();
}
catch(Exception ex){
//如果链接显示失败,则弹出对话框“无法打开网页”
JOptionPane.showMessageDialog(WebBrowser.this, "无法打开该网页","网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
}
//没有输入网址
else if(url.length()==0){
JOptionPane.showMessageDialog(WebBrowser.this, "无法打开该网页","网页浏览器",
JOptionPane.ERROR_MESSAGE);
}
}


//另存为 ...
else if(e.getSource()==picSave ¦ ¦e.getSource()==saveAsItem){
url=jurl.getText().toString().trim();
if(url.length()>0&&!url.startsWith("http://")){
url="http://"+url;
}
if(!url.equals("")){
//保存文本
saveFile(url);
}
else{
JOptionPane.showMessageDialog(WebBrowser.this,
"请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE );
}
}


//退出
else if(e.getSource()==exitItem ¦ ¦e.getSource()==picExit){
System.exit(0);
}

//后退
else if(e.getSource()==backItem ¦ ¦e.getSource()==picBack){
historyIndex--;
if(historyIndex <0)
historyIndex=0;
url=jurl.getText();
try{
//获得history对象中之前访问的地址
url=history.get(historyIndex);
jEditorPanel.setPage(url);
jurl.setText(url.toString());
jEditorPanel.setEditable(false); //设置成非编辑状态
jEditorPanel.revalidate();
}catch(Exception ex){}
}

//前进
else if(e.getSource()==forwardItem ¦ ¦e.getSource()==picForward){
historyIndex++;
if(historyIndex>=history.size())
historyIndex=history.size()-1;
url=jurl.getText();
try{
url=history.get(historyIndex);
jEditorPanel.setPage(url);
jurl.setText(url.toString());
jEditorPanel.setEditable(false);
}catch(Exception ex){}
}


//全屏
else if(e.getSource()==fullscreenItem){
boolean add_button2=true;
//获得全屏大小
Dimension size=Toolkit.getDefaultToolkit().getScreenSize();

Container content=window.getContentPane();
content.add(bar,"North");
content.add(scrollPane,"Center");

//button2为点击全屏之后的还原按钮
if(add_button2==true){
bar.add(button2);
}

//为button2添加事件监听
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
WebBrowser.this.setEnabled(true);
window.remove(bar);
window.remove(toolBar);
window.remove(scrollPane);
window.setVisible(false);

scrollPane.setPreferredSize(new Dimension(500,400));
getContentPane().add(scrollPane,BorderLayout.SOUTH);
getContentPane().add(bar,BorderLayout.CENTER);
getContentPane().add(toolBar,BorderLayout.NORTH);
bar.remove(button2);
pack();
}
});
window.setSize(size);
window.setVisible(true);
}

//查看源文件
else if(e.getSource()==sourceItem ¦ ¦e.getSource()==picView){
url=jurl.getText().toString().trim();
if(url.length()>0&&url.startsWith("http://")){
url="http://"+url;
}
if(!url.equals("")){
//根据url,获得源代码
getHtmlSource(url);
//生成显示原代码的框架对象
ViewSourceFrame vsframe=new ViewSourceFrame(htmlSource);
vsframe.setBounds(0,0,800,500);
vsframe.setVisible(true);
}
else{
JOptionPane.showMessageDialog(WebBrowser.this,
"请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
}
}


//刷新
else if(e.getSource()==reloadItem ¦ ¦e.getSource()==picReload){
url=jurl.getText();
if(url.length()>0&&url.startsWith("http://")){
try{
jEditorPanel.setPage(url);
jEditorPanel.setEditable(false); //设置成非编辑状态
jEditorPanel.revalidate();
}catch(Exception ex){}
}
}

}
dgqbcht 2008-04-29
  • 打赏
  • 举报
回复
swing里自己就有browser控件的……
SANBENREN 2008-04-29
  • 打赏
  • 举报
回复
谢谢大5,6楼对本人的支持,问题解决了,原来我把"http://"加在了url的后面!!谢过
SANBENREN 2008-04-29
  • 打赏
  • 举报
回复
而且我页加了这句 else if url=url+"http://" 啊!!
SANBENREN 2008-04-29
  • 打赏
  • 举报
回复
而且我页加了这句 else if url=url+"http://" 啊!!
SANBENREN 2008-04-29
  • 打赏
  • 举报
回复
可是我的程序的本意是加不加都可啊!!

虽然是个小问题,可对我这么爱较真的人来说真是一种折磨!!
q1531 2008-04-29
  • 打赏
  • 举报
回复
LZ你的程序我测试可以用的。不过查看源代码那里有点问题要修改一下。

地址那里必须加个http:// 才能浏览。。
http://hiphotos.baidu.com/opsun/pic/item/f25a4563fd6ea7720c33fad4.jpg
bt_lose 2008-04-29
  • 打赏
  • 举报
回复
lz辛苦了,不过一定要坚持,只有坚持到最后,才能胜利

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧