?? htmlbrowser.java
字號:
import java.io.IOException;
import java.net.URL;
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class HtmlBrowser extends JFrame {
JPanel contentPane; // 包含整個框架的容器
BorderLayout borderLayoutAll = new BorderLayout();
JLabel jLabelPrompt = new JLabel(); // 狀態提示框
JPanel jPanelMain = new JPanel();
BorderLayout borderLayoutMain = new BorderLayout();
JTextField textFieldURL = new JTextField(); // URL輸入框
JEditorPane jEditorPane = new JEditorPane(); // 顯示網頁內容的容器
public HtmlBrowser() { // 定義構造方法
try {
jbInit(); // 初始化并顯示界面
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception { // 界面初始化
contentPane = (JPanel)getContentPane();
contentPane.setLayout(borderLayoutAll);
jPanelMain.setLayout(borderLayoutMain);
jLabelPrompt.setText("請輸入URL");
textFieldURL.setText(""); // 清空文本框
textFieldURL.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
textFieldURL_actionPerformed(e); }
});
jEditorPane.setEditable(false); // 設置不可編輯
jEditorPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
jEditorPane_hyperlinkUpdate(e);
}
});
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(jEditorPane);
jPanelMain.add(textFieldURL, "North");
jPanelMain.add(scrollPane, "Center");
contentPane.add(jLabelPrompt, "North");
contentPane.add(jPanelMain, "Center");
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
this.setSize(new Dimension(600, 500));
this.setTitle("迷你IE ");
this.setVisible(true);
}
void textFieldURL_actionPerformed(ActionEvent e) { // 輸入地址后響應回車
try {
jEditorPane.setPage(textFieldURL.getText()); // 顯示URL
}
catch(IOException ex) {
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this, "URL地址不正確:"+textFieldURL.getText(), "輸入不正確!", 0);
}
}
void jEditorPane_hyperlinkUpdate(HyperlinkEvent e) { // 響應頁面打開超鏈接消息
if(e.getEventType() == javax.swing.event.HyperlinkEvent.EventType.ACTIVATED) {
try {
URL url = e.getURL(); // 從消息中得到URL
jEditorPane.setPage(url); // 顯示頁面內容
textFieldURL.setText(url.toString()); // 顯示URL
}
catch(IOException io){
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this, "打開該鏈接失敗!", "輸入不正確!", 0);
}
}
}
protected void processWindowEvent(WindowEvent e) { //處理窗體事件
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0); // 關閉
}
}
public static void main(String[] args) { // Main函數
new HtmlBrowser();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -