📄 webbrowser.java
字号:
//package com.javaweb.util;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
/**
* @author Administrator
*
*/
public class WebBrowser extends JFrame implements HyperlinkListener,
PropertyChangeListener {
/**
*
*/
// 添加swing组件
JEditorPane textPane;
JLabel messageLine;
JTextField urlField;
JFileChooser fileChooser;
JButton backButton;
JButton forwardButton;
//
java.util.List history = new ArrayList();
int currentHistoryPage = -1;
public static final int MAX_HISTORY = 50;
static int numBrowserWindows = 0;
static boolean exitWhenLastWindowClosed = false;
String home = "http://www.baidu.com";
// gouzaohanshu
public WebBrowser() {
super("WebBrowser");
textPane = new JEditorPane();
textPane.setEditable(false);
textPane.addPropertyChangeListener(this);
textPane.addHyperlinkListener(this);
this.getContentPane().add(new JScrollPane(textPane),
BorderLayout.CENTER);
messageLine = new JLabel(" ");
this.getContentPane().add(messageLine, BorderLayout.SOUTH);
this.initMenu();
this.initToolbar();
WebBrowser.numBrowserWindows++;
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// close();
}
});
}
private void initToolbar() {
// TODO Auto-generated method stub
backButton = new JButton("后退");
backButton.setEnabled(false);
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
back();
}
});
forwardButton = new JButton("前进");
forwardButton.setEnabled(false);
forwardButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
forward();
}
private void forward() {
// TODO Auto-generated method stub
if (currentHistoryPage < history.size() - 1) {
visit((URL) history.get(++currentHistoryPage));
}
backButton.setEnabled((currentHistoryPage > 0));
forwardButton
.setEnabled((currentHistoryPage < history.size() - 1));
}
});
JButton refreshButton = new JButton("刷新");
refreshButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
reload();
}
private void reload() {
// TODO Auto-generated method stub
// TODO Auto-generated method
textPane.setDocument(new javax.swing.text.html.HTMLDocument());
visit((URL) history.get(currentHistoryPage));
}
});
JButton homeButton = new JButton("主页");
homeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
home();
}
public void home() {
// TODO Auto-generated method stub
displayPage(getHome());
}
});
urlField = new JTextField();
urlField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
displayPage(urlField.getText());
}
});
JToolBar toolbar = new JToolBar();
toolbar.setEnabled(false);
toolbar.add(backButton);
toolbar.add(forwardButton);
toolbar.add(refreshButton);
toolbar.add(homeButton);
toolbar.add(new JLabel(" 地址:"));
toolbar.add(urlField);
this.getContentPane().add(toolbar, BorderLayout.NORTH);
}
protected String getHome() {
// TODO Auto-generated method stub
return home;
}
protected void back() {
// TODO Auto-generated method stub
if (currentHistoryPage > 0) {
visit((URL) history.get(--currentHistoryPage));
}
backButton.setEnabled((currentHistoryPage > 0));
forwardButton.setEnabled((currentHistoryPage < history.size() - 1));
}
public static void setExitWhenLastWindowClosed(boolean b) {
exitWhenLastWindowClosed = b;
}
public void setHome() {
this.home = home;
}
public String getHone() {
return home;
}
private boolean visit(URL url) {
try {
String herf = url.toString();
startAnimation("加载" + herf + "...");
textPane.setPage(url);
this.setTitle(herf);
urlField.setText(herf);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
stopAnimation();
messageLine.setText("不能打开页面" + e.getMessage());
return false;
}
}
public void displayPage(URL url) {
if (visit(url)) {
history.add(url);
int numentries = history.size();
if (numentries > MAX_HISTORY + 10) {
history = history.subList(numentries - MAX_HISTORY, numentries);
numentries = MAX_HISTORY;
}
currentHistoryPage = numentries - 1;
if (currentHistoryPage > 0) {
backButton.setEnabled(true);
}
}
}
public void displayPage(String href) {
try {
if (!href.startsWith("http://")) {
href = "http://" + href;
}
displayPage(new URL(href));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
messageLine.setText("错误网址:" + href);
}
}
public void openLocalPage() {
if (fileChooser == null) {
fileChooser = new JFileChooser();
FileFilter filter = new FileFilter() {
public boolean accept(File f) {
String fn = f.getName();
if (fn.endsWith(".html") || fn.endsWith(".htm")) {
return true;
} else {
return false;
}
}
public String getDescription() {
return "HTML Files";
}
};
fileChooser.addChoosableFileFilter((FileFilter) filter);
}
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
displayPage(selectedFile.toURL());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void stopAnimation() {
// TODO Auto-generated method stub
}
String animationMessage;
int animationFrame = 0;
private void startAnimation(String msg) {
// TODO Auto-generated method stub
animationMessage = msg;
animationFrame = 0;
}
private void initMenu() {
// TODO Auto-generated method stub
JMenu fileMenu = new JMenu("文件");
fileMenu.setMnemonic('F');
JMenuItem newMenuitem = new JMenuItem("新建");
newMenuitem.setMnemonic('N');
newMenuitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
newBrowser();
}
private void newBrowser() {
// TODO Auto-generated method stub
WebBrowser b = new WebBrowser();
/* b.setSize(this.getWidth(),this.getHeight()); */
b.setSize(800, 600);
b.setVisible(true);
}
});
JMenuItem openMenuitem = new JMenuItem("打开");
openMenuitem.setMnemonic('O');
openMenuitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
// openLocalPage();
}
});
JMenuItem closeMenuitem = new JMenuItem("关闭窗口");
closeMenuitem.setMnemonic('C');
closeMenuitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
close();
}
public void close() {
// TODO Auto-generated method stub
// this.setVisible(false);
// this.disPose();
}
});
JMenuItem exitMenuitem = new JMenuItem("退出");
exitMenuitem.setMnemonic('E');
exitMenuitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
exit();
}
});
fileMenu.add(newMenuitem);
fileMenu.add(openMenuitem);
fileMenu.add(closeMenuitem);
fileMenu.add(exitMenuitem);
JMenu helpMenu = new JMenu("帮助");
JMenuItem aboutMenuitem = new JMenuItem("关于");
helpMenu.setMnemonic('H');
aboutMenuitem.setMnemonic('A');
helpMenu.add(aboutMenuitem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(helpMenu);
this.setJMenuBar(menuBar);
}
public void hyperlinkUpdate(HyperlinkEvent e) {
HyperlinkEvent.EventType type = e.getEventType();
if (type == HyperlinkEvent.EventType.ACTIVATED) {
displayPage(e.getURL());
} else if (type == HyperlinkEvent.EventType.ENTERED) {
messageLine.setText(e.getURL().toString());
} else if (type == HyperlinkEvent.EventType.EXITED) {
messageLine.setText("");
}
// TODO Auto-generated method stub
}
public void exit() {
// TODO Auto-generated method stub
if(JOptionPane.showConfirmDialog(this, "您确定要退出浏览器?", "退出",
JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
System.exit(0);
// JOptionPane.YES_OPTION)
}
public void propertyChange(PropertyChangeEvent arg0) {
// TODO Auto-generated method stub
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
WebBrowser.setExitWhenLastWindowClosed(true);
// 添加了窗口的位置、大小。
WebBrowser browser = new WebBrowser();
browser.setSize(800, 600);
browser.setLocation(100, 100);
browser.setVisible(true);
browser.displayPage(browser.getHome());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -