码迷,mamicode.com
首页 > 编程语言 > 详细

java swing model实现JComboBox

时间:2015-04-07 12:10:56      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:jcombobox   swing   

效果图

技术分享


源代码:

import java.awt.Container ;
import java.awt.GridLayout ;
import java.awt.Font ;
import java.awt.event.WindowListener ;
import java.awt.event.WindowEvent ;
import java.awt.event.WindowAdapter ;
import javax.swing.JFrame ;
import javax.swing.JLabel ;
import javax.swing.JComboBox ;
import javax.swing.ComboBoxEditor ;
import javax.swing.AbstractListModel ;
import javax.swing.ComboBoxModel ;
import javax.swing.BorderFactory ;
import java.util.Vector ;

class MyComboBoxModel extends AbstractListModel implements ComboBoxModel
{
   String nations[] = {"天朝","苞米","三胖","奥观海","三哥"} ;
   String item = null;
   public Object getSelectedItem()
   {
        return this.item ;
   }
  
   public void setSelectedItem(Object anItem)
   {
       this.item = (String)anItem ;
   }
  
   public Object getElementAt(int in)
  {
     return this.nations[in] ;
  }
  
  public int getSize()
  {
      return this.nations.length ;
  } 
} ;

class MyComboBox
{
    private String defaultMsg = "输入或者选择国籍" ;
    private JFrame frame = new JFrame("投胎系统") ;
    private Container con = frame.getContentPane() ;
    private JComboBox jcb1 = null ;
    Font fnt = new Font("楷体",Font.BOLD,20) ;
    //private JComboBox jcb2 = null ;
   
    public MyComboBox()
    {
       this.frame.setLayout(new GridLayout(2,1)) ;
      
       this.jcb1 = new JComboBox(new MyComboBoxModel()) ;
       this.jcb1.setEditable(true) ;
      
       jcb1.setBorder(BorderFactory.createTitledBorder("请选择投胎国籍")) ;
       ComboBoxEditor editor = null;
       editor = this.jcb1.getEditor() ;
       this.jcb1.configureEditor(editor,defaultMsg) ;
       this.jcb1.setMaximumRowCount(4) ;    //设置最大显示选项
      
       con.add(this.jcb1) ;
       con.add(new JLabel("赢在起跑线!")) ;
      
       this.frame.setSize(180,150) ;
       this.frame.setVisible(true) ;
      
      
    }
}

class Tester
{
   public static void main(String args[])
   {
        new MyComboBox() ;
   }
}



java swing model实现JComboBox

标签:jcombobox   swing   

原文地址:http://blog.csdn.net/u012566693/article/details/44916405

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