中易网

麻烦解释一下这个java程序

答案:1  悬赏:70  
解决时间 2021-05-12 22:15

public class CustomComboBoxDemo extends JPanel {
    ImageIcon[] images;
    String[] petStrings = {"Bird", "Cat", "Dog", "Rabbit", "Pig"};

       public CustomComboBoxDemo() {
        super(new BorderLayout());

        images = new ImageIcon[petStrings.length];
        Integer[] intArray = new Integer[petStrings.length];
        for (int i = 0; i < petStrings.length; i++) {
            intArray[i] = new Integer(i);
            images[i] = createImageIcon("images/" + petStrings[i] + ".gif");
            if (images[i] != null) {
                images[i].setDescription(petStrings[i]);
            }
        }

        JComboBox petList = new JComboBox(intArray);
        ComboBoxRenderer renderer= new ComboBoxRenderer();
        renderer.setPreferredSize(new Dimension(200, 130));
        petList.setRenderer(renderer);
        petList.setMaximumRowCount(3);

        add(petList, BorderLayout.PAGE_START);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = CustomComboBoxDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
                return null;
        }
    }

       private static void createAndShowGUI() {
        
        JFrame frame = new JFrame("CustomComboBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     
        JComponent newContentPane = new CustomComboBoxDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    class ComboBoxRenderer extends JLabel implements ListCellRenderer {
        private Font uhOhFont;

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

         public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
           

            int selectedIndex = ((Integer)value).intValue();

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

             ImageIcon icon = images[selectedIndex];
            String pet = petStrings[selectedIndex];
            setIcon(icon);
            if (icon != null) {
                setText(pet);
                setFont(list.getFont());
            } else {
                setUhOhText(pet + " (no image available)",
                            list.getFont());
            }

            return this;
        }

         protected void setUhOhText(String uhOhText, Font normalFont) {
            if (uhOhFont == null) { //lazily create this font
                uhOhFont = normalFont.deriveFont(Font.ITALIC);
            }
            setFont(uhOhFont);
            setText(uhOhText);
        }
    }
}

这个程序有些看不懂,麻烦哪位给我把每步注明一下,特别是

 class ComboBoxRenderer   和 protected void setUhOhText(String uhOhText, Font normalFont) 

里面的内容。最好讲详细点啊。

最佳答案

java   的Swing ...我很久没接触了


我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
谁能给个QQ爱墙种子啊?谢谢~\(≧▽≦)/~啦啦
唉,为什么人的心情会郁闷呢?
各位路过的Les们,能否进来一下?
为什么童音比较好听勒?
love的意思是什么啊?
初父母外谁会给我人名币?
msn消息提示音WAV格式的
中国石油鑫卓站这个地址在什么地方,我要处理
<最后一课>中如果阿尔萨斯终于回到了法国的怀
QQ被盗问题(急)怎样才找得回阿!
电脑上哪个本地磁盘是硬盘
求梦幻西游找回账号方法
创新7.1声卡玩劲唱团驱动问题
谁帮我找个手机
COCOYOYO婴童生活我想知道这个在什么地方
推荐资讯
如果我伤害了一个女孩,我应该怎么办?
求深圳哪里买开关比较便宜实惠?
爱上一个人了是不是不管他为自己做什么。哪怕
襄阳洁利斯水暖洁具有限公司地址在什么地方,
人一生为什么有这么多烦恼事呢!
今天有多少货
四川4最上加满智力混沌法杖值多少?
为什么开启QQ音乐而QQ号上不显示正在收听歌曲
小小大乱斗模式
小孩子和朋友自己出去旅游注意什么呀
如果当初的密保问题忘了,想在要改密码怎么办
酒泉市物价检查所在什么地方啊,我要过去处理
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?