代码一:
import javax.swing.JFrame ;
import java.awt.Container ;
import java.awt.event.WindowEvent ;
import java.awt.event.WindowAdapter ;
import java.awt.GridLayout ;
import javax.swing.JComboBox ;
import javax.swing.JLabel ;
import javax.swing.BorderFactory ;
import java.util.Vector ;
class MyComboBox{
private JFrame frame = new JFrame("Welcome TO MLDN") ;
private Container cont = frame.getContentPane() ; // 得到容器
private JComboBox jcb1 = null ;
private JComboBox jcb2 = null ;
public MyComboBox(){
this.frame.setLayout(new GridLayout(2,2)) ; // 定义排版
String nations[] = {"中国","巴西","美国","韩国","意大利","法国"} ;
Vector<String> v = new Vector<String>() ;
v.add("MLDN") ; // 增加内容
v.add("MLDNJAVA") ; // 增加内容
v.add("智囊团") ; // 增加内容
this.jcb1 = new JComboBox(nations) ; // 实例化下拉列表框
this.jcb2 = new JComboBox(v) ; // 实例化下拉列表框
// 为每一个下拉框增加一个边框
this.jcb1.setBorder(BorderFactory.createTitledBorder("你喜欢去那个国家旅游?")) ;
this.jcb2.setBorder(BorderFactory.createTitledBorder("你喜欢的网站?")) ;
cont.add(this.jcb1) ;
cont.add(this.jcb2) ;
cont.add(new JLabel("下拉列表框")) ;
this.frame.setSize(300,150) ;
this.frame.setVisible(true) ;
this.frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1) ;
}
}) ;
}
}
public class JComboBoxDemo01{
public static void main(String args[]){
new MyComboBox() ;
}
}
代码二:
import javax.swing.JFrame ;
import java.awt.Container ;
import java.awt.event.WindowEvent ;
import java.awt.event.WindowAdapter ;
import java.awt.GridLayout ;
import javax.swing.JComboBox ;
import javax.swing.JLabel ;
import javax.swing.JLabel ;
import javax.swing.AbstractListModel ;
import javax.swing.BorderFactory ;
import javax.swing.ComboBoxEditor ;
import javax.swing.ComboBoxModel ;
import java.util.Vector ;
class MyComboBoxModel extends AbstractListModel implements ComboBoxModel{
private String nations[] = {"中国","巴西","美国","韩国","意大利","法国"} ;
private String item = null ;
public Object getSelectedItem(){ // 得到选择的选项
return this.item ;
}
public void setSelectedItem(Object anItem){ // 设置选项
this.item = (String)anItem ;
}
public int getSize(){
return this.nations.length ;
}
public Object getElementAt(int ind){
return this.nations[ind] ;
}
}
class MyComboBox{
private String defaultMsg = "请选择或输入国家的名称。" ;
private JFrame frame = new JFrame("Welcome TO MLDN") ;
private Container cont = frame.getContentPane() ; // 得到容器
private JComboBox jcb1 = null ;
private JComboBox jcb2 = null ;
public MyComboBox(){
this.frame.setLayout(new GridLayout(2,1)) ; // 定义排版
this.jcb1 = new JComboBox(new MyComboBoxModel()) ; // 实例化下拉列表框
this.jcb1.setEditable(true) ; // 表示可以编辑
// 为每一个下拉框增加一个边框
this.jcb1.setBorder(BorderFactory.createTitledBorder("你喜欢去那个国家旅游?")) ;
ComboBoxEditor editor = null ;
editor = this.jcb1.getEditor() ; /// 得到编辑对象
this.jcb1.configureEditor(editor,defaultMsg) ;// 指定默认值
this.jcb1.setMaximumRowCount(3) ;
cont.add(this.jcb1) ;
cont.add(new JLabel("下拉列表框")) ;
this.frame.setSize(180,150) ;
this.frame.setVisible(true) ;
this.frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1) ;
}
}) ;
}
}
public class JComboBoxDemo02{
public static void main(String args[]){
new MyComboBox() ;
}
}
代码三:
import javax.swing.JFrame ;
import java.awt.Container ;
import java.awt.Font ;
import java.awt.event.WindowEvent ;
import java.awt.event.ActionListener ;
import java.awt.event.ItemListener ;
import java.awt.event.ItemEvent ;
import java.awt.event.ActionEvent ;
import java.awt.event.WindowAdapter ;
import java.awt.GridLayout ;
import javax.swing.JComboBox ;
import javax.swing.JLabel ;
import javax.swing.ComboBoxEditor ;
import javax.swing.BorderFactory ;
import java.util.Vector ;
class MyComboBox implements ItemListener,ActionListener{
private JFrame frame = new JFrame("Welcome TO MLDN") ;
private Container cont = frame.getContentPane() ; // 得到容器
private JComboBox jcb1 = null ;
private JLabel label = new JLabel("www.MLDNJAVA.cn") ; // 定义标签
private String fontSize[] = {"10","12","14","16","18","20","22","24","26","48","72"} ;
public MyComboBox(){
this.frame.setLayout(new GridLayout(2,2)) ; // 定义排版
this.jcb1 = new JComboBox(fontSize) ; // 实例化下拉列表框
this.jcb1.addItemListener(this) ;
this.jcb1.addActionListener(this) ;
this.jcb1.setEditable(true) ; // 表示可以编辑
// 为每一个下拉框增加一个边框
this.jcb1.setBorder(BorderFactory.createTitledBorder("你喜欢去那个国家旅游?")) ;
ComboBoxEditor editor = null ;
editor = this.jcb1.getEditor() ; /// 得到编辑对象
this.jcb1.configureEditor(editor,"12") ;// 指定默认值
this.jcb1.setMaximumRowCount(3) ;
this.changeFontSize(12) ; // 默认值
cont.add(this.jcb1) ;
cont.add(label) ;
this.frame.setSize(300,150) ;
this.frame.setVisible(true) ;
this.frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1) ;
}
}) ;
}
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED){ // 判断是否是选中
String itemSize = (String)e.getItem() ;
try{
this.changeFontSize(Integer.parseInt(itemSize)) ;
}catch(Exception e1){
}
}
}
public void actionPerformed(ActionEvent e){
String item = (String)this.jcb1.getSelectedItem() ;
int size = 12 ; // 设置一个默认大小
try{
size = Integer.parseInt(item) ;
}catch(Exception e1){}
this.changeFontSize(size) ;
// 判断给定的size是否存在,如果不存在则需要将新的内容设置上去
if(!this.isExists(item))
{
this.jcb1.addItem(jcb1.getSelectedItem()) ;
}
}
public boolean isExists(String item){
boolean flag = false ;
for(int i=0;i<this.jcb1.getItemCount();i++){
if(item.equals(this.jcb1.getItemAt(i))){
flag = true ;
}
}
return flag ;
}
public void changeFontSize(int size){
System.out.println("*************" + size) ;
Font fnt = new Font("Serief",Font.BOLD,size) ;
this.label.setFont(fnt) ;
}
}
public class JComboBoxDemo03{
public static void main(String args[]){
new MyComboBox() ;
}
}