Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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 在窗格中设置内联文本和图像_Java_Html_Swing_Emoji_Jeditorpane - Fatal编程技术网

Java 在窗格中设置内联文本和图像

Java 在窗格中设置内联文本和图像,java,html,swing,emoji,jeditorpane,Java,Html,Swing,Emoji,Jeditorpane,我无法在窗格中设置内联txt和img 绝地武士窗格的图像: 如何将它们设置为内联 这是我的代码: JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.co

我无法在窗格中设置内联txt和img

绝地武士窗格的图像:

如何将它们设置为内联

这是我的代码:

        JFrame frame = new JFrame("HelloWorldSwing");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{363, 0};
        gridBagLayout.rowHeights = new int[]{261, 0, 0};
        gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        frame.getContentPane().setLayout(gridBagLayout);


        JEditorPane jep = new JEditorPane();
        jep.setContentType("text/html");

        HTMLEditorKit kit = new HTMLEditorKit();
        jep.setEditorKit(kit);


        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule("body {color:#000; font-family:times; margin: 4px; display: inline; line-height: 20px;}");
        styleSheet.addRule("img {padding-top: 10;}");


        jep.setText("<span>Hello computer! <img src='file:ClientServer/Sticker/1.png' width='20' height='20' valign='middle'></span>");
