0

我正在尝试制作一个 JOptionPane,其中包含带有图像和文本的按钮。JLabel 和 JButton 都允许同时显示文本和图像,但是使用 JLabel 会阻止实际按钮显示,而使用 JButton 会使按钮在单击后不执行任何操作。我发现使其工作的唯一方法是单独使用 String 或 ImageIcon ,这显然不是我想要的。

    JLabel[] members = {
       new JLabel(one.name,one.image,JLabel.LEFT), 
       new JLabel(two.name,two.image,JLabel.LEFT),  
       new JLabel(three.name,three.image,JLabel.LEFT), 
       new JLabel(four.name,four.image,JLabel.LEFT), 
       new JLabel("Continue",new ImageIcon("mog.gif"),JLabel.LEFT)};
    JButton[] members = {
       new JButton(one.name,one.image), 
       new JButton(two.name,two.image),  
       new JButton(three.name,three.image), 
       new JButton(four.name,four.image), 
       new JButton("Continue",new ImageIcon("mog.gif"))};
    String[] members = {
       one.name, 
       two.name,  
       three.name, 
       four.name, 
       "Continue"};

        choice= JOptionPane.showOptionDialog(null, "Here are your party members", "Party Members", JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE, new ImageIcon("mog.gif"), members, members[4]);

有谁知道如何解决这个问题?

4

1 回答 1

0

我发现使它工作的唯一方法是单独使用 String 或 ImageIcon

查看Compound IconText Icon。您可以使用图标和文本(表示为图标)创建自定义图标:

import java.awt.*;
import javax.swing.*;

public class OptionPaneButton
{
    private static void createAndShowUI()
    {
        ImageIcon icon = new ImageIcon("About16.gif");
        JButton button = new JButton();
        TextIcon text = new TextIcon(button, "Maybe");
        CompoundIcon compound =
            new CompoundIcon(CompoundIcon.Axis.X_AXIS, button.getIconTextGap(), icon, text);

        Object options[] = {compound, "Not Now", "Go Away"};

        int value = JOptionPane.showOptionDialog(null,
            "Would you like some green eggs to go with that ham?",
            "A Silly Question",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);

        System.out.println(value);

        if (value == JOptionPane.YES_OPTION)
        {
            System.out.println("Maybe");
        }
        else
        {
            System.out.println("Not today");
        }
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }

}
于 2013-03-23T22:02:04.517 回答