【问题标题】:Java Swing countrycombobox with icons带有图标的 Java Swing countrycombobox
【发布时间】:2015-01-21 10:57:55
【问题描述】:

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

我真的很感激一个可行的例子。我真的很想知道,对于这样的东西,没有标准选项或好的代码 sn-p(经常使用谷歌在这里找到帮助)。

【问题讨论】:

  • “我非常感谢一个可行的示例。” 我们希望您能提出一个具体的问题以及您的尝试。投票结束,因为 SO 不是代码生成机器。
  • 话虽如此(并投票),您希望查看组合框的渲染器。如果使用JLabel,则可以同时设置文本(国家名称)和图标(标志)。我倾向于将名称和标志图标封装在 Country 类中,并将 Country 对象存储在组合列表中。
  • 嘿,我尝试了很多,我找到的最佳来源是“codejava.net/java-se/swing/create-custom-gui-for-jcombobox”。但这对我不起作用,我不明白为什么......我的问题在哪里不够具体?它只是“如何向组合框添加图标?” :)
  • “我的问题哪里不够具体?” 这是你问的第一个问题。上面的“问题”不包含问题,仅包含您的要求,并且您会欣赏一个工作示例。

标签: java swing combobox icons country


【解决方案1】:

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

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 不起作用,只是为了获得正确的位置。我不能用这个来调整它的大小。

最好的问候 阿卡尼斯

【讨论】:

  • "我用绝对布局在 JPanel 中调用它:" Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不是有利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • 好吧,我先试了一下...但是我没有在任何布局管理器中找到正确的位置...我应该学习使用哪个来处理这些东西:)?
  • “与任何布局管理器。” 没有布局管理器能做好所有事情。我通常将每个视图大约 2-4 个布局管理器混合在一起,有时甚至更多。如果您不知道如何布局 GUI,请询问它。这样做时,以最小尺寸提供 ASCII 艺术或简单的 GUI 布局图,如果可调整大小,则提供更大的宽度和高度。
  • “我应该学哪一个来做这些东西:)?” 所有(虽然你可能会跳过GroupLayout,因为它更适合基于 IDE 的 GUI 设计)。有关它们的最佳教程,请参阅 How to Use Various Layout Managers
  • 嗯,谢谢 :) 现在有了一个不错的 GUI,FormLayout 是基本的,还有一些其他的东西......^^
猜你喜欢
  • 2021-01-23
  • 2013-04-11
  • 1970-01-01
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多