我正在尝试将 HTML 放入 JTable
单元格,我已经在 IE 中测试了这段代码,但问题是,它没有按应有的方式出现在表格单元格中。我可以确认它在表格单元格中不起作用吗?下面是 HTML。
<html>
<style>
div {
*display: inline; /* For IE7 */
display:inline-block;
width: 50%;
text-align: center;
</style>
<div>A</div><div>B</div>
<div>A1</div><div>B1</div>
<div>A2</div><div>B2</div>
</html>
我还尝试将样式放入 <div>
中,它在 IE 中有效,但在表格单元格中无效。有人可以帮忙吗?
最佳答案
看起来 display
样式的属性被 JSE 提供的简单 CSS 引擎忽略了。这个消息来源证明了这一点。样式化文本为红色,但显示属性没有改变任何内容。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class HTMLDisplayStyle {
final static String EOL = System.getProperty("line.separator");
final static String HTML_PRE = "<html>" + EOL
+ "<head>" + EOL
+ "<style>" + EOL
+ "span {" + EOL
+ "color: #FF0000;" + EOL
+ "display: ";
final static String HTML_POST = ";" + EOL
+ "}" + EOL
+ "</style>" + EOL
+ "</head>" + EOL
+ "<body>" + EOL
+ "<p>" + EOL
+ "Some text " + EOL
+ "<span>text with display style</span> " + EOL
+ "some more text." + EOL
+ "</p>" + EOL
+ "</body>" + EOL
+ "</html>" + EOL;
final static String[] ATTRIBUTES = {
"inline",
"block",
"none"
};
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JPanel gui = new JPanel(new BorderLayout());
String s = HTML_PRE + ATTRIBUTES[0] + HTML_POST;
final JTextArea ta = new JTextArea(s, 15, 30);
gui.add(new JScrollPane(ta), BorderLayout.PAGE_END);
final JLabel l = new JLabel(s);
gui.add(new JScrollPane(l));
final JComboBox style = new JComboBox(ATTRIBUTES);
gui.add(style, BorderLayout.PAGE_START);
ActionListener styleListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String styleAttribute =
style.getSelectedItem().toString();
String html = HTML_PRE + styleAttribute + HTML_POST;
ta.setText(html);
l.setText(html);
}
};
style.addActionListener(styleListener);
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
关于java - JTable 单元格中的 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22525770/