【问题标题】:HTML Table formatting using css not working in JTextPane使用 css 的 HTML 表格格式在 JTextPane 中不起作用
【发布时间】:2014-07-08 13:27:21
【问题描述】:

我正在尝试为某些特定项目实现仅查看 HTML 窗格。我正在使用JTextPane 使用内容类型为"text/html" 呈现HTML。我的输入 HTML 中有表格,所以为了给这些表格提供边框,我想使用 css 样式,但不幸的是它没有成功。

如果我将边框属性作为表格本身的一部分,那么它可以工作,但不能 使用 css 样式。

这是我为重新创建问题而创建的示例代码。 content1 不会为我的表格创建边框,但 content2 会创建它。我想使用 content1 方法,因为我有很多带有表格的 html 文件。感谢您的宝贵时间,我们将不胜感激。

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;


public class TestTextPane {

    private static int X = 200;
    private static int Y = 200;
    private static int W = 600;
    private static int H = 400;

    public static final String content1 = "<html>\r\n" + 
            "  <head>\r\n" + 
            "    <style type=\"text/css\">\r\n" + 
            "      <!--\r\n" + 
            "        table,th, td { border: 1px solid black }\r\n" + 
            "        body, p { font-family: Courier; font-size: 14 }\r\n" + 
            "      -->\r\n" + 
            "    </style>\r\n" + 
            "    \r\n" + 
            "  </head>\r\n" + 
            "  <body>\r\n" + 
            "    <div align=\"left\">\r\n" + 
            "      <b>Q: What is the difference between GET and POST method? </b>\r\n" + 
            "    </div>\r\n" + 
            "    <p>\r\n" + 
            "      A:\r\n" + 
            "    </p>\r\n" + 
            "    <table>\r\n" + 
            "      <tr>\r\n" + 
            "        <th width=\"50%\">\r\n" + 
            "          GET\r\n" + 
            "        </th>\r\n" + 
            "        <th>\r\n" + 
            "          POST\r\n" + 
            "        </th>\r\n" + 
            "      </tr>\r\n" + 
            "      <tr>\r\n" + 
            "        <td>\r\n" + 
            "          GET is a safe method (idempotent)\r\n" + 
            "        </td>\r\n" + 
            "        <td>\r\n" + 
            "          POST is non-idempotent method\r\n" + 
            "        </td>\r\n" + 
            "      </tr>\r\n" + 
            "      <tr>\r\n" + 
            "        <td>\r\n" + 
            "          We can send limited data with GET method and it&#8217;s sent in the header \r\n" + 
            "          request URL\r\n" + 
            "        </td>\r\n" + 
            "        <td>\r\n" + 
            "          we can send large amount of data with POST because it&#8217;s part of the \r\n" + 
            "          body.\r\n" + 
            "        </td>\r\n" + 
            "      </tr>\r\n" + 
            "    </table>\r\n" + 
            "    <br>\r\n" + 
            "    <br>\r\n" + 
            "    \r\n" + 
            "  </body>\r\n" + 
            "</html>";

    public static final String content2 = "<html>\r\n" + 
    "  <head>\r\n" + 
    "    <style type=\"text/css\">\r\n" + 
    "      <!--\r\n" + 
    "        body, p { font-family: Courier; font-size: 14 }\r\n" + 
    "      -->\r\n" + 
    "    </style>\r\n" + 
    "    \r\n" + 
    "  </head>\r\n" + 
    "  <body>\r\n" + 
    "    <div align=\"left\">\r\n" + 
    "      <b>Q: What is the difference between GET and POST method? </b>\r\n" + 
    "    </div>\r\n" + 
    "    <p>\r\n" + 
    "      A:\r\n" + 
    "    </p>\r\n" + 
    "    <table border=1>\r\n" + 
    "      <tr>\r\n" + 
    "        <th width=\"50%\">\r\n" + 
    "          GET\r\n" + 
    "        </th>\r\n" + 
    "        <th>\r\n" + 
    "          POST\r\n" + 
    "        </th>\r\n" + 
    "      </tr>\r\n" + 
    "      <tr>\r\n" + 
    "        <td>\r\n" + 
    "          GET is a safe method (idempotent)\r\n" + 
    "        </td>\r\n" + 
    "        <td>\r\n" + 
    "          POST is non-idempotent method\r\n" + 
    "        </td>\r\n" + 
    "      </tr>\r\n" + 
    "      <tr>\r\n" + 
    "        <td>\r\n" + 
    "          We can send limited data with GET method and it&#8217;s sent in the header \r\n" + 
    "          request URL\r\n" + 
    "        </td>\r\n" + 
    "        <td>\r\n" + 
    "          we can send large amount of data with POST because it&#8217;s part of the \r\n" + 
    "          body.\r\n" + 
    "        </td>\r\n" + 
    "      </tr>\r\n" + 
    "    </table>\r\n" + 
    "    <br>\r\n" + 
    "    <br>\r\n" + 
    "    \r\n" + 
    "  </body>\r\n" + 
    "</html>";

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setBounds(X, Y, W, H);

