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 を単独で使用することです

複合アイコンテキスト アイコンを確認してください。アイコンとテキスト (アイコンとして表される) の両方でカスタム アイコンを作成できます。

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 に答える