我正在尝试为某个特定项目实现仅查看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’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’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’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’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);
}
}
答案 0 :(得分: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’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’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);
}
}
答案 1 :(得分:0)
这是我为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"
答案 2 :(得分:0)
我尝试了上述所有解决方案,但不幸的是,其中任何一个都不适用于我。我正在寻找解决方案,我进入了this post
我试过table, th, td { border-width: 1px solid black }
瞧它对我有用。我不知道为什么平原边界财产对我不起作用,但它对其他人有效。
非常感谢你帮助所有人。