Java 如何在 JOptionPane 中显示多行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19342681/
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-08-12 16:17:18  来源:igfitidea点击:

How do display multiple lines in JOptionPane?

javaswingjoptionpane

提问by Hani Shah

How do I display all of this information here in just one dialog? Every time I run the file different dialog boxes appears, I really need them to appear in just one dialog box with all the information.

如何在一个对话框中显示所有这些信息?每次运行文件时都会出现不同的对话框,我真的需要它们只出现在一个包含所有信息的对话框中。

JOptionPane.showMessageDialog(null,"Your Name:"+a1,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your age:"+age,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your Birth year:"+a3,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your Height:"+H,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your Weight:"+W,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your BMI:"+BMI,"Output",JOptionPane.INFORMATION_MESSAGE );

回答by Maroun

You can use HTMLtags:

您可以使用HTML标签:

JOptionPane.showMessageDialog(null, "<html><br>First line.<br>Second line.</html>");

Or you can pass an array of objects:

或者你可以传递一个对象数组:

An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack

对象数组被解释为以垂直堆栈排列的一系列消息(每个对象一个)

As mentioned in the docs.

文档中所述。

回答by Hymanrabbit

Have a look at the Javadoc for JOptionPane, specifically the message part at the top. If you pass in an array of Object, its elements will be put in a vertical stack in the dialog.

查看JOptionPaneJavadoc,特别是顶部的消息部分。如果传入一个 Object 数组,它的元素将被放置在对话框中的垂直堆栈中。

JOptionPane.showMessageDialog(null,
                              new Object[]{"line 1","line 2","line 3","line 4"},
                              JOptionPane.INFORMATION_MESSAGE);

回答by nIcE cOw

Simply create a JPanelwith proper layout, as you want to place the components to be placed, and then add this JPanelto the JOptionPaneto display the message.

只需创建JPanel具有适当布局的一个,因为您要放置要放置的组件,然后将其添加JPanelJOptionPane以显示消息。

A small example to help you understand the logic thingy :

一个帮助你理解逻辑的小例子:

import java.awt.*;
import javax.swing.*;

public class JOptionPaneExample {
    private String name;
    private int age;
    private int birthYear;

    public JOptionPaneExample() {
        name = "Myself";
        age = 19;
        birthYear = 1994;
    }

    private void displayGUI() {
        JOptionPane.showMessageDialog(
            null, getPanel(), "Output : ",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private JPanel getPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
        JLabel nameLabel = getLabel("Your Name : " + name);
        JLabel ageLabel = getLabel("Your Age : " + age);
        JLabel yearLabel = getLabel("Your Birth Year : " + birthYear);
        panel.add(nameLabel);
        panel.add(ageLabel);
        panel.add(yearLabel);

        return panel;
    }

    private JLabel getLabel(String title) {
        return new JLabel(title);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new JOptionPaneExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

OUTPUT :

输出 :

optionpaneexample

选项窗格示例

回答by Filipe Pinhati

Just enter this instead of all those lines:

只需输入这个而不是所有这些行:

JOptionPane.showMessageDialog(null,"Your Name: " + a1 +"\n Your age: " + age +"\n Your Birth year: "+ a3 +
    "\n Your Height: " + H +"\n Your Weight: "+ W +"\n Your BMI: "+ BMI);