橘子味的心
标题:Swing JComboBox

JComboBox类是一个组合按钮或可编辑字段和下拉列表的组件。

类声明

以下是javax.swing.JComboBox类的声明 -

public class JComboBox
   extends JComponent
      implements ItemSelectable, ListDataListener, ActionListener, Accessible

字段

以下是javax.swing.JList类的字段 -

  • protected String actionCommand - 此受保护字段是特定于实现的。
  • protected ComboBoxModel dataModel - 此受保护字段是特定于实现的。
  • protected ComboBoxEditor editor - 此受保护字段是特定于实现的。
  • protected boolean isEditable - 此受保护字段是特定于实现的。
  • protected JComboBox.KeySelectionManager keySelectionManager - 此受保护字段是特定于实现的。
  • protected boolean lightWeightPopupEnabled - 此受保护字段是特定于实现的。
  • protected int maximumRowCount - 此受保护字段是特定于实现的。
  • protected ListCellRenderer renderer - 此受保护字段是特定于实现的。
  • protected Object selectedItemReminder - 此受保护字段是特定于实现的。

类构造函数

编号 构造函数 描述
1 JComboBox() 使用默认数据模型创建JComboBox
2 JComboBox(ComboBoxModel aModel) 创建一个JComboBox,从现有的ComboBoxModel获取项目。
3 JComboBox(Object[] items) 创建一个包含指定数组中元素的JComboBox
4 JComboBox(Vector<?> items) 创建一个包含指定Vector中元素的JComboBox

类方法

以下是Swing JComboBox类中的方法列表。请参阅:https://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html

方法继承

该类继承以下类中的方法 -

  • javax.swing.JComponent
  • java.awt.Container中
  • java.awt.Component
  • java.lang.Object

JComboBox示例

使用编辑器创建以下Java程序 -


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JComboBoxExample {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public JComboBoxExample(){
      prepareGUI();
   }
   public static void main(String[] args){
       JComboBoxExample  swingControlDemo = new JComboBoxExample();      
      swingControlDemo.showComboboxDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing JCombox示例");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showComboboxDemo(){                                    
      headerLabel.setText("Control in action: JComboBox"); 
      final DefaultComboBoxModel fruitsName = new DefaultComboBoxModel();

      fruitsName.addElement("Java");
      fruitsName.addElement("Python");
      fruitsName.addElement("MySQL");
      fruitsName.addElement("Perl");

      final JComboBox fruitCombo = new JComboBox(fruitsName);    
      fruitCombo.setSelectedIndex(0);

      JScrollPane fruitListScrollPane = new JScrollPane(fruitCombo);    
      JButton showButton = new JButton("Show");

      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) { 
            String data = "";
            if (fruitCombo.getSelectedIndex() != -1) {                     
               data = "Language Selected: " 
                  + fruitCombo.getItemAt
                  (fruitCombo.getSelectedIndex());             
            }              
            statusLabel.setText(data);
         }
      }); 
      controlPanel.add(fruitListScrollPane);          
      controlPanel.add(showButton);    
      mainFrame.setVisible(true);             
   }
}

执行上面示例代码,得到以下结果:


目录

分类