java - 为什么我的 CSS 在 Java 中不能正常工作?

标签 java html css swing

想知道你是否可以帮助我?我正在尝试通过实现 CSS 在 Java 中创建一些代码以在 JEditorPane 中执行 html 代码。

我得到它来实现 一些 CSS 编码作为规则,但它似乎忽略了 "float:" 位编码。在 JSFiddle ( link ) 上,我让它完美运行,但在 Java/NB 上,它创建了容器,左边的 div 很小,右边的 div,但它把它放在左边 div 的下方,而不是右边。我在想的是 Java 和 HTMLEditorKit 以及 StyleSheet 不支持我想做的事情。任何人都可以澄清一下吗?

编辑:

似乎只支持HTML 3.2。是否有可能的解决方法?

import java.awt.Dimension; 
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

@SuppressWarnings("serial")
public class jBulletins extends JPanel {

    javax.swing.JEditorPane bulletinBoard = new javax.swing.JEditorPane();

    public jBulletins() {

        Dimension d = new Dimension();
        d.setSize(800, 600);

        bulletinBoard.setPreferredSize(d);
        bulletinBoard.setContentType("text/html"); // NOI18N

        add(new JScrollPane(bulletinBoard));

        loadBulletins();
    }

    private void loadBulletins() {

        //<editor-fold defaultstate="collapsed" desc="HTML/CSS Script">
        String eBullStyles = "#divMsgs{ "
                + "height: 99px; "
                + "margin: auto; "
                + "background-color: #3399FF; "
                + "} "
                + "#divLeft{ "
                + "float: left; "
                + "width: 110px; "
                + "height: 85px; "
                + "border: 2px #6666ff outset; "
                + "padding: 5px; "
                + "background-color: #189cd8; "
                + "color: #ffffff; "
                + "font-size: 11px; "
                + "font-weight: normal; "
                + "font-family: Tahoma, Geneva, sans-serif; "
                + "font-style: normal; "
                + "text-decoration: inherit; "
                + "} "
                + "#divRight{ "
                + "float: right; "
                + "width: 50px; "
                + "height: 85px; "
                + "border: 2px #6666ff outset; "
                + "background-color: #189cd8; "
                + "padding: 5px; "
                + "color: #ffffff; "
                + "font-size: 11px; "
                + "font-weight: normal; "
                + "font-family: Tahoma, Geneva, sans-serif; "
                + "font-style: normal; "
                //+ "text-decoration: inherit; "
                + "line-height: 1.3em; "
                + "}"; //</editor-fold>

        bulletinBoard.setContentType("text/html");
        bulletinBoard.setText("");

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

        // add some styles to the html
        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule(eBullStyles);

        Document setdoc = kit.createDefaultDocument();
        bulletinBoard.setDocument(setdoc);

        String initial = "<div id=\"divMsgs\">"
                + "<div id=\"divLeft\">"
                + "Hello Bob"
                + "</div>"
                + "<div id=\"divRight\">"
                + "How are you today?"
                + "</div>"
                + "</div>";

        try {
            Document doc = bulletinBoard.getDocument();
            bulletinBoard.setText(initial);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void createAndShowGui() {
        jBulletins mainPanel = new jBulletins();
        JFrame frame = new JFrame("Bulletin Board");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

最佳答案

根据documentation , Swing 组件仅支持 HTML 3.2。

并且,根据这个link , 某些 CSS 属性未呈现:

The following describes the CSS properties that are suppored by the rendering engine:

  • font-family
  • font-style
  • font-size (supports relative units)
  • font-weight
  • font
  • color
  • background-color (with the exception of transparent)
  • background-image
  • background-repeat
  • background-position
  • background
  • text-decoration (with the exception of blink and overline)
  • vertical-align (only sup and super)
  • text-align (justify is treated as center)
  • margin-top
  • margin-right
  • margin-bottom
  • margin-left
  • margin
  • padding-top
  • padding-right
  • padding-bottom
  • padding-left
  • padding
  • border-top-style
  • border-right-style
  • border-bottom-style
  • border-left-style
  • border-style (only supports inset, outset and none)
  • border-top-color
  • border-right-color
  • border-bottom-color
  • border-left-color
  • border-color
  • list-style-image
  • list-style-type
  • list-style-position

The following are modeled, but currently not rendered.

  • font-variant
  • background-attachment (background always treated as scroll)
  • word-spacing
  • letter-spacing
  • text-indent
  • text-transform
  • line-height
  • border-top-width (this is used to indicate if a border should be used)
  • border-right-width
  • border-bottom-width
  • border-left-width
  • border-width
  • border-top
  • border-right
  • border-bottom
  • border-left
  • border
  • width
  • height
  • float
  • clear
  • display
  • white-space
  • list-style

关于java - 为什么我的 CSS 在 Java 中不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147141/

相关文章:

java - 当代码为 400 时,使用 Swagger @ApiResponse responseContainer 不起作用

php - 在 php codeigniter 中包含另一个页面时,父页面未加载

html - 嵌套的 flex 容器没有延伸到剩余空间

css - IE8 中的最大宽度,宽度为 :100%?

java - 在openshift中的tomcat server.xml中定义上下文

java - 使用 java.bean API 检查非公共(public)类的实例

java - 如何在java中运行MySQL脚本?

html - 非标准形状的输入字段

html - 修复导航栏重叠问题

html表单布局安排