http://weblogs.java.net/blog/aim/archive/2007/07/embedding_swing.html
http://docs.oracle.com/javase/1.5.0/docs/api/index.html?javax/swing/text/html/ObjectView.html
Я добавляю HTML-код в JEditorPane:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<table width="90%" height="90%" align="center">
<tr align="center">
<td align="center">
<object classid="javax.swing.JButton" label="just do it">
</object>
</td>
</tr>
</table>
</body>
</html>
image http://s006.radikal.ru/i215/1202/f7/4d1d3108a521.gif
Как я могу управлять этой кнопкой?(Изменить размер, цвет, добавить слушателя и т. Д.)
Я решил проблему, рабочий пример:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.ViewFactory;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.html.ObjectView;
import javax.swing.text.AbstractDocument;
import javax.swing.text.View;
import javax.swing.text.StyleConstants;
import javax.swing.text.*;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import java.net.URL;
public class EditorPaneTest extends JFrame
{
public EditorPaneTest()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
JEditorPane editPane = new JEditorPane();
JScrollPane scrollPane = new JScrollPane(editPane);
editPane.setContentType("text/html");
editPane.setEditable(false);
editPane.setEditorKit(new CompEditorKit()); // install our hook
editPane.setText("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"+
"<html>"+
"<body>"+
"<object classid=\"javax.swing.JLabel\" label=\"just do it\">"+
"</object>"+
"</body>"+
"</html>");
add(scrollPane, BorderLayout.CENTER);
setSize(400, 300);
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new EditorPaneTest();
}
});
}
protected class CompEditorKit extends HTMLEditorKit {
@Override
public ViewFactory getViewFactory() {
return new CompFactory();
}
}
protected class CompFactory extends HTMLEditorKit.HTMLFactory {
public CompFactory() {
super();
}
@Override
public View create(Element element) {
AttributeSet attrs = element.getAttributes();
Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
if (o instanceof HTML.Tag) {
if ((HTML.Tag) o == HTML.Tag.OBJECT) {
return new CompView(element);
}
}
return super.create(element);
}
}
protected class CompView extends ObjectView {
public CompView(Element element) {
super(element);
}
@Override
protected Component createComponent() {
Component component = super.createComponent(); // COMPONENT IS CREATED HERE
System.out.println(component);
JLabel layeredPane = (JLabel)component;
layeredPane.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/9.gif"))));
return component;
}
}
}
Но теперь другая проблема.Я не могу получить координаты Jlabel System.out.println (layeredPane.getLocation () + "|" + layeredPane.getSize () + "|" + layeredPane.getY ());Возврат 0 0 0