我试图将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中有效,但在表格单元格中无效。有人可以帮忙吗?

最佳答案

JCC附带的简单CSS引擎似乎忽略了display样式的属性。此消息源证明了这一点。样式化的文本为红色,但display属性不变。



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

09-10 03:56