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);
}
}
執行上面示例代碼,得到以下結果: