📜  Java摇摆 |带有示例的 JComboBox

📅  最后修改于: 2022-05-13 01:55:09.244000             🧑  作者: Mango

Java摇摆 |带有示例的 JComboBox

JComboBox 是Java Swing 包的一部分。 JComboBox 继承 JComponent 类。 JComboBox 显示一个显示列表的弹出菜单,用户可以从该指定列表中选择一个选项。根据程序员的选择,JComboBox 可以是可编辑的或只读的。
JComboBox 的构造函数是:

  1. JComboBox() :创建一个新的空 JComboBox 。
  2. JComboBox(ComboBoxModel M) : 使用来自指定 ComboBoxModel 的项目创建一个新的 JComboBox
  3. JComboBox(E [ ] i) :使用指定数组中的项目创建一个新的 JComboBox。
  4. JComboBox(Vector items) :使用来自指定向量的项目创建一个新的 JComboBox

常用的方法有:

  1. addItem(E item) : 将项目添加到 JComboBox
  2. addItemListener(ItemListener l) : 添加一个 ItemListener 到 JComboBox
  3. getItemAt(int i) : 返回索引 i 处的项目
  4. getItemCount() :返回列表中的项目数
  5. getSelectedItem() :返回被选中的项目
  6. removeItemAt(int i) :删除索引 i 处的元素
  7. setEditable(boolean b) :布尔 b 确定组合框是否可编辑。如果传递 true,则组合框可编辑,反之亦然。
  8. setSelectedIndex(int i) : 在索引 i 处选择 JComboBox 的元素。
  9. showPopup() :使组合框显示其弹出窗口。
  10. setUI(ComboBoxUI ui) :设置渲染该组件的 L&F 对象。
  11. setSelectedItem(Object a) :将组合框显示区域中的选中项设置为参数中的对象。
  12. setSelectedIndex(int a) :选择索引 anIndex 处的项目。
  13. setPopupVisible(boolean v) :设置弹出窗口的可见性。
  14. setModel(ComboBoxModel a) :设置 JComboBox 用于获取项目列表的数据模型。
  15. setMaximumRowCount(int count) :设置 JComboBox 显示的最大行数。
  16. setEnabled(boolean b) :启用组合框以便可以选择项目。
  17. removeItem(Object anObject) :从项目列表中删除一个项目。
  18. removeAllItems() :从项目列表中删除所有项目。
  19. removeActionListener(ActionListener l) :删除一个 ActionListener。
  20. isPopupVisible() :确定弹出窗口的可见性。
  21. addPopupMenuListener(PopupMenuListener l) :添加一个 PopupMenu 监听器,它将监听来自组合框弹出部分的通知消息。
  22. getActionCommand() :返回包含在发送到动作侦听器的事件中的动作命令。
  23. getEditor() :返回用于在 JComboBox 字段中绘制和编辑所选项目的编辑器。
  24. getItemCount() :返回列表中的项目数。
  25. getItemListeners() :返回使用 addItemListener() 添加到此 JComboBox 的所有 ItemListener 的数组。
  26. createDefaultKeySelectionManager() :返回默认键选择管理器的实例。
  27. fireItemStateChanged(ItemEvent e) :通知所有已注册对此事件类型的通知感兴趣的侦听器。
  28. firePopupMenuCanceled() :通知 PopupMenuListeners 组合框的弹出部分已被取消。
  29. firePopupMenuWillBecomeInvisible() :通知 PopupMenuListeners 组合框的弹出部分已变为不可见。
  30. firePopupMenuWillBecomeVisible() :通知 PopupMenuListeners 组合框的弹出部分将变得可见。
  31. setEditor(ComboBoxEditor a) :设置用于在 JComboBox 字段中绘制和编辑所选项目的编辑器。
  32. setActionCommand(String a) :设置应该包含在发送到 actionListeners 的事件中的动作命令。
  33. getUI() :返回呈现此组件的外观对象。
  34. paramString() :返回此 JComboBox 的字符串表示形式。
  35. getUIClassID() :返回呈现此组件的外观类的名称。
  36. getAccessibleContext() :获取与此 JComboBox 关联的 AccessibleContext

以下程序将说明 JComboBox 的使用
1. 程序创建一个简单的 JComboBox 并向其添加元素。

