gpt4 book ai didi

java - JEdi​​torPane 中的 JButton

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:52:59 26 4
gpt4 key购买 nike

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

如何控制这个按钮? (更改大小、颜色、添加监听器等)

我解决了这个问题,一个有效的例子:

    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

最佳答案

这不会回答您的问题,但如果您只想在 JEditorKit 中拥有一个对点击使用react的按钮,它可能会对您有所帮助。

诀窍是使用 HTML 表单,并为每个按钮使用不同的 URL。然后,HyperlinkListener 将对按钮点击使用react,您可以在其中根据 URL 的内容调度您的事件。

    JEditorPane ed = new JEditorPane();
ed.setContentType("text/html");
HTMLEditorKit kit = (HTMLEditorKit) ed.getEditorKit();
HTMLDocument doc = (HTMLDocument) ed.getDocument();
doc.setBase(new URL("http://fakeurl.com"));
kit.setAutoFormSubmission(false);
ed.setText("<html>"
+ "<head>"
+ "</head>"
+ "<body>"
+ " <form action='http://fakeurl.com:1'><input type='submit' value='hello 1' /></form>"
+ " <form action='http://fakeurl.com:2'><input type='submit' value='hello 2' /></form>"
+ "</body>");
ed.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
System.err.println(e.getURL().getPort());
}
});
UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName());
JFrame f = new JFrame("test frame");
f.setContentPane(ed);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1024, 768);
f.setVisible(true);

关于java - JEdi​​torPane 中的 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9417355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com