The JToolBar toolbar is the same as a container for a component that can add buttons, fine-tune controllers, and so on to the toolbar. Each added component is assigned an index of an integer to determine the order in which the component is displayed. In addition, the component can be in any one border of the form, or it can be a separate form
In general, the toolbar is mainly represented by an icon, located under the menu bar, can also be a floating toolbar, the form is very flexible JToolBar constructor:
JToolBar (): Establishes a new JToolBar with the default horizontal orientation.
JToolBar (int orientation): establishes a specified jtoolbar.
JToolBar (String name): Establishes a jtoolbar with the specified name.
JToolBar (String name,int orientation): Establishes a jtoolbar that specifies the name and location.
Note: The specified caption is displayed when the toolbar is a floating toolbar, with static constants horizontal and vertical in the specified direction, respectively, for horizontal and vertical directions
To construct the JToolBar component:
The horizontal orientation is generally used when using JToolBar, so we construct the JToolBar by using the first construction method in the table above. If you need to change the direction and then use the SetOrientation () method in the JToolBar to change the settings, Or you can change the position of the JToolBar in a mouse-driven way.
Common methods of JToolBar class
Public JButton Add (action a): Adds a new button that assigns an action to the toolbar
public void AddSeparator (): Adds the default size delimiter to the end of the toolbar
Public Component getcomponentatindex (int i): Returns the component at the specified index location
public int Getcomponentindex (Component c): Returns the index of the specified component
public int getorientation (): Returns the current orientation of the toolbar
public boolean isfloatable (): Gets the Floatable property to determine if the toolbar can be dragged and returns true if it can, otherwise false
public boolean isrollover (): Gets the rollover state to determine whether the border of the button is drawn when the mouse passes over the toolbar button, or True if drawing is required, otherwise returns false
public void Setfloatable (Boolean B): Sets the Floatable property, this property must be set to TRUE if you want to move the toolbar
Package Ch10;import java.awt.borderlayout;import java.awt.event.*;import javax.swing.*;p ublic class ToolBarTest Extends JFrame implements actionlistener{JButton LeftButton = new JButton ("left Justified", New ImageIcon ("D:/1.png")); JButton Middlebutton = new JButton ("center", New ImageIcon ("D:/1.png")); JButton Rightbutton = new JButton ("center left", New ImageIcon ("D:/1.png"); Private jbutton[] Buttonarray = new Jbutton[]{leftbutton,middlebutton,rightbutton}; Private JToolBar toolbar = new JToolBar ("Easy Toolbar"); Private JLabel JL = new JLabel ("Click on the toolbar, select Alignment!) "); Public Toolbartest () {for (int i=0;i<buttonarray.length;i++) {toolbar.add (buttonarray[i]); Sets the tooltip information for the button and displays a message Buttonarray[i].settooltiptext (Buttonarray[i].gettext ()) when the mouse is placed over it; Buttonarray[i].addactionlistener (this); } toolbar.setfloatable (True); Set the toolbar to true to be a floating toolbar this.add (Toolbar,borderlayout.north); This.add (JL); This.settitle ("Toolbar Test window"); This.setvisible (TRUE); This.setbounds (200,200,300,200); This.setdefaultcloseoperation (Jframe.exit_on_close); } public void actionperformed (ActionEvent a) {if (A.getsource () ==buttonarray[0]) {jl.sethorizontalalignmen T (Jlabel.left); Jl.settext ("The alignment you choose is:" +buttonarray[0].gettext () + "!");} if (A.getsource () ==buttonarray[1]) {jl.sethorizontalalignment (jlabel.center); Jl.settext ("The alignment you choose is:" +buttonarray[1].gettext () + "!");} if (A.getsource () ==buttonarray[2]) {jl.sethorizontalalignment (jlabel.right); Jl.settext ("The alignment you choose is:" +buttonarray[2].gettext () + "!");}} public static void Main (String args[]) {new toolbartest (); }}
Toolbars (JToolBar)