簡體   English   中英

使用CSS的HTML表格格式在JTextPane中不起作用

[英]HTML Table formatting using css not working in JTextPane

我正在嘗試為某些特定項目實現僅查看HTML窗格。 我正在使用JTextPane來使用內容類型為"text/html"來呈現HTML。 我在輸入HTML中有表格,因此為了給這些表格加上邊框,我想到了使用CSS樣式,但不幸的是,它沒有奏效。

如果我將border屬性作為表本身的一部分,則它可以工作,但不能用於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);
    }

}

我從樣式中刪除了注釋符號,並添加了字體大小的單位。

在此處輸入圖片說明

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);
    }
}

這就是我為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"

在此處輸入圖片說明

我嘗試了上述所有解決方案,但不幸的是,這些解決方案均不適合我。 我正在尋找解決方案,因此進入了這篇文章

我試過了table, th, td { border-width: 1px solid black }瞧這對我有用。 我不知道為什么普通邊境財產對我不起作用,但對其他人卻起作用。

非常感謝您提供的所有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM