zoukankan      html  css  js  c++  java
  • JComboBox

    package swing.combox;
    
    import java.awt.FlowLayout;
    
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    
    public class JcomboxDemo extends JFrame{
        private static final long serialVersionUID = 1L;
        
        public JcomboxDemo() {
            
            this.setLayout(new FlowLayout());
            JComboBox combox=new JComboBox();
            
            Object[] types={"123","类型","xyz"};
            
            Object source=types;
            DefaultComboBoxModel model=new DefaultComboBoxModel((Object[]) source);
            combox.setModel(model);
            
            this.add(combox);
            combox.setSelectedItem("类型");
            
            this.pack();
            this.setSize(800, 600);
            this.setLocationRelativeTo(null);
            this.setVisible(true);
            
        }
    
        public static void main(String[] args) {
            new JcomboxDemo();
        }
    
    }
    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);
        }
    
    }
  • 相关阅读:
    状态压缩+枚举 UVA 11464 Even Parity
    数学/思维 UVA 11300 Spreading the Wealth
    贪心 UVA 11729 Commando War
    二分专题
    二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil
    数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
    贪心 Codeforces Round #191 (Div. 2) A. Flipping Game
    DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
    递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries
    递推DP Codeforces Round #260 (Div. 1) A. Boredom
  • 原文地址:https://www.cnblogs.com/softidea/p/4545228.html
Copyright © 2011-2022 走看看