Java Tutorial/Swing/Web Browser — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:31, 31 мая 2010
Browser by JEditorPane
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.IDN;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class WebBrowserBasedOnJEditorPane extends JFrame implements HyperlinkListener {
private JTextField txtURL= new JTextField("");
JEditorPane ep = new JEditorPane();
private JLabel lblStatus= new JLabel(" ");
public WebBrowserBasedOnJEditorPane() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel pnlURL = new JPanel();
pnlURL.setLayout(new BorderLayout());
pnlURL.add(new JLabel("URL: "), BorderLayout.WEST);
pnlURL.add(txtURL, BorderLayout.CENTER);
getContentPane().add(pnlURL, BorderLayout.NORTH);
getContentPane().add( ep, BorderLayout.CENTER);
getContentPane().add(lblStatus, BorderLayout.SOUTH);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
String url = ae.getActionCommand().toLowerCase();
if (url.startsWith("http://"))
url = url.substring(7);
ep.setPage("http://" + IDN.toASCII(url));
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(WebBrowserBasedOnJEditorPane.this, "Browser problem: " + e.getMessage());
}
}
};
txtURL.addActionListener(al);
setSize(300, 300);
setVisible(true);
}
public void hyperlinkUpdate(HyperlinkEvent hle) {
HyperlinkEvent.EventType evtype = hle.getEventType();
if (evtype == HyperlinkEvent.EventType.ENTERED)
lblStatus.setText(hle.getURL().toString());
else if (evtype == HyperlinkEvent.EventType.EXITED)
lblStatus.setText(" ");
}
public static void main(String[] args) {
new WebBrowserBasedOnJEditorPane();
}
}
Listening for Hyperlink Events from a JEditorPane Component
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class Main {
public static void main(String[] argv) throws Exception{
String url = "http://java.sun.ru";
JEditorPane editorPane = new JEditorPane(url);
editorPane.setEditable(false);
editorPane.addHyperlinkListener(new MyHyperlinkListener());
}
}
class MyHyperlinkListener implements HyperlinkListener {
public void hyperlinkUpdate(HyperlinkEvent evt) {
if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane pane = (JEditorPane) evt.getSource();
try {
// Show the new page in the editor pane.
pane.setPage(evt.getURL());
} catch (IOException e) {
}
}
}
}