Java experience--check, list, scroll, dialog

Source: Internet
Author: User

1. Select Components

A selection component is an AWT component that is specifically used to perform a single or multiple-selection operation from multiple entries.

Μ check box and radio button
check box provides two states: checked | unchecked. The checkbox class in the java.awt package is used to establish a check box.

Construction method
Public Checkbox ()
Public checkbox (String str): Title text to the right of the check box
Public checkbox (String Str,boolean B): B is the initial state of the check box
Public checkbox (String Str,boolean b,checkboxgroupg): Which group the check box belongs to, where the check box is the radio button other methods
Public Boolean getState ();
Public SetState (Boolean B): Sets the state of the check box.
Public Getlabel (): Gets the caption text to the right of the check box.
Public SetLabel (String s);

U Trigger Event
Selecting or canceling a check box triggers the option event itemevent, the corresponding listener interface ItemListener, and the event handling method is:
public void itemstatechanged (ItemEvent e);

registering the Listener/Delete listener method
public void Additemlistener (ItemListener);

Public Voidremoveitemlistener (itemlistener);

2. Drop-down list box Choice

The user can see the first option and the arrow button next to it in the Drop-down list box, and the list of options opens when the user clicks the arrow button.

Construction method
public Choice ();

U Other methods
public void Add (String name): Adds a list item name.
public void Insert (String name, int index);
public void Remove (String name);
public void Remove (int index);
public void RemoveAll ();

U Other methods
public void Select (String name): Sets the name option in the Drop-down list box to the selected state.
public void Select (int index);
public int GetItemCount (): Returns the total number of list items.
public int Getselectedindex (): Returns the index value of the selected list item in the current Drop-down list box, with the index starting at 0.
Public String GetSelectedItem (): Returns the name of the list item selected in the Current Drop-down list box.

U Trigger Event
When a user selects a list item in a Drop-down list box, the ItemEvent option event is triggered and the listener interface: ItemListener. How to register and delete a listener:
public void Additemlistener (ItemListener l);
public void Removeitemlistener (ItemListener l);

the value of index in the above method is non-negative and is less than the total number of list items in the Drop-down list box, otherwise a arrayindexoutofboundsexception exception occurs.

3. Listing box List

contains, for example, a dry list item, in which the user can select one or more items in a list box. When the number of list items in the list box exceeds the height of the list box, the list box automatically increases the vertical scroll bar, and the user can select the list items by scrolling.

Construction method
public List ();
public List (int n);
Public List (int n,boolean b): parameter n Sets the number of visible rows, and B is true to set whether to allow multiple selections, or to prohibit multiple selections.

U Common Methods

U Other methods
public int getselectedindex ();
Public String GetSelectedItem ();
Public int[] Getselectedindexes (): Index value items for all list items.
Public string[] Getselecteditems ();

U Trigger Event

Μ triggers a itemevent event when you click a list item in a list box with the mouse;

Μ When you double-click a list item with the mouse, the ActionEvent event is triggered.

4. Choose two different ways to use components:

Μ submits a number of selection components to the form, and then submits and processes them as a whole.

Μ responds and processes immediately when the selection state of a selected component changes.

5. Rolling Assembly
The scrolling components in AWT include both scrollbar (scroll bars) and scrolling panels (scrollpane).

Μ scroll bar scrollbar: The ability to quickly select a value within a specified range of values.

Μ Construction Method
Public Scrollbar (int orientation,int value,int visible,int minimum,int maximum)

parameter Orientation: scrollbar.horizontal represents a horizontal scroll bar; Scrollbar.vertical represents a vertical scroll bar. Value: The initial value of the slider in the scroll bar.  Visible: Slider size. Minimum: Slider minimum value. Maximum: Slider maximum value.

U Common Methods
public void SetValue (int value);
public int getValue ();
public void setblockincrement (int value): Specifies the block increment for the scroll bar.
public void setunitincrement (int value): Specifies the unit increment for the scroll bar.

U Trigger Event
When the user changes the position of the slider in the scroll bar by mouse action (such as dragging the slider position), the AdjustmentEvent type event is triggered and the corresponding listener interface is: Adjustmentlistener, event handling method:
public void adjustmentvaluechanged (AdjustmentEvent e);

