经过多年的努力,我的点击工作正常。遗憾的是,当我将JTextPane
的格式更改为"text/html"
并将文本添加到JTextPane时,我的按钮消失了。我几乎完成了这个苛刻的情妇。有人可以帮忙吗?
代码如下......
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.applet.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class jlabeltest extends Applet {
public void init() {
jlabeltest textPaneExample = new jlabeltest();
textPaneExample.setSize(550, 300);
textPaneExample.setVisible(true);
}
public jlabeltest() {
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
InlineB button = new InlineB("Button");
textPane.setText("<p color='#FF0000'>Cool!</p>");
button.setAlignmentY(0.85f);
button.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
JOptionPane.showMessageDialog(null,"Hello!");
// Right Click
}
if (SwingUtilities.isLeftMouseButton(e)) {
JOptionPane.showMessageDialog(null,"Click!");
// Left Click
}
}
});
textPane.insertComponent(button);
this.add(textPane);
}
}
答案 0 :(得分:3)
添加组件时,此内容类型似乎存在问题(请参阅this帖子),但您可以尝试以下内容:
JTextPane textPane = new JTextPane();
JButton button = new JButton("Button");
button.setAlignmentY(0.85f);
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
textPane.setEditorKit(kit);
textPane.setDocument(doc);
try {
kit.insertHTML(doc, doc.getLength(), "<p color='#FF0000'>Cool!", 0, 0, HTML.Tag.P);
kit.insertHTML(doc, doc.getLength(), "<p></p>", 0, 0, null);
} catch (BadLocationException ex) {
} catch (IOException ex) {
}
答案 1 :(得分:2)
问题是当你向JEditorPane
添加文字然后添加一个嵌入JEditorPane
的HTML中的组件时:
以下是一个例子:
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class Test {
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame);
frame.pack();
frame.setVisible(true);
}
private void initComponents(JFrame frame) {
final JTextPane editorPane = new JTextPane();
editorPane.setSelectedTextColor(Color.red);
//set content as html
editorPane.setContentType("text/html");
editorPane.setText("<p color='#FF0000'>Cool!</p>");
//added <u></u> to underlone button
final InlineB label = new InlineB("<html><u>JLabel</u></html>");
label.setAlignmentY(0.85f);
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
//added check for MouseEvent.BUTTON1 which is left click
if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
JOptionPane.showMessageDialog(null, "Hello!");
String s = editorPane.getText();
System.out.println(s);
}
}
});
editorPane.insertComponent(label);
frame.getContentPane().add(editorPane);
}
}
class InlineB extends JButton {
public InlineB(String caption) {
super(caption);
setBorder(null);//set border to nothing
}
}
当我们点击按钮时,执行:
//added check for MouseEvent.BUTTON1 which is left click
if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
JOptionPane.showMessageDialog(null, "Hello!");
String s = editorPane.getText();
System.out.println(s);
}
println的结果是:
<html>
<head>
</head>
<body>
<p color="#FF0000">
Cool!
<p $ename="component">
</p>
</body>
</html>
正如您所看到的那样,组件嵌入在HTML中,任何对setText()
的进一步调用都将删除此内容。
唯一可行的解决方案(除了Rempelos +1之外)也是如此:
在您致电JEditorPane
之后将组件重新添加到setText()
:
@Override
public void mouseReleased(MouseEvent e) {
//added check for MouseEvent.BUTTON1 which is left click
if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
JOptionPane.showMessageDialog(null, "Hello!");
String s = editorPane.getText();
System.out.println(s);
editorPane.setText("<html><u>Hello</u></html>");
//re add after call to setText
editorPane.insertComponent(label);
}
}
虽然更好的方法可能是扩展JEditorPane
类并使setText()
方法自动添加其组件/按钮