Java 如何按“对齐多行JOptionPane输出对话框消息”:&引用;?
我需要使用JOptionPane.showdialogmessage()将输出分成3行,并通过“:”对齐: *请注意“:”的对齐方式 我曾尝试在每个“:”之前手动添加空格,但这三行仍然没有严格对齐:Java 如何按“对齐多行JOptionPane输出对话框消息”:&引用;?,java,string-formatting,joptionpane,showdialog,Java,String Formatting,Joptionpane,Showdialog,我需要使用JOptionPane.showdialogmessage()将输出分成3行,并通过“:”对齐: *请注意“:”的对齐方式 我曾尝试在每个“:”之前手动添加空格,但这三行仍然没有严格对齐: String output = String.format("Total manufacturing cost :$%.2f\n", manufactCost) + String.format(&
String output = String.format("Total manufacturing cost :$%.2f\n", manufactCost) +
String.format("Number of units manufactured : $%d\n", numManufacted) +
String.format("Cost per unit : $%.2f\n", unitCost);
JOptionPane.showMessageDialog(null, output);
以下是输出屏幕截图:
实现这种对齐的最简单方法是什么?
谢谢
附:这是我的第一篇文章。对不起,我仍然在为如何以一种向你展示输出正确格式的方式编辑文章而挣扎…我已经被格式逼疯了。。。所以我只是发布了截图。。。lol如果您将标签文本设置为以
“
开头,则文本将被解释为非常基本的早期HTML。因此,您可以插入表格或其他对齐方式。如果可以使用单间距字体:“单倍行距”
字体(默认字体不是单倍行距的,因此大多数情况下不可能对齐)
String.format
根据需要格式化字符串使用
JPanel
作为JOptionPane
的消息,您可以在那里使用布局管理器,如GridBagLayout
(或GridLayout
,易于使用)
Tom的答案,使用HTML,也很容易使用(可能是最简单的解决方案)避免使用单间距字体或奇怪的技巧。
构建一个
JPanel
,它使用GridBagLayout
,记住showMessageDialog
接受其他组件,而不仅仅是文本
相关代码。我为:
选择了一个单独的标签,这样您就可以根据自己的喜好对其进行自定义,同时保持对齐
final JPanel panel = new JPanel(new GridBagLayout());
final GridBagConstraints gc = new GridBagConstraints();
final Insets descriptionInsets = new Insets(3, 5, 3, 15);
final Insets valuesInsets = new Insets(3, 2, 3, 2);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.anchor = GridBagConstraints.NORTH;
gc.insets = descriptionInsets;
gc.weightx = 1.0;
panel.add(new JLabel("Total cost"), gc);
gc.insets = valuesInsets;
gc.weightx = 0;
gc.gridx = 1;
panel.add(new JLabel(":"), gc);
gc.gridx = 2;
panel.add(new JLabel("$11175.00"), gc);
gc.insets = descriptionInsets;
gc.weightx = 1.0;
gc.gridx = 0;
gc.gridy = 1;
panel.add(new JLabel("Number of units"), gc);
gc.insets = valuesInsets;
gc.weightx = 0;
gc.gridx = 1;
panel.add(new JLabel(":"), gc);
gc.gridx = 2;
panel.add(new JLabel("500"), gc);
gc.insets = descriptionInsets;
gc.weightx = 1.0;
gc.gridx = 0;
gc.gridy = 2;
panel.add(new JLabel("Cost per unit"), gc);
gc.insets = new Insets(3, 2, 3, 2);
gc.weightx = 0;
gc.gridx = 1;
panel.add(new JLabel(":"), gc);
gc.gridx = 2;
panel.add(new JLabel("$22.35"), gc);
JOptionPane.showMessageDialog(frame, panel);
@luhaoGeopy-我编辑了你的问题以格式化输出-这只是一个选项如何做-最终你可以删除图像(并根据你的意愿编辑问题)((文本最好以这样的方式发布,而不是(仅)以图像的形式发布-对话框图像可以保留在IMO中))谢谢你对我的帖子进行格式化!我学到了一些东西,谢谢你!这是否意味着无法在JOptionPane.showdialogmessage()中解决此问题?我可能错了,但我认为这是一个字符串格式问题,如果你看一下我用JOptionPane.showMessageDialog()
解决的代码,它是?@luhaoGeopy,但是我没有传入字符串,但是另一个JPanel
具有特殊布局。@luhaoGeopy您无法构建String
并获得正确的布局,因为每个字体和每个系统都不同。@luhaoGeopy如果需要澄清,请告诉我。如果你不知道,记得接受答案。非常感谢!
final JPanel panel = new JPanel(new GridBagLayout());
final GridBagConstraints gc = new GridBagConstraints();
final Insets descriptionInsets = new Insets(3, 5, 3, 15);
final Insets valuesInsets = new Insets(3, 2, 3, 2);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.anchor = GridBagConstraints.NORTH;
gc.insets = descriptionInsets;
gc.weightx = 1.0;
panel.add(new JLabel("Total cost"), gc);
gc.insets = valuesInsets;
gc.weightx = 0;
gc.gridx = 1;
panel.add(new JLabel(":"), gc);
gc.gridx = 2;
panel.add(new JLabel("$11175.00"), gc);
gc.insets = descriptionInsets;
gc.weightx = 1.0;
gc.gridx = 0;
gc.gridy = 1;
panel.add(new JLabel("Number of units"), gc);
gc.insets = valuesInsets;
gc.weightx = 0;
gc.gridx = 1;
panel.add(new JLabel(":"), gc);
gc.gridx = 2;
panel.add(new JLabel("500"), gc);
gc.insets = descriptionInsets;
gc.weightx = 1.0;
gc.gridx = 0;
gc.gridy = 2;
panel.add(new JLabel("Cost per unit"), gc);
gc.insets = new Insets(3, 2, 3, 2);
gc.weightx = 0;
gc.gridx = 1;
panel.add(new JLabel(":"), gc);
gc.gridx = 2;
panel.add(new JLabel("$22.35"), gc);
JOptionPane.showMessageDialog(frame, panel);