| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 760 人关注过本帖
标题:一段代码不太理解
只看楼主 加入收藏
dukai5302885
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-7-24
收藏
 问题点数:0 回复次数:1 
一段代码不太理解
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * <p>Title: 检查盒演示</p>
 * <p>Description: 选择不同的选择框显示不同的图片</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Filename: CheckBoxDemo.java</p>
 * @author 杜江
 * @version 1.0
 */
public class CheckBoxDemo extends JPanel
                          implements ItemListener {
    JCheckBox chinButton;
    JCheckBox glassesButton;
    JCheckBox hairButton;
    JCheckBox teethButton;

    /*
     * 有四个检查盒,分别对应下巴、眼镜、头发和牙齿
     * 图片不是拼出来的,而是根据检查盒选择拼写图片文件名
     * 图片文件名的定义格式为"geek-XXXX.gif"
     * 其中 XXXX 根据检查盒的不同选择,而不同。它的格式如下:

       ----             //没有选择

       c---             //一个选择
       -g--
       --h-
       ---t

       cg--             //两个选择
       c-h-
       c--t
       -gh-
       -g-t
       --ht

       -ght             //三个选择
       c-ht
       cg-t
       cgh-

       cght             //所有都选
     */

    StringBuffer choices;
    JLabel pictureLabel;

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

        //创建检查盒
        chinButton = new JCheckBox("下巴(c)");
        chinButton.setMnemonic(KeyEvent.VK_C);
        chinButton.setSelected(true);

        glassesButton = new JCheckBox("眼镜(g)");
        glassesButton.setMnemonic(KeyEvent.VK_G);
        glassesButton.setSelected(true);

        hairButton = new JCheckBox("头发(h)");
        hairButton.setMnemonic(KeyEvent.VK_H);
        hairButton.setSelected(true);

        teethButton = new JCheckBox("牙齿(t)");
        teethButton.setMnemonic(KeyEvent.VK_T);
        teethButton.setSelected(true);

        //给检查盒添加监听
        chinButton.addItemListener(this);
        glassesButton.addItemListener(this);
        hairButton.addItemListener(this);
        teethButton.addItemListener(this);

        choices = new StringBuffer("cght");

        //放置一个带图片的标签
        pictureLabel = new JLabel();
        pictureLabel.setFont(pictureLabel.getFont().deriveFont(Font.ITALIC));
        updatePicture();

        //将检查盒放置到面版中
        JPanel checkPanel = new JPanel(new GridLayout(0, 1));
        checkPanel.add(chinButton);
        checkPanel.add(glassesButton);
        checkPanel.add(hairButton);
        checkPanel.add(teethButton);

        add(checkPanel, BorderLayout.LINE_START);
        add(pictureLabel, BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }
/**
 *<br>方法说明:监听检查盒事件,拼凑图片的文件名XXXX部分
 *<br>输入参数:
 *<br>返回类型:
 */
    public void itemStateChanged(ItemEvent e) {
        int index = 0;
        char c = '-';
        Object source = e.getItemSelectable();

        if (source == chinButton) {
            index = 0;
            c = 'c';
        } else if (source == glassesButton) {
            index = 1;
            c = 'g';
        } else if (source == hairButton) {
            index = 2;
            c = 'h';
        } else if (source == teethButton) {
            index = 3;
            c = 't';
        }
        
        //取消选择事件
        if (e.getStateChange() == ItemEvent.DESELECTED) {
            c = '-';
        }

        //改变文件名字XXXX
        choices.setCharAt(index, c);

        updatePicture();
    }
/**
 *<br>方法说明:绘制图片
 *<br>输入参数:
 *<br>返回类型:
 */
    protected void updatePicture() {
        //将得到的图片制成图标
        ImageIcon icon = createImageIcon(
                                    "images/geek/geek-"
                                    + choices.toString()
                                    + ".gif");
        pictureLabel.setIcon(icon);
        pictureLabel.setToolTipText(choices.toString());
        if (icon == null) {
            pictureLabel.setText("没有发现图片");
        } else {
            pictureLabel.setText(null);
        }
    }
/**
 *<br>方法说明:获取图标
 *<br>输入参数:String path 图片路径
 *<br>返回类型:ImageIcon对象
 */
    protected static ImageIcon createImageIcon(String path) {
         imgURL = CheckBoxDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
/**
 *<br>方法说明:主方法
 *<br>输入参数:
 *<br>返回类型:
 */
    public static void main(String s[]) {
         JFrame.setDefaultLookAndFeelDecorated(true);

        //创建一个窗体,
        JFrame frame = new JFrame("CheckBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //创建一个面板
        JComponent newContentPane = new CheckBoxDemo();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        //显示窗体
        frame.pack();
        frame.setVisible(true);
    }
}


虽说它给了注释但是
public void itemStateChanged(ItemEvent e) {
        int index = 0;
        char c = '-';
        Object source = e.getItemSelectable();

        if (source == chinButton) {
            index = 0;
            c = 'c';
        } else if (source == glassesButton) {
            index = 1;
            c = 'g';
        } else if (source == hairButton) {
            index = 2;
            c = 'h';
        } else if (source == teethButton) {
            index = 3;
            c = 't';
        }
        
        //取消选择事件
        if (e.getStateChange() == ItemEvent.DESELECTED) {
            c = '-';
        }

        //改变文件名字XXXX
        choices.setCharAt(index, c);

        updatePicture();
    }

这段代码不是特别清楚!
哪位高手可以帮我详细的分析一下么
搜索更多相关主题的帖子: 杜江 眼镜 import java JCheckBox 
2008-07-24 17:50
jdk2006
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2007-5-12
收藏
得分:0 
public void itemStateChanged(ItemEvent e) {
        int index = 0;
        char c = '-';
        Object source = e.getItemSelectable();//

        if (source == chinButton) {
            index = 0;
            c = 'c';
        } else if (source == glassesButton) {
            index = 1;
            c = 'g';
        } else if (source == hairButton) {
            index = 2;
            c = 'h';
        } else if (source == teethButton) {
            index = 3;
            c = 't';
        }
        
        //取消选择事件
        if (e.getStateChange() == ItemEvent.DESELECTED) {
            c = '-';
        }

        //改变文件名字XXXX
        choices.setCharAt(index, c);

        updatePicture();
    }
这段代码是JCheckBox的选择事件,经行文件名的拼写。文件名形式为4个char,必须满足geek-XXXX形式。而这段代码就是完成这块的监听事件。当你选择chinButton时c=‘c’,否则c='-';其他的也是这样的理解。最后拼成geek-xxxx形式。

2008-07-30 04:24
快速回复:一段代码不太理解
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028016 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved