// HtmlBrowserimport java.io.IOException;
import java.net.URL;
import java.util.*;
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import javax.swing.event.*;public class HtmlBrowser extends JFrame {
JPanel contenPane;//包括整个框架的容器 BorderLayout borderLayoutALL = new BorderLayout(); JLabel jLabelPrompt = new JLabel();//状态提示栏 JPanel jPanelMain = new JPanel(); BorderLayout borderLayoutMain = new BorderLayout(); JTextField textFieldURL = new JTextField(); JEditorPane jEditorPane = new JEditorPane(); public HtmlBrowser() {
try {
jbInit();//初始化并显示界面
} catch (Exception e) {
e.printStackTrace();
}
} private void jbInit() throws Exception {
contenPane = (JPanel) getContentPane();
contenPane.setLayout(borderLayoutALL);
jPanelMain.setLayout(borderLayoutMain);
jLabelPrompt.setText("请输入URL");
textFieldURL.setText(" ");//清空文本框
textFieldURL.addActionListener(new java.awt.event.ActionListener() {//类必须实现继承的抽象方法 ActionListener.actionPerformed(ActionEvent)
public void actionPerformed (ActiveEvent 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");
contenPane.add(jLabelPrompt, "North");
contenPane.add(jPanelMain, "Center");
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
this.setSize(new Dimension(600, 500));
this.setTitle("Archy_IE Beta1");
this.setVisible(true);
} void textFieldURL_actionPerformed(ActiveEvent e) {
try {
jEditorPane.setPage(textFieldURL.getText());
} catch (IOException ex) {
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this, "  URL ERROR : "
+ textFieldURL.getText(), "Input ERROR", 0);
}
}void jEditorPane_hyperlinkUpdate(HyperlinkEvent e){
if (e.getEventType()
javax.swing.event.HyperlinkEvent.EventType.ACTIVATED){//报错说javax有语法错误
try{
URL url = e.getURL();
jEditorPane.setPage(url);
textFieldURL.setText(url.toString());
}catch(IOException io){
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"Open the URL Failed!","Input Error!",0);
}
}
} protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} public static void main(String[] args) {
new HtmlBrowser();
}
}
刚学Java1个月,实在不懂,老师降到循环,呵呵~
但小弟闲着没啥事敲敲代码,熟悉下语句~
红色是错误所在~
编译器是Eclipse 3.0
JDK是1.5

解决方案 »

  1.   

    你的是:
    if (e.getEventType()
    javax.swing.event.HyperlinkEvent.EventType.ACTIVATED)
    在e.getEventType()后面少了一个符号吧,应该是==吧
      

  2.   

    这代码是书上的么?楼主自己写错了吧...textFieldURL.addActionListener(new java.awt.event.ActionListener() {//类必须实现继承的抽象方法 ActionListener.actionPerformed(ActionEvent) //问题是不是出在这儿啊?因为你没用把这句注释掉...
    public void actionPerformed (ActiveEvent e) { 
    textFieldURL_actionPerformed(e); 
    ActionEvent