中心JOptionPane图标

时间:2014-04-03 05:17:10

标签: java swing joptionpane

我有一个带有大量文字的JOptionPane ...它比它更宽。这会使图标显示为" float"在窗户高处。

有没有办法让#34;中心"图标?

JOptionPane.showMessageDialog(ProgManager.getMainWindow(),
    ProgManager.getMainWindow().getAboutBoxPane(), 
        Res.getString("title.about"), JOptionPane.INFORMATION_MESSAGE, 
            ProgRes.getImageIcon(ProgRes.MAIN_IMAGE));

我喜欢这个:

看起来更像是:

无需修改图像。

1 个答案:

答案 0 :(得分:3)

  1. 将图片包裹在JLabel中,并将该标签用JPanel包裹GridBagLayout(此布局会使图片/标签保持居中)。

  2. 使用JPanel创建另一个BorderLayout,将图片面板添加到WEST,将文本组件添加到CENTER

  3. 只需将JPanel从2添加到JOptionPane.


  4. 实施例

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    
    public class TestImageCenter {
    
        public static void main(String[] args) {
            ImageIcon icon = new ImageIcon(TestImageCenter.class.getResource("/resources/images/ooooo.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, StackOverfkow"));
            }
    
            JPanel mainPanel = new JPanel(new BorderLayout());
            mainPanel.add(textPanel);
            mainPanel.add(iconPanel, BorderLayout.WEST);
            JOptionPane.showMessageDialog(null, mainPanel, "Center Image Dialog", JOptionPane.PLAIN_MESSAGE);
        }
    }