Bonjour.
J'ai developpé un petit navigateur web en java, sauf que je n'arrive pas à gérer les liens. Voici mon code:
Citation :
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.lang.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
public class Connec1 implements ActionListener, MouseListener
{
JTextField tf1,tf2;
JButton b1,b2,bQ;
JFrame f;
JPanel p,p1;
JLabel l1,l2;
JTextArea txt1;
JEditorPane html;
String path = "
http://www.google.fr/";
public Connec1()
{
f = new JFrame("Affiche");
p = new JPanel();
p1=new JPanel();
tf1 = new JTextField(30);
tf2 = new JTextField(10);
b1 = new JButton("Envoyer");
b2 = new JButton("Effacer");
bQ = new JButton("Quitter");
l1 = new JLabel("URL:");
l2 = new JLabel("port:");
txt1 = new JTextArea(20,100);
f.add(p);
p.add(l1);
p.add(tf1);
p.add(l2);
p.add(tf2);
p.add(b1);
p.add(b2);
p.add(bQ);
f.setSize(1000,550);
p1.setSize(900,400);
f.show();
b1.addActionListener(this);
b2.addActionListener(this);
bQ.addActionListener(this);
p.addMouseListener(this);
}
public static void main(String toto[])
{
new Connec1();
}
public void actionPerformed(ActionEvent toto)
{
if (toto.getSource()==bQ)
{
System.exit(0);
}
else if (toto.getSource()==b1)
{
try
{
URL url1=new URL(tf1.getText());
DataInputStream dis = new DataInputStream(url1.openStream());
String inputLine;
JEditorPane html = new JEditorPane( url1 );
html.setEditable(false); // empèche de pouvoir modifier des choses sur la page.
html.addHyperlinkListener( createHyperLinkListener() );
html.setBounds( 0, 0, 500, 500 );
JScrollPane scroller = new JScrollPane();
JViewport vp = scroller.getViewport();
// while ((inputLine = dis.readLine()) != null)
// {
vp.add( html );
p1.removeAll();
p1.add(vp);
p.add(p1);
// }
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe);
}
}
else if (toto.getSource()==b2)
{
tf1.setText("");
txt1.setText("");
tf2.setText("");
p.remove(p1);
}
}
public void mouseClicked(MouseEvent toto)
{
}
public void mouseEntered(MouseEvent toto)
{
}
public void mouseExited(MouseEvent toto)
{
}
public void mousePressed(MouseEvent toto)
{
}
public void mouseReleased(MouseEvent toto)
{
}
private HyperlinkListener createHyperLinkListener()
{
return new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent e)
{
if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
{
if (e instanceof HTMLFrameHyperlinkEvent)
{
//((HTMLDocument) html.getDocument()).processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) e);
}
else
{
try
{
path = e.getURL().getPath();
html.setPage( e.getURL() );
tf2.setText(path);
}
catch (IOException ioe)
{
System.out.println("IOE: " + ioe);
}
}
}
}
};
}
}
Merci d'avance.