Scroll bar registers or deletes the event listener method:
public void Addadjustmentlistener (Adjustmentlistener l);
public void Removeadjustmentlistener (Adjustmentlistener

scrolling Panel:

Java.awt.ScrollPane This class inherits the container directly, but differs from other components (such as Frame, Panel), The ScrollPane component contains only a single component and supports a horizontal or vertical scrolling operation on the contained components (you can implement a scroll panel to display/scroll through multiple components simultaneously in a container nesting). There is no default layout manager (whose default layout manager is null) and the SetLayout () method is not allowed to set its layout.

you can interpret ScrollPane as a special panel with horizontal and vertical scroll bars.

6. Menu Components

menu components include menubar (menu bar), menu (menu), MenuItem (menu item), Checkboxmenuitem (menu item with check box), and PopupMenu (pop-up menu).

The principle of its use is:

Create a menu bar object and add it to the specified menu container (that is, a container that can hold menubar), and then add a menu component to the menu bar, which can be nested with the same components to implement multilevel menus, and only the last level uses the menu item. The use of MenuItem is the same as the button, where the ActionListener listener is often registered to trigger the real processing logic.

                               

Μ menu Bar menubar
MenuBar mb=new MenuBar ();
Frame.setmenubar (MB);

Μ Menu
Menu mnfile=new Menu ("file");
Menu Mnedit=new menu ();
Mb.add (Mnfile);
Mb.add (mnedit);

Μ menu item MenuItem
public MenuItem ();
Public MenuItem (String label);
Public MenuItem (String label,menushortcut s);

parameter S is used to set the shortcut key for the menu item, which is the object of the Menushortcut class, the constructor of which is Menushortcut (Intkey), and the parameter key takes a static constant in the KeyEvent class

MenuItem miopen=new MenuItem ("open");
Menushortcut msc=new menushortcut (keyevent.vk_e);

mnfile.addseparator (): Separator bar

event triggered by menu item: ActionEvent

7. Level two menu and check menu items

Μ level Two menu: nested menus in menus

Μ Check menu item (checkboxmenuitem)

Note: The event that is raised with the mouse click of the Check menu item is ItemEvent, so the listener interface to be implemented specifically is itemlistener.

8. Pop-up menu (PopupMenu)

Μ Construction Method
public PopupMenu ();
For example: PopupMenu popm=new popupmenu ();
MenuItem miopen=new MenuItem ("open");
MenuItem misave=new MenuItem ("Save");
Popm.add (Miopen);
Popm.add (misave);

Μ Because the pop-up menu is not visible by default, it is usually attached to a component or container, and then additional mouse monitoring is added to the component or container. For example: Txt.add (POPM);

Μ Display pop-up menu
public void Show (Component origin,int x,int y);

Μ the method in the Mouse event class Ispopuptrigger () is used to return the event that the mouse event is triggered by the component's pop-up menu.

triggered when the right mouse button is loosened in the Windows system. The Mac OS system is triggered when the mouse is pressed, in order to simplify programming, the MouseEvent class provides a unified method Ispopuptrigger () to detect whether this current mouse event is the platform's pop-up menu trigger event, and returns True if it is.

9. dialog box

Μ dialog Box Component dialog: The interface effect is similar to a frame, which is a top-level window with a border and a maximized, minimized, Close button, which can also add other components or containers, except that the dialog component is primarily used to receive some form of simple input from the user, and to "confirm" Action or the role of "warning/hint".

  

10. Attention should be paid to:

The default Layout manager for the dialog component is the BorderLayout type, and the default initialization is invisible, and the setvisible (true) method is required to display it

Although the dialog component is a top-level window, it must rely on one other window and not exist alone, and the dependent window is called the owner of the dialog box, usually a frame or other dialog.

when its owner window is minimized, dialog is also automatically hidden to be invisible to the user, and when the owner window is restored, the dialog becomes visible again .

11. Create a dialog class by creating a subclass of dialog, and then an instance of the class, an object of that class, is a dialog box.

Construction method
Public Dialod (Frame f,string s): Constructs an initially invisible dialog box with a caption, S is the title of the dialog box, and F is the owner of the dialog box.
Public Dialod (Frame f,string S,boolean b): B Determines whether the dialog box is modal or modeless.

Μ Common methods
Public String getTitle ();
public void Settitle ();
public void Setmodal (Boolean model): Setting mode
public void setSize (int width,int height);
public void setvisible (Boolean B);

12. The dialog box is divided into modeless dialogs (Modelessdialog) and modal dialog boxes (Modal Dialog):

- A modal dialog box blocks the contents of all other forms entered into the application in which they reside, such as the file dialog box in MS Word.

-The modeless dialog box does not display or affect other forms of the application, and is typically used to provide unnecessary accessibility features, such as Word's Find/Replace dialog box.

13. File dialog Box FileDialog

The file dialog box Java.awt.FileDialog, inherits the dialog class, but belongs to a modal dialog box that specifies the path and file name of the file when you open and save the file. The default is open, or you can display in the constructor method how you specified how it works.

Μ Construction Method
Public FileDialog (Frame f,string S, int mode): mode specifies how the dialog box works: Open or Save, value: Filedialog.load, Filedialog.save

Μ Common methods
Public String getdirectory (): Returns the directory to which the selected files in the file dialog box belong.
Public String getFile (): Returns the selected file name in the file dialog box and returns null if the file does not exist.

14. Note that the file dialog box only provides a file operation interface, to really implement the operation of the file should use the file input and output stream (method LoadFile (), SaveFile () and SaveFileAs () are used in the file input/output stream operation).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.