效果图
源代码:
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() ; } }
时间: 2024-10-20 08:51:05