How to use the combo box (JComboBox):
A combo box (JCOMBOBOX) is a combination of a text box and a list that you can enter in the text box, or you can click the drop-down button to select from the list that appears.
Common construction methods for combo boxes:
JComboBox (): Set up a JComboBox object with no options.
JComboBox (Jcomboboxmodel AModel): Establishes a JComboBox object with the data model.
JComboBox (Object[]items): Creates a JComboBox object with an array object.
The other common methods of a combo box are the following:
- AddItem (Object obj): Adds an option to the combo box.
- GetItemCount (): Gets the total number of entries for the combo box.
- RemoveItem (Object ob): Deletes the specified option.
- Removeitemat (int index): Removes the option for the specified index.
- Insertitemat (Object ob,int Index): Inserts an entry at the specified index.
- Getselectedindex (): Gets the index value of the selected item (starting at 0).
- GetSelectedItem (): Gets the contents of the selected item.
- Seteditable (Boolean B): set to editable. The default state of a combo box is not editable, and you need to call this method to be editable to respond to the select input event.
There are two types of events that occur on JComboBox objects. One is the user selected item, and the event responder gets the item selected by the user. Second, the user enters the item and presses the ENTER key, and the event responder reads the user's input. The interface for the first type of event is itemlistener; the second type of event is the input event, and the interface is ActionListener.
Importjava.awt.BorderLayout;ImportJava.awt.Container;ImportJava.awt.Font;ImportJava.awt.Image;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJavax.swing.ImageIcon;ImportJavax.swing.JButton;ImportJavax.swing.JComboBox;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;ImportJavax.swing.JScrollPane;ImportJavax.swing.JTextArea; Public classFonttestextendsJFrameImplementsActionListener { Public Static voidMain (string[] args) {JFrame frame=Newfonttest (); Frame.setsize (800, 200); Frame.setvisible (true);} Public Staticfont font;PrivateJButton Btngo;//The main class inherits the JFrame directly and implements the ActionListener interface.Private Static Final LongSerialversionuid = -2344998755780835481l;//gets the current environment variablePrivateJava.awt.GraphicsEnvironment env =java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment ();//font name, style, size drop-down boxPrivateJComboBox cbfontname, Cbfontstyle, cbfontheight;//text edit boxPrivateJTextArea Taeditor; Publicfonttest () {Super("Font");//inheriting the constructor of the parent class This. SetSize (500, 280);//Set Window Size This. Setlocationrelativeto (NULL);//Center window ScreenImage img =NewImageIcon ("src/icon/font. JPG"). GetImage (); This. Seticonimage (IMG); Container Container= This. Getcontentpane ();//get the name of all available fonts in the system and join the drop-down boxstring[] FontNames =env.getavailablefontfamilynames (); Cbfontname=NewJComboBox (fontnames);//Add three font styles to the drop-down boxString[] Fontstyles = {"Normal", "bold", "italic"};cbfontstyle=NewJComboBox (fontstyles);//Add the 12~21 font size to the drop-down boxstring[] Fontheights =NewString[10]; for(inti = 12; I < 22; i++) Fontheights[i-12] =NewString (integer.tostring (i)); Cbfontheight=NewJComboBox (fontheights);//Create the Execute button and join the ListenerBtngo =NewJButton ("Apply Font"); Btngo.setactioncommand ("Apply Font"); Btngo.addactionlistener ( This);//Create a panel, put three drop-down boxes, buttons to put in, the panel default is FlowLayoutJPanel Panetop =NewJPanel ();p anetop.add (cbfontname);p anetop.add (Cbfontstyle);p anetop.add (cbfontheight);p anetop.add (Btngo);//Create a text box and put it in the scroll barTaeditor =NewJTextArea (); Taeditor.append ("Learn Java Well"); JScrollPane SP=NewJScrollPane (taeditor);//adding components to the windowContainer.add (Panetop, Borderlayout.page_start); Container.add (sp, borderlayout.center);} Public voidactionPerformed1 (ActionEvent event) {if(Event.getactioncommand (). Equals ("Apply Font")) {//determine if the action is from a button//get the font nameString FontName =Cbfontname.getselecteditem (). toString ();//get font style, default is "normal"intFontStyle;Switch(Cbfontstyle.getselectedindex ()) { Case0: FontStyle=Font.plain; Break; Case1: FontStyle=Font.Bold; Break; Case2: FontStyle=Font.Italic; Break;default: FontStyle=Font.plain; Break;}//Get font sizeintFontheight =Integer.parseint (Cbfontheight.getselecteditem (). toString ());//Create a new fontFont =NewFont (FontName, FontStyle, fontheight);//text box Apply new fontTaeditor.setfont (font);}} @Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub}}
Java Second Experiment