2012年6月5日 星期二

Intermediate Java(36) – loadCrap


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;

public class Read extends JFrame{
private JTextField addressBar;
private JEditorPane display;
//constructor
public Read(){
super("New Browser");
addressBar = new JTextField("enter a URL!");
addressBar.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
loadCrap(event.getActionCommand());
}
}
);
add(addressBar, BorderLayout.NORTH);
display = new JEditorPane();
display.setEditable(false);
display.addHyperlinkListener(
new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent event){
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
loadCrap(event.getURL().toString());
}
}
}
);
add(new JScrollPane(display),BorderLayout.CENTER);
setSize(500,300);
setVisible(true);
}
//加載在屏幕上顯示
private void loadCrap(String userText){
try{
display.setPage(userText); → setPageURL的所有內容display
addressBar.setText(userText);
}catch(Exception e){
System.out.println("Error!");
}
}

}





創建主類稱為ReadMain.java:

import javax.swing.JFrame;

public class ReadMain {

public static void main(String[] args) {
Read dude = new Read();
dude.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

沒有留言:

張貼留言