Java
// Java Program to create a simple JComboBox
// and add elements to it
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame implements ItemListener {
 
    // frame
    static JFrame f;
 
    // label
    static JLabel l, l1;
 
    // combobox
    static JComboBox c1;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("frame");
 
        // create a object
        solve s = new solve();
 
        // set layout of frame
        f.setLayout(new FlowLayout());
 
        // array of string containing cities
        String s1[] = { "Jalpaiguri", "Mumbai", "Noida", "Kolkata", "New Delhi" };
 
        // create checkbox
        c1 = new JComboBox(s1);
 
        // add ItemListener
        c1.addItemListener(s);
 
        // create labels
        l = new JLabel("select your city ");
        l1 = new JLabel("Jalpaiguri selected");
 
        // set color of text
        l.setForeground(Color.red);
        l1.setForeground(Color.blue);
 
        // create a new panel
        JPanel p = new JPanel();
 
        p.add(l);
 
        // add combobox to panel
        p.add(c1);
 
        p.add(l1);
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(400, 300);
 
        f.show();
    }
    public void itemStateChanged(ItemEvent e)
    {
        // if the state combobox is changed
        if (e.getSource() == c1) {
 
            l1.setText(c1.getSelectedItem() + " selected");
        }
    }
}


Java
// Java Program to create two  checkbox
// one editable and other read only
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame implements ItemListener {
 
    // frame
    static JFrame f;
 
    // label
    static JLabel l, l1, l3, l4;
 
    // combobox
    static JComboBox c1, c2;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("frame");
 
        // create a object
        solve s = new solve();
 
        // array of string containing cities
        String s1[] = { "Jalpaiguri", "Mumbai", "Noida", "Kolkata", "New Delhi" };
        String s2[] = { "male", "female", "others" };
 
        // create checkbox
        c1 = new JComboBox(s1);
        c2 = new JComboBox(s2);
 
        // set Kolakata and male as selected items
        // using setSelectedIndex
        c1.setSelectedIndex(3);
        c2.setSelectedIndex(0);
 
        // add ItemListener
        c1.addItemListener(s);
        c2.addItemListener(s);
 
        // set the checkbox as editable
        c1.setEditable(true);
 
        // create labels
        l = new JLabel("select your city ");
        l1 = new JLabel("Jalpaiguri selected");
        l3 = new JLabel("select your gender ");
        l4 = new JLabel("Male selected");
 
        // set color of text
        l.setForeground(Color.red);
        l1.setForeground(Color.blue);
        l3.setForeground(Color.red);
        l4.setForeground(Color.blue);
 
        // create a new panel
        JPanel p = new JPanel();
 
        p.add(l);
 
        // add combobox to panel
        p.add(c1);
 
        p.add(l1);
 
        p.add(l3);
 
        // add combobox to panel
        p.add(c2);
 
        p.add(l4);
 
        // set a layout for panel
        p.setLayout(new FlowLayout());
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(400, 400);
 
        f.show();
    }
    public void itemStateChanged(ItemEvent e)
    {
        // if the state combobox 1is changed
        if (e.getSource() == c1) {
 
            l1.setText(c1.getSelectedItem() + " selected");
        }
 
        // if state of combobox 2 is changed
        else
            l4.setText(c2.getSelectedItem() + " selected");
    }
}


Java
// Java  Program to create a checkbox
// and add or remove items from it
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve11 extends JFrame implements ItemListener, ActionListener {
 
    // frame
    static JFrame f;
 
    // label
    static JLabel l, l1;
 
    // combobox
    static JComboBox c1;
 
    // textfield to add and delete items
    static JTextField tf;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("frame");
 
        // create a object
        solve11 s = new solve11();
 
        // set layout of frame
        f.setLayout(new FlowLayout());
 
        // array of string containing cities
        String s1[] = { "Jalpaiguri", "Mumbai", "Noida", "Kolkata", "New Delhi" };
 
        // create checkbox
        c1 = new JComboBox(s1);
 
        // create textfield
        tf = new JTextField(16);
 
        // create add and remove buttons
        JButton b = new JButton("ADD");
        JButton b1 = new JButton("REMOVE");
 
        // add action listener
        b.addActionListener(s);
        b1.addActionListener(s);
 
        // add ItemListener
        c1.addItemListener(s);
 
        // create labels
        l = new JLabel("select your city ");
        l1 = new JLabel("Jalpaiguri selected");
 
        // set color of text
        l.setForeground(Color.red);
        l1.setForeground(Color.blue);
 
        // create a new panel
        JPanel p = new JPanel();
 
        p.add(l);
 
        // add combobox to panel
        p.add(c1);
 
        p.add(l1);
        p.add(tf);
        p.add(b);
        p.add(b1);
 
        f.setLayout(new FlowLayout());
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(700, 200);
 
        f.show();
    }
    // if button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("ADD")) {
            c1.addItem(tf.getText());
        }
        else {
            c1.removeItem(tf.getText());
        }
    }
 
    public void itemStateChanged(ItemEvent e)
    {
        // if the state combobox is changed
        if (e.getSource() == c1) {
 
            l1.setText(c1.getSelectedItem() + " selected");
        }
    }
}


