码迷,mamicode.com
首页 > 其他好文 > 详细

JComboBox

时间:2015-06-02 00:16:48      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

 

package swing.combox;

import static common.SwingConsole.run;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

/*2015-6-1*/
public class ComboxDemo extends JFrame {
    private static final long serialVersionUID = 1L;

    public ComboxDemo() {
        this.setLayout(new FlowLayout());
        /*
         * ComboBoxModel aModel = null; JComboBox box=new JComboBox(aModel);
         */
        final JTextField text=new JTextField("test",25);
        this.add(text);
        
        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig", "Tiger", "Lion", "Egg", "Swan","Goose"};
        // Create the combo box, select the item at index 4.
        final JComboBox petList = new JComboBox(petStrings);
        petList.setSelectedIndex(4);
        this.add(petList);
        petList.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                text.setText(petList.getSelectedItem().toString()+":"+petList.getSelectedIndex());
            }
        });
        
        petList.setSelectedItem("Swan");
        
        JButton button=new JButton("Click");
        button.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                text.setText(petList.getSelectedItem().toString()+":"+petList.getSelectedIndex());
            }
        });
        this.add(button);
        
    }

    public static void main(String[] args) {
        run(new ComboxDemo(), 800, 600);
    }

}

 

JComboBox

标签:

原文地址:http://www.cnblogs.com/softidea/p/4545228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!