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
Align text to the right in JOptionPane
提问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 JPanel
using a GridBagLayout
, put a JLabel
in it with your text, use the righ set of GridBagConstraints
to ensure the text is right-align, and voila ! A JOptionPane with a right-aligned text.
正如... Hovercraft Full Of Eels 所述,您完全可以使用 JComponent 作为JOptionPane#showMessageDialog
. 因此,创建一个JPanel
using 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.
希望这可以帮助。