博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JComboBox
阅读量:6501 次
发布时间:2019-06-24

本文共 2568 字,大约阅读时间需要 8 分钟。

 

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);    }}

 

转载地址:http://ebvyo.baihongyu.com/

你可能感兴趣的文章
使用PostgreSQL 9.6 架设mediawiki服务器
查看>>
数据库服务器硬件对性能的影响
查看>>
LVM
查看>>
windows+群辉服务器环境下,搭建git版本管理
查看>>
Boolean类型
查看>>
Ubuntu 修改源
查看>>
php 几个比较实用的函数
查看>>
(译)OpenGL ES2.0 – Iphone开发指引
查看>>
@RestController 与 @RequestMapping
查看>>
黑马程序员.bobo.DAY.1
查看>>
Unity shader 官网文档全方位学习(二)
查看>>
pbrun
查看>>
浏览器加载和渲染网页顺序
查看>>
微服务架构springcloud
查看>>
深入剖析Android系统试读样章
查看>>
测试用例出错重跑--flaky插件
查看>>
yaf的安装
查看>>
比较java与C++的不同
查看>>
Twitter Storm入门
查看>>
使用scikit-learn进行文本分类
查看>>