This article is your own study notes, welcome reprint, but please indicate the source: http://blog.csdn.net/jesson20121020
similar to the functionality of a set of radio buttons, a combo box (drop-down list) also forces users to select only one of a set of possible elements, but this approach is more compact and makes it easier to change the contents of a drop-down list without confusing the user.
In the following example, the JComboBox combo box starts with some elements, and then when a button is pressed, a new element is added to the combo box.
public class ComboBoxes extends JFrame {private string[] weekdays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};p rivate JTextField t = new JTextField;p rivate JComboBox c = new JComboBox ();p rivate JButton b = new JButton ("ADD items");p Riva Te int count = 0;public comboboxes () {//TODO auto-generated constructor Stubsetlayout (new FlowLayout ()); SetSize (200,175) ; setvisible (true); for (int i = 0; i < 3;i++) C.additem (weekdays[count++]); t.seteditable (false); B.addactionlistener ( New ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method stubif (Count < ; Weekdays.length) {C.additem (weekdays[count++]);}}); C.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated Method Stubt.settext ("You have selected" + C.getselecteditem ());}); C.seteditable (True); add (t); add (c); add (b);} /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubnew comboboxes ();}}
The effect is as follows:
When you click an option in the combo box, the selected results are displayed in JTextField.
Java Interface Programming (8)------combo box (drop-down list)