HTML JLabel 中的文本忽略与 text-align: center 的对齐
HTML text in JLabel ignores alignment with text-align: center
我需要将文本格式化为 HTML。因此,文本包含在 HTML 标签中,并应用了正确的颜色和对齐方式。颜色设置有效,但 'text-align: center;' 设置的对齐无效。希望在 'botlabel'.
中居中对齐 HTML 文本
清理和重建项目没有帮助。
关于什么可能会覆盖此对齐设置的任何想法?
任何帮助深表感谢。谢谢。
这是代码
public class TestMessageDialogV2 extends JFrame {
public static void main(String[] args) {
final String html = "<html><h3 style='color: #ff0000; padding: 3px;'>The folder does not exist at this path.</h3>"
+ "<ol style='padding: 3px;'><li><span style='color: #000000;'>a folder path</span></li>"
+ "</ol><h3 style='color: #ff0000;padding: 3px;'>Invalid files selected:</h3>"
+ "<ol style='padding: 3px;'><li>"
+ "<span style='color: #000000;'>The file does not exist at this path.<br />some file</span></li>"
+ "</ol><br clear=all />"
+ "</html>";
UIManager.put("OptionPane.background", Color.WHITE.brighter());
UIManager.put("Panel.background", Color.WHITE.brighter());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
JPanel panel = new JPanel();
panel.setLayout( new BorderLayout() );
panel.setBackground( Color.WHITE.brighter() );
final JEditorPane editorPane = new JEditorPane("text/html", html);
editorPane.setEditable(false);
//editorPane.setPreferredSize();
editorPane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(editorPane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
JScrollPane scrollPane = new JScrollPane (
editorPane
, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(400,120));
JLabel botlabel = new JLabel();
botlabel.setText(
"<html>"
+ "<br clear=all />"
+ "<h3 style='color: #0033CC; text-align: center; '>"
//+ com.fox.dv.utils.iofile.DoHtml.SPACES
+ "Press Ok to continue or Cancel to exit.</h3>"
+ "</html>"
);
botlabel.setBorder(BorderFactory.createLineBorder( Color.black, 1 ) );
botlabel.setHorizontalTextPosition(JLabel.CENTER);
botlabel.setVerticalAlignment(SwingConstants.CENTER);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(botlabel, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(
null
, panel
, "Title"
, JOptionPane.ERROR_MESSAGE
);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private static final long serialVersionUID = -2116426209967837157L;
}
因为 Swing Components 对 HTML 的支持是 limited,请为 botlabel
指定 JLabel.CENTER
。
JLabel botlabel = new JLabel("", JLabel.CENTER);
botlabel.setText(
"<html>"
+ "<br clear=all />"
+ "<h3 style='color: #0033CC'>"
+ "Press Ok to continue or Cancel to exit.</h3>"
+ "</html>"
);
我需要将文本格式化为 HTML。因此,文本包含在 HTML 标签中,并应用了正确的颜色和对齐方式。颜色设置有效,但 'text-align: center;' 设置的对齐无效。希望在 'botlabel'.
中居中对齐 HTML 文本清理和重建项目没有帮助。 关于什么可能会覆盖此对齐设置的任何想法? 任何帮助深表感谢。谢谢。
这是代码
public class TestMessageDialogV2 extends JFrame {
public static void main(String[] args) {
final String html = "<html><h3 style='color: #ff0000; padding: 3px;'>The folder does not exist at this path.</h3>"
+ "<ol style='padding: 3px;'><li><span style='color: #000000;'>a folder path</span></li>"
+ "</ol><h3 style='color: #ff0000;padding: 3px;'>Invalid files selected:</h3>"
+ "<ol style='padding: 3px;'><li>"
+ "<span style='color: #000000;'>The file does not exist at this path.<br />some file</span></li>"
+ "</ol><br clear=all />"
+ "</html>";
UIManager.put("OptionPane.background", Color.WHITE.brighter());
UIManager.put("Panel.background", Color.WHITE.brighter());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
JPanel panel = new JPanel();
panel.setLayout( new BorderLayout() );
panel.setBackground( Color.WHITE.brighter() );
final JEditorPane editorPane = new JEditorPane("text/html", html);
editorPane.setEditable(false);
//editorPane.setPreferredSize();
editorPane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(editorPane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
JScrollPane scrollPane = new JScrollPane (
editorPane
, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(400,120));
JLabel botlabel = new JLabel();
botlabel.setText(
"<html>"
+ "<br clear=all />"
+ "<h3 style='color: #0033CC; text-align: center; '>"
//+ com.fox.dv.utils.iofile.DoHtml.SPACES
+ "Press Ok to continue or Cancel to exit.</h3>"
+ "</html>"
);
botlabel.setBorder(BorderFactory.createLineBorder( Color.black, 1 ) );
botlabel.setHorizontalTextPosition(JLabel.CENTER);
botlabel.setVerticalAlignment(SwingConstants.CENTER);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(botlabel, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(
null
, panel
, "Title"
, JOptionPane.ERROR_MESSAGE
);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private static final long serialVersionUID = -2116426209967837157L;
}
因为 Swing Components 对 HTML 的支持是 limited,请为 botlabel
指定 JLabel.CENTER
。
JLabel botlabel = new JLabel("", JLabel.CENTER);
botlabel.setText(
"<html>"
+ "<br clear=all />"
+ "<h3 style='color: #0033CC'>"
+ "Press Ok to continue or Cancel to exit.</h3>"
+ "</html>"
);