首页javajoptionpaneJava Swing - 如何在JOptionPane中显示多行

Java Swing - 如何在JOptionPane中显示多行

我们想知道如何在JOptionPane中显示多行。
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {
  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 : w3cschool.cn" );
    JLabel ageLabel = getLabel("Your Age : 12");
    JLabel yearLabel = getLabel("Your Birth Year : 2004");
    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) {
    new Main().displayGUI();
  }
}