首页javajoptionpaneJava Swing - 如何中心JOptionPane图标

Java Swing - 如何中心JOptionPane图标

我们想知道如何中心JOptionPane图标。
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.net.URL;

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

public class Main {

  public static void main(String[] args) throws Exception {
    ImageIcon icon = new ImageIcon(new URL(
        "http://www.w3cschool.cn/style/download.png"));
    JLabel iconLabel = new JLabel(icon);
    JPanel iconPanel = new JPanel(new GridBagLayout());
    iconPanel.add(iconLabel);

    JPanel textPanel = new JPanel(new GridLayout(0, 1));
    for (int i = 0; i < 15; i++) {
      textPanel.add(new JLabel("Hello"));
    }

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(textPanel);
    mainPanel.add(iconPanel, BorderLayout.WEST);
    JOptionPane.showMessageDialog(null, mainPanel, "Center Image Dialog",
        JOptionPane.PLAIN_MESSAGE);
  }
}