JFrame=newjframe(“HelloWorldSwing”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout GridBagLayout=新的GridBagLayout();
gridBagLayout.columnWidths=newint[]{363,0};
gridBagLayout.rowHeights=newint[]{261,0,0};
gridBagLayout.columnWeights=新的double[]{0.0,double.MIN_值};
gridBagLayout.rowWeights=新的double[]{0.0,0.0,double.MIN_值};
frame.getContentPane().setLayout(gridBagLayout);
JEditorPane jep=新的JEditorPane();
setContentType(“text/html”);
HTMLEditorKit=新的HTMLEditorKit();
jep.setEditorKit(工具包);
StyleSheet StyleSheet=kit.getStyleSheet();
addRule(“body{color:#000;字体系列:times;margin:4px;display:inline;行高:20px;}”);
addRule(“img{padding top:10;}”);
jep.setText(“你好,电脑!”);
  • javax/swing/text/html/ImageView.java
  • 通过参考上述代码,指定
    “align”
    而不是
    “valign”
    似乎效果不佳。
    • styleSheet.addRule(“img{padding top:10;align:middle;valign:middle;vertical align:middle;}”)
下面是重写
ImageView#getAlignment(…)
方法的示例

import java.awt.*;
导入java.net。*;
导入javax.swing.*;
导入javax.swing.text.*;
导入javax.swing.text.html.*;
公共类ImgBaselineTest{
私有JComponent makeUI(){
JEditorPane editor2=makeEditorPane(新的ImgBaselineHTMLEditorKit());
JEditorPane editor1=makeEditorPane(新的HTMLEditorKit());
JPanel p=新的JPanel(新的网格布局(2,1));
p、 添加(新的JScrollPane(编辑器1));
p、 添加(新的JScrollPane(编辑器2));
返回p;
}
私人绝地武士窗格makeEditorPane(HTMLeditor工具包){
JEditorPane jep=新的JEditorPane();
setContentType(“text/html”);
jep.putClientProperty(JEditorPane.HONOR\u DISPLAY\u属性,Boolean.TRUE);
//HTMLEditorKit=新的HTMLEditorKit();
//HTMLEditorKit=新的ImgBaselineHTMLEditorKit();
jep.setEditorKit(工具包);
StyleSheet StyleSheet=kit.getStyleSheet();
addRule(“span{color:orange;字体系列:times;边距:4px;显示:inline;行高:20px;}”);
addRule(“img{padding top:10;align:middle;valign:middle;vertical align:middle;}”);
//URL=ImgBaselineTest.class.getResource(“16x16.png”);
字符串url=”https://i.stack.imgur.com/MOQoc.png";
//jep.setText(“你好,电脑!”);
jep.setText(“你好,电脑!”);
返回jep;
}
公共静态void main(字符串[]args){
EventQueue.invokeLater(()->{
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f、 getContentPane().add(新的ImgBaselineTest().makeUI());
f、 设置大小(320240);
f、 setLocationRelativeTo(空);
f、 setVisible(真);
});
}
}
类ImgBaselineHTMLEditorKit扩展了HTMLEditorKit{
@覆盖公共ViewFactory getViewFactory(){
返回新的HTMLEditorKit.HTMLFactory(){
@替代公共视图创建(图元元素){
View=super.create(elem);
if(查看LabelView的实例){
System.out.println(view.getAlignment(view.Y_轴));
}
AttributeSet attrs=elem.getAttributes();
Object elementName=attrs.getAttribute(AbstractDocument.ElementNameAttribute);
对象o=elementName!=null?null:attrs.getAttribute(StyleConstants.NameAttribute);
if(o instanceof HTML.Tag){
tagkind=(HTML.Tag)o;
if(kind==HTML.Tag.IMG){
返回新的图像视图(elem){
@替代公共浮动对齐(int轴){
开关(轴){
案例视图Y_轴:
return.8125f;//幻数。。。
违约:
返回super.getAlignment(轴);
}
}
};
}
}
返回视图;
}
};
}
}
/**
 * Update any cached values that come from attributes.
 */
protected void setPropertiesFromAttributes() {
    StyleSheet sheet = getStyleSheet();
    this.attr = sheet.getViewAttributes(this);

    //...

    AttributeSet attr = getElement().getAttributes();

    // Alignment.
    // PENDING: This needs to be changed to support the CSS versions
    // when conversion from ALIGN to VERTICAL_ALIGN is complete.
    Object alignment = attr.getAttribute(HTML.Attribute.ALIGN);

    vAlign = 1.0f;
    if (alignment != null) {
        alignment = alignment.toString();
        if ("top".equals(alignment)) {
            vAlign = 0f;
        }
        else if ("middle".equals(alignment)) {
            vAlign = .5f;
        }
    }
import java.awt.*;
import java.net.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class ImgBaselineTest {
  private JComponent makeUI() {
    JEditorPane editor2 = makeEditorPane(new ImgBaselineHTMLEditorKit());
    JEditorPane editor1 = makeEditorPane(new HTMLEditorKit());
    JPanel p = new JPanel(new GridLayout(2, 1));
    p.add(new JScrollPane(editor1));
    p.add(new JScrollPane(editor2));
    return p;
  }
  private JEditorPane makeEditorPane(HTMLEditorKit kit) {
    JEditorPane jep = new JEditorPane();
    jep.setContentType("text/html");
    jep.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    // HTMLEditorKit kit = new HTMLEditorKit();
    // HTMLEditorKit kit = new ImgBaselineHTMLEditorKit();
    jep.setEditorKit(kit);
    StyleSheet styleSheet = kit.getStyleSheet();
    styleSheet.addRule("span {color:orange; font-family:times; margin: 4px; display: inline; line-height: 20px;}");
    styleSheet.addRule("img {padding-top: 10; align: middle; valign: middle; vertical-align: middle; }");

    //URL url = ImgBaselineTest.class.getResource("16x16.png");
    String url = "https://i.stack.imgur.com/MOQoc.png";
    // jep.setText("<span>Hello computer! <img src='" + url + "' width='20' height='20' valign='middle'></span>");
    jep.setText("<span>Hello computer! <img src='" + url + "''></span>");
    return jep;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new ImgBaselineTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

class ImgBaselineHTMLEditorKit extends HTMLEditorKit {
  @Override public ViewFactory getViewFactory() {
    return new HTMLEditorKit.HTMLFactory() {
      @Override public View create(Element elem) {
        View view = super.create(elem);
        if (view instanceof LabelView) {
          System.out.println(view.getAlignment(View.Y_AXIS));
        }
        AttributeSet attrs = elem.getAttributes();
        Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
        Object o = elementName != null ? null : attrs.getAttribute(StyleConstants.NameAttribute);
        if (o instanceof HTML.Tag) {
          HTML.Tag kind = (HTML.Tag) o;
          if (kind == HTML.Tag.IMG) {
            return new ImageView(elem) {
              @Override public float getAlignment(int axis) {
                switch (axis) {
                case View.Y_AXIS:
                  return .8125f; // magic number...
                default:
                  return super.getAlignment(axis);
                }
              }
            };
          }
        }
        return view;
      }
    };
  }
}