为什么我的CSS不能在Java中正常工作? [英] Why isn't my CSS working right in Java?
问题描述
想知道你能帮我吗?我试图在Java中创建一些编码,以通过实现 CSS
在 JEditorPane
中执行html代码。 >
我可以通过 CSS
编码来实现 忽略编码中的float:
位。在JSFiddle(链接)我得到它运行无瑕疵,但在Java / NB它创建容器,左 div
这是小的,并且应该右边 div
,但它放在左边< c $ c> 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 =collapseddesc =HTML / CSS Script>
String eBullStyles =#divMsgs {
+height:99px;
+margin:auto;
+background-color:#3399FF;
+}
+#divLeft {
+float:left;
+width:110px;
+height:85px; b $ b +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);
//为html添加一些样式
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule(eBullStyles);
文档setdoc = kit.createDefaultDocument();
bulletinBoard.setDocument(setdoc);
String initial =< div id = \divMsgs \>
+< div id = \divLeft\>
+ Bob
+< / div>
+< div id = \divRight\>
+ b +< / div>
+< / div>;
try {
Document doc = bulletinBoard.getDocument();
bulletin .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);
frames.setVisible(true);
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGui();
}
});
}
}
到文档 ,Swing组件只支持HTML 3.2。
根据链接,某些CSS属性不会呈现:
以下描述了渲染引擎支持的CSS属性:
- font-family
- font-style
- font-size(支持相对单位)
- font-weight
- font
- color
- 背景颜色(透明除外)
- background-image
- background-repeat
- 背景位置
- 背景
装饰(除了眨眼和超线)
- vertical-align(只有sup和super)
- text-align中心)
- margin-top
- margin-right
li>
- margin-left
- margin
- padding-top
- padding- $ b
- padding-bottom
- padding-left
- padding
-top-style
- border-right-style
- border-bottom-style
-style
- border-style(仅支持插入,开始和无)
- border-top-color
li> border-right-color
- border-bottom-color
- border-left-color
border-color
- list-style-image
- list-style-type
位置
以下是建模,但目前未显示。
- font-variant
- 背景附件(背景始终视为滚动)
- $ b
- letter-spacing
- text-indent
- text-transform
- line-height
- border-top-width(用于指示是否应使用边框)
- border-right-width
- border-width
- border-left-width
/ li>
- border-top
- border-right
left
- border
- width
- height
b $ b
- 清除
- 显示
- white-space
style
Wondering if you could help me? I'm trying to create some coding in Java to execute html code in a JEditorPane
by implementing CSS
.
I get it to implement some of the CSS
coding as a rule, but it seems to ignore the "float:"
bit in the coding. On JSFiddle (link) I get it to run flawlessly, but on Java/NB it creates the container, left div
which is small, and the supposed right div
, but it puts it below the left div
, instead of to the right of it. What I'm thinking is that Java and HTMLEditorKit
as well as StyleSheet
doesn't support what I want to do. Can anybody give some clarity on this?
Edit:
It seem that only HTML 3.2
is supported. Is there a possible workaround for this?
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();
}
});
}
}
According to the documentation, Swing components support only HTML 3.2.
And, according to this link, some CSS properties are not rendered:
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
这篇关于为什么我的CSS不能在Java中正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!