Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';如果设置为";text/html";_Java_Html_Swing_Jtextpane_Jcomponent - Fatal编程技术网

Java Can';如果设置为";text/html";

Java Can';如果设置为";text/html";,java,html,swing,jtextpane,jcomponent,Java,Html,Swing,Jtextpane,Jcomponent,我辛辛苦苦地干了很多活,终于使我的咔哒声工作得很好了。遗憾的是,当我将JTextPane的格式更改为“text/html”并向JTextPane添加文本时,我的按钮消失了。我几乎受够了这个苛刻的情妇。有人能帮忙吗 代码如下 import java.awt.*; import javax.swing.*; import java.awt.Color; import javax.swing.JTextPane; import javax.swing.JButton; import java.appl

我辛辛苦苦地干了很多活,终于使我的咔哒声工作得很好了。遗憾的是,当我将
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); 
    }
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.Color;
导入javax.swing.JTextPane;
导入javax.swing.JButton;
导入java.applet.*;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
公共类jlabeltest扩展了Applet{
公共void init(){
jlabeltest textPaneExample=新的jlabeltest();
textPaneExample.setSize(550300);
textPaneExample.setVisible(true);
}
公共jlabeltest(){
JTextPane textPane=新的JTextPane();
setContentType(“text/html”);
InlineB按钮=新的InlineB(“按钮”);
textPane.setText(“

酷!

”; 按钮设置对齐Y(0.85f); button.addMouseListener(新的MouseAdapter(){ 公共无效MouseEvent(MouseEvent e){ 如果(例如isPopupTrigger()){ showMessageDialog(null,“Hello!”); //右击 } if(SwingUtilities.isLeftMouseButton(e)){ showMessageDialog(null,“单击!”); //左键单击 } } }); textPane.insertComponent(按钮); 添加(文本窗格); } }
添加组件时,此内容类型似乎存在一些问题(请参阅文章),但您可以尝试以下操作:

    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) {
    }
JTextPane textPane=newjtextpane();
JButton按钮=新JButton(“按钮”);
按钮设置对齐Y(0.85f);
HTMLEditorKit=新的HTMLEditorKit();
HTMLDocument doc=新的HTMLDocument();
textPane.setEditorKit(工具包);
textPane.setDocument(doc);
试一试{
insertHTML(doc,doc.getLength(),“

酷!”,0,0,HTML.Tag.p); insertHTML(doc,doc.getLength(),“”,0,0,null); }捕获(BadLocationException ex){ }捕获(IOEX异常){ }


问题在于,当您将文本添加到
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
    }
}
println的结果是:


虽然更好的方法可能是扩展
JEditorPane
类并使
setText()
方法自动添加其组件/按钮

我应该将其保持在本机模式而不使用HTML吗?或者我有选择吗?请查看post by and about
JTextPane
/
JEditorPane
,其中显示了如何为按钮添加自定义标记
//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);
                }
<html>
  <head>

  </head>
  <body>
    <p color="#FF0000">
      Cool!
      <p $ename="component">

    </p>
  </body>
</html>
        @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);
            }
        }