我希望有人能帮助我。我试图在Java Swing中创建带有图标的“ countrycombobox”。我发现了一些东西,但对我没有任何帮助。也许问题是Iam仍然对Java是“新的”。
我只想这样简单:http://www.zomex.com/libs/images/layout/whmcs-template-language-select-w-flags-eco.jpg
只是国家前面的旗帜。

我真的很感谢一个可行的例子。我真的很奇怪,对于这样的东西,没有标准的选择或好的代码片段(Google经常在这里找到帮助)。

最佳答案

我找到了一个更好的例子,并想与您分享我的东西。只剩下一个问题,我没有确定它的大小。

package view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CountryComboBox extends JPanel {
    ImageIcon[] images;
    String[] imgStrings = {"de"};

    /*
     * Despite its use of EmptyBorder, this panel makes a fine content
     * pane because the empty border just increases the panel's size
     * and is "painted" on top of the panel's normal background.  In
     * other words, the JPanel fills its entire background if it's
     * opaque (which it is by default); adding a border doesn't change
     * that.
     */
    public CountryComboBox() {
        super(new BorderLayout());

        //Load the images and create an array of indexes.
        images = new ImageIcon[imgStrings.length];
        Integer[] intArray = new Integer[imgStrings.length];
        for (int i = 0; i < imgStrings.length; i++) {
            intArray[i] = new Integer(i);
            images[i] = createImageIcon("/res/" + imgStrings[i] + ".png");
            if (images[i] != null) {
                images[i].setDescription(imgStrings[i]);
            }
        }

        //Create the combo box.
        JComboBox imgList = new JComboBox(intArray);
        ComboBoxRenderer renderer= new ComboBoxRenderer();
        imgList.setRenderer(renderer);
        imgList.setMaximumRowCount(3);

        //Lay out the demo.
        add(imgList, BorderLayout.PAGE_START);
        //setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = CountryComboBox.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
                return null;
        }
    }

    class ComboBoxRenderer extends JLabel
                           implements ListCellRenderer {
        private Font uhOhFont;

        public ComboBoxRenderer() {
            setOpaque(true);
            setHorizontalAlignment(CENTER);
            setVerticalAlignment(CENTER);
        }

        /*
         * This method finds the image and text corresponding
         * to the selected value and returns the label, set up
         * to display the text and image.
         */
        @Override
        public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
            //Get the selected index. (The index param isn't
            //always valid, so just use the value.)
            int selectedIndex = ((Integer)value).intValue();

            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }

            //Set the icon and text.  If icon was null, say so.
            ImageIcon icon = images[selectedIndex];
            String img = imgStrings[selectedIndex];
            setIcon(icon);
            if (icon != null) {
                setText(img);
                setFont(list.getFont());
            } else {
                setUhOhText(img + " (no image available)",
                            list.getFont());
            }

            return this;
        }

        //Set the font and text when no image was found.
        protected void setUhOhText(String uhOhText, Font normalFont) {
            if (uhOhFont == null) { //lazily create this font
                uhOhFont = normalFont.deriveFont(Font.ITALIC);
            }
            setFont(uhOhFont);
            setText(uhOhText);
        }
    }
}


我在具有绝对布局的JPanel中调用它:

JComponent newContentPane = new CountryComboBox();
newContentPane.setOpaque(true); //content panes must be opaque
newContentPane.setBounds(10, 75, 50, 26);
contentPane.add(newContentPane);


setBounds无法正常工作,只是为了获得正确的位置。我不能用这个。

最好的祝福
阿卡尼斯

10-04 14:49