Ric*_*les 3 html java swing jlabel alignment
我需要将文本格式化为 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;
}
Run Code Online (Sandbox Code Playgroud)
由于 Swing 组件中对 HTML 的支持有限,因此指定JLabel.CENTERfor botlabel。

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>"
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1707 次 |
| 最近记录: |