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
使用編輯器建立以下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);
}
}
執行上面範例程式碼,得到以下結果: