gpt4 book ai didi

java - 居中 JOptionPane 图标

转载 作者:行者123 更新时间:2023-12-01 23:05:35 25 4
gpt4 key购买 nike

我有一个 JOptionPane,上面有很多文本...它将它拉伸(stretch)得比宽度还高。这会导致图标看起来“漂浮”在窗口的高处。

有没有办法让图标“居中”?

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

我想要这个:

看起来更像:

无需修改图像。

最佳答案

  1. 将图像包装在 JLabel 中,并使用 GridBagLayout 将该标签包装在 JPanel 中(此布局将保留图像/标签居中)。

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

  3. 将 2 中的 JPanel 添加到 JOptionPane。

<小时/>

示例

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

关于java - 居中 JOptionPane 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22828426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com