java 在 JOptionPane 中将文本向右对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6454887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:57:21  来源:igfitidea点击:

Align text to the right in JOptionPane

javaalignmentarabicjoptionpane

提问by Eng.Fouad

Is it possible to align the text to the right in JOptionPane? (I don't want to use JDialog) because I want to write some sentences in Arabic

是否可以在 JOptionPane 中将文本向右对齐?(我不想使用 JDialog)因为我想用阿拉伯语写一些句子

回答by Hovercraft Full Of Eels

Create a JPanel, align your text in your JPanel and then add the JPanel as the Object parameter of the JOptionPane. Or use a JDialog (why the desire not to use one of these?).

创建一个 JPanel,在 JPanel 中对齐文本,然后添加 JPanel 作为 JOptionPane 的 Object 参数。或者使用 JDialog(为什么不希望使用其中之一?)。

回答by Bala R

 String message = "<html><body><div width='200px' align='right'>This is some text!</div></body></html>";
 JLabel messageLabel = new JLabel(message);
 JOptionPane.showConfirmDialog(null, messageLabel);

回答by Riduidel

As stated by ... Hovercraft Full Of Eels, you perfectly can use a JComponent as message in a JOptionPane#showMessageDialog. As a consequence, create a JPanelusing a GridBagLayout, put a JLabelin it with your text, use the righ set of GridBagConstraintsto ensure the text is right-align, and voila ! A JOptionPane with a right-aligned text.

正如... Hovercraft Full Of Eels 所述,您完全可以使用 JComponent 作为JOptionPane#showMessageDialog. 因此,创建一个JPanelusing a GridBagLayout,将 aJLabel与您的文本一起放入其中,使用正确的集合GridBagConstraints来确保文本右对齐,瞧!带有右对齐文本的 JOptionPane。

回答by adamjmarkham

Try using a JLabel on the JOptionPane and then use HTML to align it.

尝试在 JOptionPane 上使用 JLabel,然后使用 HTML 对齐它。

You can find more about using HTML with Swing components here

您可以在此处找到有关在 Swing 组件中使用 HTML 的更多信息

Hope this helps.

希望这可以帮助。