2

经过大量的努力,我的点击工作正常。可悲的是,当我更改我的格式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); 
    }
}
4

2 回答 2

3

添加组件时,此内容类型似乎存在一些问题(请参阅帖子),但您可以尝试以下操作:

    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) {
    }
于 2012-09-29T12:43:52.767 回答
2

问题是当您向 中添加文本JEditorPane,然后添加一个组件时,它会嵌入到 的 HTML 中JEditorPane

这是一个例子:

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(s) 的结果是:

<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()方法自动添加其组件/按钮

于 2012-09-29T13:04:13.957 回答