当前位置: 代码迷 >> Java相关 >> 超链接问题
  详细解决方案

超链接问题

热度:215   发布时间:2012-04-08 22:40:56.0
超链接问题
为什么下面的程序不能打开超链接
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.JEditorPane;
import javax.swing.*;
import javax.swing.event.*;
public class Example11_2 {
    public static  void main(String args[])
    {
        new Win();
    }

}
class  Win extends JFrame implements ActionListener,Runnable
{
    Button button;
    URL url;
    TextField text;
    JEditorPane editPane;
    byte b[]=new byte[118];
    Thread thread;
    public Win()
    {
        text=new TextField(20);
        editPane=new JEditorPane();
        editPane.setEditable(false);
        button=new Button("确定");
        button.addActionListener(this);
        thread=new Thread(this);
        Panel p=new Panel();
        p.add(new Label("输入网址:"));
        p.add(text);
        p.add(button);
        ScrollPane scroll=new ScrollPane();
        scroll.add(editPane);
        add(scroll,BorderLayout.CENTER);
        add(p,BorderLayout.NORTH);
        setBounds(160,60,360,300);
        setVisible(true);
        validate();
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       editPane.addHyperlinkListener(new HyperlinkListener()
       {
           public void hyperlinkUpdate(HyperlinkEvent e)
           {
              if(e.getSource()==HyperlinkEvent.EventType.ENTERED)
               {
                   try{
                     
                       editPane.setPage(e.getURL());
                     
                   }
                   catch(IOException e2){
                       System.out.println(e2);
                   }
               }
           }


       });



    }
    public void actionPerformed(ActionEvent e)
    {
        if(!(thread.isAlive()))
        {
            thread=new Thread(this);

        }
        try{
            thread.start();
        }
        catch(Exception ee){}
        }

    public void run()
    {
        try{
            editPane.setText(null);
            url=new URL("http://"+text.getText().trim());
            editPane.setPage(url);
        }
        catch(Exception e1){
            return;
        }
    }
}


搜索更多相关的解决方案: void  thread  public  button  import  

----------------解决方案--------------------------------------------------------
我试了下,可以打开啊
----------------解决方案--------------------------------------------------------
  相关解决方案