Java by API/javax.swing/JEditorPane — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:18, 31 мая 2010
Содержание
- 1 JEditorPane: addHyperlinkListener(HyperlinkListener listener)
- 2 JEditorPane: getDocument()
- 3 JEditorPane: setDocument(Document doc)
- 4 JEditorPane: setEditable(boolean b)
- 5 JEditorPane: setPage(URL url)
- 6 new JEditorPane(String URL) ("http://www.java2s.com")
- 7 new JEditorPane("text/html", "html string")
JEditorPane: addHyperlinkListener(HyperlinkListener listener)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class MainClass extends JFrame implements ActionListener, HyperlinkListener {
JEditorPane view;
JTextField commandLine;
MainClass(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
commandLine = new JTextField();
commandLine.addActionListener(this);
getContentPane().add(commandLine, BorderLayout.NORTH);
view = new JEditorPane();
view.setEditable(false);
view.setPreferredSize(new Dimension(400, 400));
view.addHyperlinkListener(this);
getContentPane().add(view, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
URL url = new URL(e.getActionCommand());
view.setPage(url);
commandLine.setText(url.toExternalForm());
} catch (MalformedURLException e2) {
System.out.println("Bad URL: " + e.getActionCommand());
} catch (java.io.IOException e2) {
}
}
public void hyperlinkUpdate(HyperlinkEvent e) {
try {
view.setPage(e.getURL());
commandLine.setText(e.getURL().toExternalForm());
} catch (java.io.IOException e2) {
}
}
public static void main(String[] args) {
new MainClass("Editor Demo");
}
}
JEditorPane: getDocument()
import java.awt.EventQueue;
import java.awt.Frame;
import java.io.IOException;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("EditorPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
JEditorPane editorPane = new JEditorPane("http://www.jexp.ru");
editorPane.setEditable(false);
HyperlinkListener hyperlinkListener = new ActivatedHyperlinkListener(
editorPane);
editorPane.addHyperlinkListener(hyperlinkListener);
JScrollPane scrollPane = new JScrollPane(editorPane);
frame.add(scrollPane);
} catch (IOException e) {
System.err.println("Unable to load: " + e);
}
frame.setSize(640, 480);
frame.setVisible(true);
}
}
class ActivatedHyperlinkListener implements HyperlinkListener {
JEditorPane editorPane;
public ActivatedHyperlinkListener(JEditorPane editorPane) {
this.editorPane = editorPane;
}
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
final URL url = hyperlinkEvent.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("URL: " + url);
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Activated");
Document doc = editorPane.getDocument();
try {
editorPane.setPage(url);
} catch (IOException ioException) {
System.out.println("Error following link");
editorPane.setDocument(doc);
}
}
}
}
JEditorPane: setDocument(Document doc)
import java.awt.EventQueue;
import java.awt.Frame;
import java.io.IOException;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("EditorPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
JEditorPane editorPane = new JEditorPane("http://www.jexp.ru");
editorPane.setEditable(false);
HyperlinkListener hyperlinkListener = new ActivatedHyperlinkListener(
editorPane);
editorPane.addHyperlinkListener(hyperlinkListener);
JScrollPane scrollPane = new JScrollPane(editorPane);
frame.add(scrollPane);
} catch (IOException e) {
System.err.println("Unable to load: " + e);
}
frame.setSize(640, 480);
frame.setVisible(true);
}
}
class ActivatedHyperlinkListener implements HyperlinkListener {
JEditorPane editorPane;
public ActivatedHyperlinkListener(JEditorPane editorPane) {
this.editorPane = editorPane;
}
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
final URL url = hyperlinkEvent.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("URL: " + url);
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Activated");
Document doc = editorPane.getDocument();
try {
editorPane.setPage(url);
} catch (IOException ioException) {
System.out.println("Error following link");
editorPane.setDocument(doc);
}
}
}
}
JEditorPane: setEditable(boolean b)
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class Main {
public static void main(String args[]) {
JFrame f = new JFrame("JEditorPane Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
JEditorPane editor = new JEditorPane(
"text/html",
"<H3>Help</H3><center><IMG src=file:///c:/a.jpg></center><li>One<li><i>Two</i><li><u>Three</u>");
editor.setEditable(false);
JScrollPane scrollPane = new JScrollPane(editor);
content.add(scrollPane, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
JEditorPane: setPage(URL url)
import java.awt.EventQueue;
import java.awt.Frame;
import java.io.IOException;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("EditorPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
JEditorPane editorPane = new JEditorPane("http://www.jexp.ru");
editorPane.setEditable(false);
HyperlinkListener hyperlinkListener = new ActivatedHyperlinkListener(
editorPane);
editorPane.addHyperlinkListener(hyperlinkListener);
JScrollPane scrollPane = new JScrollPane(editorPane);
frame.add(scrollPane);
} catch (IOException e) {
System.err.println("Unable to load: " + e);
}
frame.setSize(640, 480);
frame.setVisible(true);
}
}
class ActivatedHyperlinkListener implements HyperlinkListener {
JEditorPane editorPane;
public ActivatedHyperlinkListener(JEditorPane editorPane) {
this.editorPane = editorPane;
}
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
final URL url = hyperlinkEvent.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("URL: " + url);
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Activated");
Document doc = editorPane.getDocument();
try {
editorPane.setPage(url);
} catch (IOException ioException) {
System.out.println("Error following link");
editorPane.setDocument(doc);
}
}
}
}
new JEditorPane(String URL) ("http://www.java2s.com")
import java.awt.EventQueue;
import java.awt.Frame;
import java.io.IOException;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("EditorPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
JEditorPane editorPane = new JEditorPane("http://www.jexp.ru");
editorPane.setEditable(false);
HyperlinkListener hyperlinkListener = new ActivatedHyperlinkListener(
editorPane);
editorPane.addHyperlinkListener(hyperlinkListener);
JScrollPane scrollPane = new JScrollPane(editorPane);
frame.add(scrollPane);
} catch (IOException e) {
System.err.println("Unable to load: " + e);
}
frame.setSize(640, 480);
frame.setVisible(true);
}
}
class ActivatedHyperlinkListener implements HyperlinkListener {
JEditorPane editorPane;
public ActivatedHyperlinkListener(JEditorPane editorPane) {
this.editorPane = editorPane;
}
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
final URL url = hyperlinkEvent.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("URL: " + url);
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Activated");
Document doc = editorPane.getDocument();
try {
editorPane.setPage(url);
} catch (IOException ioException) {
System.out.println("Error following link");
editorPane.setDocument(doc);
}
}
}
}
new JEditorPane("text/html", "html string")
import java.awt.BorderLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JEditorPane Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JEditorPane editor = new JEditorPane(
"text/html",
"<H3>Help</H3><center>www.jexp.ru</center><li>One<li><i>Two</i><li><u>Three</u>");
editor.setEditable(false);
JScrollPane scrollPane = new JScrollPane(editor);
f.add(scrollPane, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}