How do display multiple lines in JOptionPane?

孤街浪徒 提交于 2020-01-13 19:00:13

问题


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 );

回答1:


You can use HTML tags:

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.




回答2:


Simply create a JPanel with proper layout, as you want to place the components to be placed, and then add this JPanel to the JOptionPane to display the message.

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 :




回答3:


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.

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



回答4:


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); 


来源:https://stackoverflow.com/questions/19342681/how-do-display-multiple-lines-in-joptionpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!