输出 :

2.程序创建两个复选框,一个可编辑,另一个只读

Java

// Java Program to create two  checkbox
// one editable and other read only
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame implements ItemListener {
 
    // frame
    static JFrame f;
 
    // label
    static JLabel l, l1, l3, l4;
 
    // combobox
    static JComboBox c1, c2;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("frame");
 
        // create a object
        solve s = new solve();
 
        // array of string containing cities
        String s1[] = { "Jalpaiguri", "Mumbai", "Noida", "Kolkata", "New Delhi" };
        String s2[] = { "male", "female", "others" };
 
        // create checkbox
        c1 = new JComboBox(s1);
        c2 = new JComboBox(s2);
 
        // set Kolakata and male as selected items
        // using setSelectedIndex
        c1.setSelectedIndex(3);
        c2.setSelectedIndex(0);
 
        // add ItemListener
        c1.addItemListener(s);
        c2.addItemListener(s);
 
        // set the checkbox as editable
        c1.setEditable(true);
 
        // create labels
        l = new JLabel("select your city ");
        l1 = new JLabel("Jalpaiguri selected");
        l3 = new JLabel("select your gender ");
        l4 = new JLabel("Male selected");
 
        // set color of text
        l.setForeground(Color.red);
        l1.setForeground(Color.blue);
        l3.setForeground(Color.red);
        l4.setForeground(Color.blue);
 
        // create a new panel
        JPanel p = new JPanel();
 
        p.add(l);
 
        // add combobox to panel
        p.add(c1);
 
        p.add(l1);
 
        p.add(l3);
 
        // add combobox to panel
        p.add(c2);
 
        p.add(l4);
 
        // set a layout for panel
        p.setLayout(new FlowLayout());
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(400, 400);
 
        f.show();
    }
    public void itemStateChanged(ItemEvent e)
    {
        // if the state combobox 1is changed
        if (e.getSource() == c1) {
 
            l1.setText(c1.getSelectedItem() + " selected");
        }
 
        // if state of combobox 2 is changed
        else
            l4.setText(c2.getSelectedItem() + " selected");
    }
}

输出 :

3. 程序创建一个复选框并从中添加或删除项目。

Java

// Java  Program to create a checkbox
// and add or remove items from it
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve11 extends JFrame implements ItemListener, ActionListener {
 
    // frame
    static JFrame f;
 
    // label
    static JLabel l, l1;
 
    // combobox
    static JComboBox c1;
 
    // textfield to add and delete items
    static JTextField tf;
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("frame");
 
        // create a object
        solve11 s = new solve11();
 
        // set layout of frame
        f.setLayout(new FlowLayout());
 
        // array of string containing cities
        String s1[] = { "Jalpaiguri", "Mumbai", "Noida", "Kolkata", "New Delhi" };
 
        // create checkbox
        c1 = new JComboBox(s1);
 
        // create textfield
        tf = new JTextField(16);
 
        // create add and remove buttons
        JButton b = new JButton("ADD");
        JButton b1 = new JButton("REMOVE");
 
        // add action listener
        b.addActionListener(s);
        b1.addActionListener(s);
 
        // add ItemListener
        c1.addItemListener(s);
 
        // create labels
        l = new JLabel("select your city ");
        l1 = new JLabel("Jalpaiguri selected");
 
        // set color of text
        l.setForeground(Color.red);
        l1.setForeground(Color.blue);
 
        // create a new panel
        JPanel p = new JPanel();
 
        p.add(l);
 
        // add combobox to panel
        p.add(c1);
 
        p.add(l1);
        p.add(tf);
        p.add(b);
        p.add(b1);
 
        f.setLayout(new FlowLayout());
 
        // add panel to frame
        f.add(p);
 
        // set the size of frame
        f.setSize(700, 200);
 
        f.show();
    }
    // if button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("ADD")) {
            c1.addItem(tf.getText());
        }
        else {
            c1.removeItem(tf.getText());
        }
    }
 
    public void itemStateChanged(ItemEvent e)
    {
        // if the state combobox is changed
        if (e.getSource() == c1) {
 
            l1.setText(c1.getSelectedItem() + " selected");
        }
    }
}

输出 :

注意:以上程序可能无法在在线编译器中运行,请使用离线 IDE