我要实现的功能是:
1,在菜单栏中有:文件(新建、打开、另存为、退出);编辑(复制、剪切、粘贴);设置(文字颜色、背景颜色);帮助(帮助)。
2.工具栏
3.窗口
现在我我的文件中的新建、打开、另存为;编辑;和设置没有完成 望高手帮忙~!还有我的浏览器现在不能浏览本地HTML文件~1 我的邮箱是:qianwugang1982@163.com 大家可以把源代码发到我的邮箱中,分一样给!
源代码:
第一个Show.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Show
{
public static void main(String[] args)
{
BrowserFrame bf = new BrowserFrame();
bf.setVisible(true);
}
}
第二个BrowserFrame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class BrowserFrame extends JFrame{
private WebWindow ww = new WebWindow(this);
private MenuBar mb = new MenuBar(this);
private ToolBar tb = new ToolBar(this);
private HistoryList hl = new HistoryList();
private String sURL;
public BrowserFrame(){
setTitle( "Show ");
setSize(640, 480);
setLocation(20, 20);
getContentPane().setLayout(new BorderLayout());
setJMenuBar(mb);
JPanel jpToolMenu = new JPanel();
jpToolMenu.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
jpToolMenu.add(tb);
getContentPane().add(jpToolMenu, BorderLayout.NORTH);
getContentPane().add(ww, BorderLayout.CENTER);
setCurrentURL( "http://www.163.com ");}
public void setToolBarURL(String s) {
tb.setTextField(s);}
public void goBack() {
try {
sURL = hl.getLast();
ww.setCurrentURL(sURL);
setToolBarURL(sURL);
}catch(Exception e) {new PopupDialog( "错误! ", e.getMessage());}
}
public void goForward() {
try {
sURL = hl.getNext();
ww.setCurrentURL(sURL);
setToolBarURL(sURL);
}catch(Exception e) {new PopupDialog( "错误 ", e.getMessage());}
}
public void refreshURL() {
try {
ww.setCurrentURL(sURL);
setToolBarURL(sURL);
}catch(Exception e) {new PopupDialog( "错误! ", e.getMessage());}
}
public String getCurrentURL() {
return sURL;}
public void setCurrentURL(String current) {
if (!(current.substring(0, 7)).equals( "http:// ")) {
if(!(current.substring(0, 3)).equals( "www "));
current = "www. "+current;current = "http:// "+current;}
sURL = current;
try {
ww.setCurrentURL(sURL);
hl.add(sURL);
setToolBarURL(sURL);
}catch(Exception e) {new PopupDialog( "错误! ", e.getMessage());}
}
}
第三个:TOOLBar.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ToolBar extends JToolBar implements ActionListener{
private JButton jbBack = new JButton(new ImageIcon( "./Back16.gif "));
private JButton jbForward = new JButton(new ImageIcon( "./Forward16.gif "));
private JButton jbRefresh = new JButton(new ImageIcon( "./Refresh16.gif "));
private JButton jbGo = new JButton( "Go ");
private JButton jbShogun = new JButton(new ImageIcon( "./s.gif "));
private JTextField jtfLocation = new JTextField(20);
private BrowserFrame bfRef;
public ToolBar() {
this(new BrowserFrame());
}
public ToolBar(BrowserFrame bf) {
bfRef = bf;
setName( "Toolbar ");
setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
jbBack.setToolTipText( "Back ");
jbForward.setToolTipText( "Forward ");
jbRefresh.setToolTipText( "Refresh ");
jbGo.setToolTipText( "Go ");
jbShogun.setToolTipText( "Homepage ");
this.add(jbBack);
this.add(jbForward);
this.add(jbRefresh);
this.add(new JLabel( "Location: "));
this.add(jtfLocation);
this.add(jbGo);
this.add(new JLabel( " "));
this.add(jbShogun);
jbBack.addActionListener(this);
jbForward.addActionListener(this);
jbRefresh.addActionListener(this);
jtfLocation.addActionListener(this);
jbGo.addActionListener(this);
jbShogun.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if ((e.getSource() == jbGo) || (e.getSource() == jtfLocation))
bfRef.setCurrentURL(jtfLocation.getText());
else if(e.getSource() == jbRefresh)
bfRef.refreshURL();
else if(e.getSource() == jbBack)
bfRef.goBack();
else if (e.getSource() == jbForward)
bfRef.goForward();
else if (e.getSource() == jbShogun)
bfRef.setCurrentURL( "http://www.163.com ");
}
public void setTextField(String s) {
jtfLocation.setText(s);
}
}
第四个:MenuBar.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuBar extends JMenuBar implements ActionListener, ItemListener{
private JMenu jmFile = new JMenu( "文件 ");
private JMenu jmEdit = new JMenu( "编辑 ");
private JMenu jmSet = new JMenu( "设置 ");
private JMenu jmBookmarks = new JMenu( "标签 ");
private JMenu jmHelp = new JMenu( "帮助 ");
private JMenuItem jmiNew =new JMenuItem( "新建 ");
private JMenuItem jmiOpen =new JMenuItem( "打开 ");
private JMenuItem jmiSaveas =new JMenuItem( "另存为 ");
private JMenuItem jmiExit = new JMenuItem( "退出 ");
private JMenuItem jmiCopy = new JMenuItem( "复制 ");
private JMenuItem jmiCut = new JMenuItem( "剪切 ");
private JMenuItem jmiPaste = new JMenuItem( "粘贴 ");
private JMenuItem jmiFcolor = new JMenuItem( "文字颜色 ");
private JMenuItem jmiBcolor = new JMenuItem( "背景颜色 ");
private JMenuItem jmiAddBookmark = new JMenuItem( "加入标签 ");
private JCheckBoxMenuItem jmiRemoveBookmark = new JCheckBoxMenuItem( "移除标签 ");
private JMenuItem jmiAbout = new JMenuItem( "帮助 ");
private boolean bRemove = false;
private BrowserFrame bfRef;
private BookmarkList bl = new BookmarkList();
public MenuBar() {
this(new BrowserFrame());
}
public MenuBar(BrowserFrame bf) {
bfRef = bf;
jmFile.add(jmiNEW);
jmFile.add(jmiOpen);
jmFile.add(jmiSaveas);
jmFile.add(jmiExit);
jmEdit.add(jmiCopy);
jmEdit.add(jmiCut);
jmEdit.add(jmiPaste);
jmSet.add(jmiFcolor);
jmSet.add(jmiBcolor);
jmBookmarks.add(jmiAddBookmark);
jmBookmarks.add(jmiRemoveBookmark);
jmHelp.add(jmiAbout);
jmBookmarks.addSeparator();
add(jmFile);
add(jmEdit);
add(jmSet);
add(jmBookmarks);
add(jmHelp);
jmiNew.addActionListener(this);
jmiOpen.addActionListener(this);
jmiSaveas.addActionListener(this);
jmiExit.addActionListener(this);
jmiCopy.addActionListener(this);
jmiCut.addActionListener(this);
jmiPaste.addActionListener(this);
jmiFcolor.addActionListener(this);
jmiBcolor.addActionListener(this);
jmiAbout.addActionListener(this);
jmiAddBookmark.addActionListener(this);
jmiRemoveBookmark.addItemListener(this);
addBookmarks();
}
public void addBookmarks() {
for(int x = 0; x < bl.getSize(); x++) {
JMenuItem jmiBM = new JMenuItem(bl.returnURL(x));
jmBookmarks.add(jmiBM);
jmiBM.addActionListener(this);}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jmiExit)
System.exit(0);
else if (e.getSource() == jmiAbout)
new PopupDialog( "Help? ", "Help? 自己动手,丰衣足食!!^_^ ");
else if (e.getSource() == jmiAddBookmark) {
bl.add(bfRef.getCurrentURL());
addBookmarkItem(bfRef.getCurrentURL());
}
else {
if (!bRemove)
bfRef.setCurrentURL(e.getActionCommand());
else {
jmBookmarks.remove((JMenuItem) e.getSource());
try {
bl.remove(e.getActionCommand());
}catch(Exception ex) {new PopupDialog( "错误 ", "移除标签出错! ");}
}
}
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == jmiRemoveBookmark)
bRemove = !bRemove;
}
public void addBookmarkItem(String s) {
JMenuItem newItem = new JMenuItem(s);
newItem.addActionListener(this);
jmBookmarks.add(newItem);
}
}
第五个WebNode.java
public class WebNode {
private String site;
private WebNode next;
private WebNode prev;
public WebNode() {
site = null;
next = null;
prev = null;
}
public WebNode(String addr) {
site = addr;
next = null;
prev = null;
}
public void setURL(String url) {
site = url;}
public String getURL() {
return site;
}
public void setNext(WebNode node) {
next = node;
}
public WebNode getNext() {
return next;
}
public void setPrev(WebNode node) {
prev = node;
}
public WebNode getPrev() {
return prev;
}
}
第六个PopuPDialog.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PopupDialog extends JDialog implements ActionListener{
public PopupDialog(String title, String text) {
super( new Frame( " "), title, true );
setSize(300,100);
setResizable( false );
setLocation (240, 240);
JLabel message = new JLabel(text, JLabel.CENTER);
getContentPane().add( message, BorderLayout.CENTER );
JButton close = new JButton( " 关闭 " );
JPanel p = new JPanel();
close.addActionListener( this );
p.add(close);
getContentPane().add( p, BorderLayout.SOUTH );
setVisible(true);}
public void actionPerformed( ActionEvent e ){
setVisible( false );}
}
第七个List.java
public abstract class List {
protected WebNode head;
protected int size = 0;
public List() {
head = null;}
public void remove(String s) throws Exception {}
public void add(String url) {
WebNode placeHolder = new WebNode();
placeHolder.setURL(url);
placeHolder.setNext(head);
head=placeHolder;
size++;}
public String getName(int i) throws Exception{
WebNode wn = head;
if (isEmpty())
throw new Exception( "记录为空 ");
for (int x = 0; x < i; x++) {
wn = wn.getNext();
}
return wn.getURL();
}
public int getSize() {return size;}
public boolean isEmpty() { return (head == null); }
}
第八个WebWindow.java
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
import java.awt.*;
import java.io.*;
public class WebWindow extends JScrollPane implements HyperlinkListener{
private JEditorPane je = new JEditorPane();
private BrowserFrame bfRef;
public WebWindow() {
this(new BrowserFrame());}
public WebWindow(BrowserFrame bf) {
bfRef = bf;
je.setEditable(false);
setViewportView(je);
setAutoscrolls(false);}
public void setCurrentURL(String sURL) throws Exception {
try {
je = new JEditorPane(sURL);
je.setEditable(false);
setViewportView(je);
je.addHyperlinkListener(this);
}
catch(Exception e) {throw new Exception( "有些小错误!去不了你想去的地方!^_^ ");
}
}
public void hyperlinkUpdate(HyperlinkEvent evt) {
if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
bfRef.setCurrentURL(evt.getURL().toString());
}
}
}
第九个 HistoryList.java
public class HistoryList extends List{
private WebNode tail;
private WebNode current;
public HistoryList() {
super();
tail = null;
}
public void add(String url) {
super.add(url);
if (size == 1) {
tail = head;
current = head;
}
else {
head.setPrev(current);
current.setNext(head);
current = head;
head.setNext(null);
}
}
public String getLast() throws Exception{
if(current.getPrev() != null)
current = current.getPrev();
else
throw new Exception( "无法后退! ");
return current.getURL();
}
public String getNext() throws Exception{
if(current.getNext() != null)
current = current.getNext();
else
throw new Exception( "不能前进! ");
return current.getURL();
}
}
第十个BookmarkList.java
import java.util.*;
import java.io.*;
public class BookmarkList extends List {
public static final String BFILE = "bookmark.lis ";
public BookmarkList() {
super();
readIntoList();
}
public void add(String s) {
super.add(s);
writeToFile();}
public void startupAdd(String s){
super.add(s);
}
public void remove(String s) throws Exception {
WebNode holder = head;
boolean isFound = false;
if(isEmpty())
throw new Exception( "记录为空 ");
else if ((holder.getURL()).equals(s)){
head = holder.getNext();
size--;
writeToFile();
isFound = true;
}
else {
while ((holder != null) && (!isFound)){
if ((holder.getNext().getURL()).equals(s)) {
holder.setNext(holder.getNext().getNext());
isFound = true;
size--;
writeToFile();
}
else{
holder = holder.getNext();
}
}
}
if (!isFound) {
throw new Exception( "No match found! ");
}
}
private void writeToFile() {
try {
FileOutputStream fOut = new FileOutputStream(BFILE);
for (WebNode x = head; x != null; x = x.getNext()) {
fOut.write(x.getURL().getBytes());
fOut.write( '\n ');
}
fOut.close();
}catch (Exception e) {
new PopupDialog( "文件错误 ", "不能写入文件 ");
}
}
private void readIntoList() {
try {
FileInputStream fIn = new FileInputStream(new File(BFILE));
byte bt[] = new byte[(int) (new File(BFILE)).length()];
fIn.read(bt);
String s = new String(bt);
StringTokenizer st = new StringTokenizer(s, "\n ");
while(st.hasMoreTokens())
startupAdd(st.nextToken());
fIn.close();}catch(Exception e) { }
}
public String returnURL(int loc) {
WebNode wn = head;
for (int x = 0; x < loc; x++)
wn = wn.getNext();
return wn.getURL();
}
}
在编译第二个和第四个文件时提示出错。。
.\MenuBar.java:30: cannot find symbol
symbol : variable jmiNEW
location: class MenuBar
jmFile.add(jmiNEW);
^
1 error
学习中。。
牛人啊,为啥和我写一样的东西啊?
保存的给你写完了,告诉我怎么显示本地的htlm
注意:要显示页面,不是代码。
public void saveFile()
{
try
{
file = new File(currentFileName);
writefile = new FileWriter(file);
String s=paneURL.getText();
writefile.write(s);
writefile.close();
}
catch(IOException ee)
{
JOptionPane.showMessageDialog(this, "Warning ",ee.toString(),
JOptionPane.ERROR_MESSAGE);
}
}
public void saveAsFile()
{
int n=saveAsFileDialog.showSaveDialog(this);
file = saveAsFileDialog.getSelectedFile();
if(n==JFileChooser.APPROVE_OPTION)
{
try
{
currentFileName = saveAsFileDialog.getSelectedFile().getPath();
writefile=new FileWriter(file);
write=new BufferedWriter(writefile);
String s=paneURL.getText();
write.write(s);
write.newLine();
write.flush();
write.close();
writefile.close();
this.setTitle(currentFileName);
}
catch(IOException ee)
{
JOptionPane.showMessageDialog(this, "Warning ",ee.toString(),
JOptionPane.ERROR_MESSAGE);
}
}
}
啊哈哈,我简直就是一个白痴,现成的东西不会用
public void openFile()
{
int n=openFileDialog.showOpenDialog(this);
if(n==JFileChooser.APPROVE_OPTION)
{
paneURL.setText(null);
file=openFileDialog.getSelectedFile();
try
{
currentFileName = openFileDialog.getSelectedFile().getPath();
url = file.toURL(); //这个是最关键的,要把File类型的file变成URL型的。
in = url.openStream();
paneURL.setPage(url);
stringText = paneURL.getText().trim();
}
catch(IOException ee)
{
paneURL.setText(currentFileName);
JOptionPane.showMessageDialog(this, "Opening Error !! ",ee.toString(),
JOptionPane.WARNING_MESSAGE);
}
}
}