在JOptionPane中对齐文本

时间:2018-06-10 18:04:17

标签: java swing joptionpane

我有以下代码行

JOptionPane.showMessageDialog(rootPane, "Código incorreto\nPor favor verifique", "Atenção", JOptionPane.ERROR_MESSAGE);

在此处显示此消息

enter image description here

如何将这两行文字居中放在盒子的中央?

2 个答案:

答案 0 :(得分:3)

JLabel label = new JLabel("<html><center>Código incorreto<br>Por favor verifique");
label.setHorizontalAlignment(SwingConstants.CENTER);
JOptionPane.showMessageDialog(null, label, "Atenção", JOptionPane.ERROR_MESSAGE);

试试这个。预览:

enter image description here

答案 1 :(得分:0)

您可以使用以下代码:

String message = "<html><body><div width='100px' align='center'>Código incorreto<br>Por favor verifique</div></body></html>";
JLabel messageLabel = new JLabel(message);
JOptionPane.showConfirmDialog(null, messageLabel, "Atenção", JOptionPane.DEFAULT_OPTION);