论坛首页 Java企业应用论坛

Swing板块第一帖-JComboBox的使用

浏览 6598 次
精华帖 (0) :: 良好帖 (2) :: 新手帖 (5) :: 隐藏帖 (4)
作者 正文
   发表时间:2009-05-05   最后修改:2009-05-05
第一个帖子,发点什么呢,就分享一个Icon图标化,且下拉菜单中带级别的JComboBox吧。
首先看一下程序运行的效果:





ImagedComboBoxItem类:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bca.eg.base.ui.combobox;

import javax.swing.Icon;

/**
 *
 * @author  BCA Inc :Edward Gao
 * @contact http://hi.baidu.com/Edwardworld EdwardWorld@126.com
 */
class ImagedComboBoxItem {

    private Icon icon = null;
    private String text = null;
    private int indent = 0;

    ImagedComboBoxItem(String text, Icon icon, int indent) {
        this.text = text;
        this.icon = icon;
        this.indent = indent;
    }

    public String getText() {
        return text;
    }

    public Icon getIcon() {
        return icon;
    }

    public int getIndent() {
        return indent;
    }
}



JImagedComboBox类:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bca.eg.base.ui.combobox;

import java.awt.Component;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 *
 * @author  BCA Inc :Edward Gao
 * @contact http://hi.baidu.com/Edwardworld EdwardWorld@126.com
 */
public class JImagedComboBox extends JComboBox {

    public JImagedComboBox(Vector values) {
        super(values);
        ListCellRenderer comboBoxRenderer = new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(
                    JList list,
                    Object value,
                    int index,
                    boolean isSelected,
                    boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof ImagedComboBoxItem) {
                    ImagedComboBoxItem item = (ImagedComboBoxItem) value;
                    this.setText(item.getText());
                    this.setIcon(item.getIcon());
                    if (isPopupVisible()) {
                        int offset = 10 * item.getIndent();
                        this.setBorder(BorderFactory.createEmptyBorder(0, offset, 0, 0));
                    }
                }
                return this;
            }
        };
        this.setRenderer(comboBoxRenderer);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(500, 200);
        frame.setLayout(new java.awt.FlowLayout());
        frame.setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE );
        frame.setTitle("Edward Swing Test - JComboBox Test");

        Vector values = new Vector();
        Icon openIcon = new ImageIcon(JImagedComboBox.class.getResource("/bca/stocktss/resources/icon/InitCti.gif"));
        Icon newIcon = new ImageIcon(JImagedComboBox.class.getResource("/bca/stocktss/resources/icon/InitCti.gif"));

        ImagedComboBoxItem a1 = new ImagedComboBoxItem("ffff",openIcon, 0);
        ImagedComboBoxItem a2 = new ImagedComboBoxItem("dddd",newIcon, 3);

        values.add(a1);
        values.add(a2);

        JLabel myLabel = new JLabel();
        myLabel.setText("这是Label显示的文字!");

        JImagedComboBox comboBox = new JImagedComboBox( values );
        frame.add(comboBox);
        frame.add(myLabel);

        
        
        frame.setVisible(true);
    }
}


  • 大小: 150.2 KB
  • 大小: 46.1 KB
  • 大小: 168.2 KB
   发表时间:2009-05-05  
呵呵 swing 板块开了
关注swing 关注你的系列帖子
0 请登录后投票
   发表时间:2009-05-05  
其实我是很不希望别人来学习Swing的,因为如果大家都学Swing,我的饭碗就不保了,呵呵,所以为了自私地拥有Swing"金贵"身份,我不希望大家学习Swing,Swing复杂难懂,运行缓慢,中国运用少之又少,而且学习资料也很匮乏,所以大家不要学习Swing了,没有”钱途“的。
0 请登录后投票
   发表时间:2009-05-05  
看出来喽主对swing知识很了解,也想学学swing了,可惜现在有更重要的需要学习。先留影,以后再一起研究
0 请登录后投票
   发表时间:2009-05-05  
真没想到,楼主这么热爱swing....
顶这第一贴
0 请登录后投票
   发表时间:2009-05-05  
呵呵,来观瞻一下,遮掩了半天,还是看出了是 证券电话行销系统
0 请登录后投票
   发表时间:2009-05-05  
EdwardWorld 写道
其实我是很不希望别人来学习Swing的,因为如果大家都学Swing,我的饭碗就不保了,呵呵,所以为了自私地拥有Swing"金贵"身份,我不希望大家学习Swing,Swing复杂难懂,运行缓慢,中国运用少之又少,而且学习资料也很匮乏,所以大家不要学习Swing了,没有”钱途“的。

你应该搞个swingeye去
0 请登录后投票
   发表时间:2009-05-05  
shiren1118 写道
EdwardWorld 写道
其实我是很不希望别人来学习Swing的,因为如果大家都学Swing,我的饭碗就不保了,呵呵,所以为了自私地拥有Swing"金贵"身份,我不希望大家学习Swing,Swing复杂难懂,运行缓慢,中国运用少之又少,而且学习资料也很匮乏,所以大家不要学习Swing了,没有”钱途“的。

你应该搞个swingeye去

正有此意,不过不是SwingEye,而是APP GUI WEBSITE。
0 请登录后投票
   发表时间:2009-05-09  
呵呵! Swing版终于开了。
楼主精神可加,我也非常的喜欢Swing,只是学习的时间比较少。
很多自定义组件的方法根本没时间去研究,还只是停留在会用已有组件的层面上。
我买了本 SwingHacks 里面自定义组件的方法很不错。现在正在看呢!
0 请登录后投票
   发表时间:2009-05-10   最后修改:2009-05-10
EdwardWorld 写道
其实我是很不希望别人来学习Swing的,因为如果大家都学Swing,我的饭碗就不保了,呵呵,所以为了自私地拥有Swing"金贵"身份,我不希望大家学习Swing,Swing复杂难懂,运行缓慢,中国运用少之又少,而且学习资料也很匮乏,所以大家不要学习Swing了,没有”钱途“的。



我最近也开始学习swing了,jdk6下感觉速度比以前快了不少。

楼主界面的排版做的不错,用的什么布局?

顺便问lz一个问题,jcombobox的datamodel显示一个vector的对象元素的某个string字段有没简易的方法?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics