2011年3月26日
<Java> JVA307-簡易瀏覽器
這真的是一個很簡易的瀏覽器...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.io.*;
import java.net.*;
public class JVA307 extends JFrame implements HyperlinkListener {
JTextField jURL = new JTextField();
JEditorPane htmlPane = new JEditorPane();
// Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch(Exception e) {
e.printStackTrace();
}
new JVA307();
}
// 建構函式
public JVA307() {
super("簡易Web Browser瀏覽器");
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JLabel jlabel1 = new JLabel();
jlabel1.setText("URL:");
//////////////////////////////////////////////////////////
jURL.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String url = jURL.getText();
showHTML(url);
}
});
////////////////////////////////////////////////////////
// JEditorPane
htmlPane.setEditable(false);
htmlPane.addHyperlinkListener(this);
//////////////////////////////////////////////////////////
JPanel jpanel1 = new JPanel();
jpanel1.setLayout(new BorderLayout());
jpanel1.setPreferredSize(new Dimension(10, 35));
JPanel jpanel2 = new JPanel();
jpanel2.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
jpanel2.setPreferredSize(new Dimension(35, 10));
JPanel jpanel3 = new JPanel();
jpanel3.setLayout(new BorderLayout());
jpanel3.setBorder(BorderFactory.createEmptyBorder(5,0,5,5));
jpanel2.add(jlabel1);
jpanel1.add(jpanel2, BorderLayout.WEST);
jpanel3.add(jURL);
jpanel1.add(jpanel3, BorderLayout.CENTER);
contentPane.add(jpanel1, BorderLayout.NORTH);
///////////////////////////////////////////////////
// 捲軸
JScrollPane jscrollpane = new JScrollPane(htmlPane,20,30);
contentPane.add(jscrollpane,BorderLayout.CENTER);
///////////////////////////////////////////////////
// Center the frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
this.setLocation((screenSize.width - frameSize.width) 2, (screenSize.height - frameSize.height) 2);
this.validate();
this.setSize(new Dimension(250, 250));
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// 處理超連結事件
void showHTML(String url) {
try {
if (url.length() > 0) {
if (!url.startsWith("http://") && !url.startsWith("file:/")) {
if (url.indexOf(':') == 1) {
url = "file:/" + url;
}
else {
url = "http://" + url;
}
}
//////////////////////////////
URL url1 = new URL(url);
htmlPane.setPage(url1); //
jURL.setText(url);
jURL.select(0,url.length());
/////////////////////////////
}
}
catch (java.net.MalformedURLException muex) {
JOptionPane.showMessageDialog(this, muex.toString() + ": " + url,
"Error", JOptionPane.ERROR_MESSAGE);
}
catch (Exception ex){
JOptionPane.showMessageDialog(this, ex.toString() + ": " + url,
"Error", JOptionPane.ERROR_MESSAGE);
}
}
////////////////////////////////////////////////
public void hyperlinkUpdate(HyperlinkEvent he){
if(he.getEventType() == HyperlinkEvent.EventType.ACTIVATED){ //
String s = he.getURL().toString().trim();
showHTML(s);
}
}
//////////////////////////////////////////////////
}
相關物件說明:
URL
Dimension
參考網站:
Java A+
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言