        JTextPane pane = new JTextPane();
        pane.setContentType("text/html");
        pane.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(pane);
        scrollPane.setBounds(X,Y,W,H);

        frame.getContentPane().add(scrollPane);

        pane.setText(content2); // change content here

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

【问题讨论】:

  • 在样式中添加 tr 以及为 content1 添加 table,tr, th, td { border: 1px solid black }

标签: java html css swing jtextpane


【解决方案1】:

我从样式中删除了注释符号并添加了字体大小的单位。

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class TestTextPane {

    private static int X = 200;
    private static int Y = 200;
    private static int W = 600;
    private static int H = 400;

    public static final String content1 = "<html>\r\n" +
            "  <head>\r\n" +
            "    <style type=\"text/css\">\r\n" +
            "        table, th, td { border: 2px solid #FF0000; }\r\n" +
            // be sure to add a unit to the font size, otherwise it is invalid
            "        body, p { font-family: Courier; font-size: 14px; }\r\n" +
            "    </style>\r\n" +
            "    \r\n" +
            "  </head>\r\n" +
            "  <body>\r\n" +
            "    <div align=\"left\">\r\n" +
            "      <b>Q: What is the difference between GET and POST method? </b>\r\n" +
            "    </div>\r\n" +
            "    <p>\r\n" +
            "      A:\r\n" +
            "    </p>\r\n" +
            "    <table>\r\n" +
            "      <tr>\r\n" +
            "        <th width=\"50%\">\r\n" +
            "          GET\r\n" +
            "        </th>\r\n" +
            "        <th>\r\n" +
            "          POST\r\n" +
            "        </th>\r\n" +
            "      </tr>\r\n" +
            "      <tr>\r\n" +
            "        <td>\r\n" +
            "          GET is a safe method (idempotent)\r\n" +
            "        </td>\r\n" +
            "        <td>\r\n" +
            "          POST is non-idempotent method\r\n" +
            "        </td>\r\n" +
            "      </tr>\r\n" +
            "      <tr>\r\n" +
            "        <td>\r\n" +
            "          We can send limited data with GET method and it&#8217;s sent in the header \r\n" +
            "          request URL\r\n" +
            "        </td>\r\n" +
            "        <td>\r\n" +
            "          we can send large amount of data with POST because it&#8217;s part of the \r\n" +
            "          body.\r\n" +
            "        </td>\r\n" +
            "      </tr>\r\n" +
            "    </table>\r\n" +
            "    <br>\r\n" +
            "    <br>\r\n" +
            "    \r\n" +
            "  </body>\r\n" +
            "</html>";

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setBounds(X, Y, W, H);

        JTextPane pane = new JTextPane();
        pane.setContentType("text/html");
        pane.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(pane);
        scrollPane.setBounds(X,Y,W,H);

        frame.getContentPane().add(scrollPane);

        pane.setText(content1); // change content here

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

【讨论】:

  • 感谢您的快速回复。我做了上述更改,但仍然无法获得表格的边框。
【解决方案2】:

这是我为 content1 尝试过的,它工作正常。

我添加了这一行

body table { border: 1px solid red }

示例代码:

public static final String content1 = "<html>\r\n"
        + "  <head>\r\n"
        + "    <style type=\"text/css\">\r\n"
        + "      <!--\r\n"
        + "        table, th, td { border: 1px solid black }\r\n"
        + "        body table { border: 1px solid red }\r\n"
        + "        body, p { font-family: Courier; font-size: 14; }\r\n"
        + "      -->\r\n"
        + "    </style>\r\n"

【讨论】:

  • 尝试了这个建议但无法获得边框:(
  • 请验证我的截图。这是你想要的吗?
  • 我只想为我的表格添加单色边框,我知道可以通过您的建议来实现,但我仍然无法在我的程序中使用它
  • 如果这是您想要的,请从我的帖子中复制。根据您的要求更改颜色。如果需要,将红色变为黑色。这与html css样式更相关。
  • 添加 tr 样式以及 table,tr, th, td { border: 1px solid black } 为 content1 如果它不起作用。
【解决方案3】:

我尝试了上述所有解决方案,但不幸的是,其中任何一个都不适合我。我正在寻找解决方案,我进入了this post

我尝试了table, th, td { border-width: 1px solid black } 瞧,它对我有用。我不知道为什么普通边框属性对我不起作用,但对其他人起作用。

非常感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 2017-03-12
    • 2014-03-24
    • 2012-07-30
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多