Statement before reading: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx
6-1-1: Use of jtabbedpane:
Class hierarchy structure:
Java. Lang. Object
-- Java. AWT. Component
-- Java. AWT. Container
-- Javax. Swing. jcomponent
-- Javax. Swing. jtabbedpane
Jtabbedpane constructor:
Jtabbedpane (): creates an empty jtabbedpane object.
Jtabbedpane (INT tabplacement): creates an empty jtabbedpane object and specifies the placement location, such as top, bottom, left, right.
Jtabbedpane event processing mode:
Jtabbedpane mainly processes changeevent events. A changeevent event is generated every time a tag is selected in jtabbedpane. Therefore, you need to process the selected tag
The corresponding operation must implement the interface changelistener. In addition, each tag on jtabbedpane has an index value (INDEX). Generally, if no index is set
The index is set from left to right in order of 0, 1, 2 ....., therefore, in the following example, the index value of picture is 0 and that of label 2 is 1.
Each tag has corresponding operations.
Example: jtabbedpane2
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;
/* Since changeevent is a swing event rather than an AWT event, the import swing event class is used to process
* Changeevent event.
*/
Import javax. Swing. event .*;
Public class jtabbedpane2 implements actionlistener, changelistener {
Int Index = 0;
Int newnumber = 1;
Jtabbedpane tabbedpane = NULL;
Public jtabbedpane2 (){
Jframe F = new jframe ("jtabbedpane2 ");
Container contentpane = f. getcontentpane ();
Jlabel label1 = new jlabel (New imageicon (". // icons // flower.jpg "));
Jpanel Panel1 = new jpanel ();
Panel1.add (label1 );
Jlabel label2 = new jlabel ("label 2", jlabel. center );
Label2.setbackground (color. Pink );
Label2.setopaque (true );
Jpanel panel2 = new jpanel ();
Panel2.add (label2 );
Jlabel label3 = new jlabel ("label 3", jlabel. center );
Label3.setbackground (color. Yellow );
Label3.setopaque (true );
Jpanel panel3 = new jpanel ();
Panel3.add (label3 );
Tabbedpane = new jtabbedpane ();
Tabbedpane. settabplacement (jtabbedpane. Top); // you can specify the tag placement position.
/* Since changeevent is a swing event rather than an AWT event, the import swing event class is used to process
* Changeevent event.
*/
Tabbedpane. addchangelistener (this );
Tabbedpane. addtab ("picture", null, Panel1, "pattern ");
Tabbedpane. addtab ("label 2", panel2 );
Tabbedpane. addtab ("label 3", null, panel3, "label ");
Tabbedpane. setenabledat (2, false); // set label 3 to disable
Jbutton B = new jbutton ("Add Tag ");
B. addactionlistener (this );
Contentpane. Add (B, borderlayout. South );
Contentpane. Add (tabbedpane, borderlayout. center );
F. Pack ();
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
}
/* Implement the changelistener method to enable the label on the right to display the Enable status if the label on the left is selected. The getselectedindex () method returns
* Currently, you can click the index value of a tag. The gettabcount () method returns several jtabbedpane tags, while the setenabledat () method enables a tag.
* The status is enable or disable (true is enable, false is disable ).
*/
Public void statechanged (changeevent e ){
If (index! = Tabbedpane. getselectedindex ()){
If (index <tabbedpane. gettabcount ()-1)
Tabbedpane. setenabledat (index + 1, true );
}
Index = tabbedpane. getselectedindex ();
}
/* Implement the actionlistener interface. When you press the "Add Tag" button
* A disable status label is added to tabbedpane.
*/
Public void actionreceivmed (actionevent e ){
Jpanel pane1 = new jpanel ();
Jlabel label4 = new jlabel ("new label" + newnumber, jlabel. center );
Label4.setopaque (true );
Pane1.add (label4 );
Tabbedpane. addtab ("new" + newnumber, pane1 );
Tabbedpane. setenabledat (newnumber + 2, false );
Newnumber ++;
Tabbedpane. Validate ();
}
Public static void main (string [] ARGs ){
New jtabbedpane2 ();
}
}
6-1-1: Use of jscrollpane:
Class hierarchy structure:
Java. Lang. Object
-- Java. AWT. Component
-- Java. AWT. Container
-- Javax. Swing. jcomponent
-- Javax. Swing. jscrollpane
Jscrollpane constructor:
Jscrollpane (): creates an empty jscrollpane object.
Jscrollpane (Component view): Creates a new jscrollpane object. When the component content is greater than the display area, a scroll axis is automatically generated.
Jscrollpane (Component view, int vsbpolicy, int hsbpllicy): Creates a new jscrollpane object that contains the display component and sets the scroll axis.
Occurrence time.
Jscrollpane (INT vsbpolicy, int hsbpolicy): Creates a new jscrollpane object, which does not contain display components, but sets the time when the scroll bar appears.
Jscrollpane or the following parameters are used to set the time when the scroll axis appears. These parameters are defined in the scrollpaneconstants interface, while
The jscrollpane class implements this interface, so these parameters can be used:
Horizontal_scrollbar_alaways: displays the horizontal scroll axis.
Horizontal_scrollbar_as_needed: when the horizontal area of the component content is greater than the display area, the horizontal scroll axis appears.
Horizontal_scrollbar_never: the horizontal scroll axis is not displayed.
Vertical_scrollbar_always: displays the vertical scroll axis.
Vertical_scrollbar_as_needed: When the vertical area of the component content is greater than the display area, the vertical scroll axis appears.
Vertical_scrollbar_never: the vertical scroll axis is not displayed.
Example: jscrollpane1.java
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;
Public class jscrollpane1 implements actionlistener {
Jscrollpane scrollpane;
Public jscrollpane1 (){
Jframe F = new jframe ("jscrollpane1 ");
Container contentpane = f. getcontentpane ();
Jlabel label1 = new jlabel (New imageicon (". // icons // flower.jpg "));
Jpanel Panel1 = new jpanel ();
Panel1.add (label1 );
/* Add a jscrollpane object and put Panel1 into jscrollpane. If the size of the pane1 component is in the window size
* If the window size is greater than the window size, scrollbar is displayed automatically.
*/
Scrollpane = new jscrollpane (Panel1 );
Jpanel panel2 = new jpanel (New gridlayout (3, 1 ));
Jbutton B = new jbutton ("display horizontal scroll axis ");
B. addactionlistener (this );
Panel2.add (B );
B = new jbutton ("do not display the horizontal scroll axis ");
B. addactionlistener (this );
Panel2.add (B );
B = new jbutton ("Displaying horizontal scroll axes in due time ");
B. addactionlistener (this );
Panel2.add (B );
Contentpane. Add (panel2, borderlayout. West );
Contentpane. Add (scrollpane, borderlayout. center );
F. setsize (new dimension (350,220 ));
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
}
/* Implement the actionlistener interface. When you press the "show horizontal scroll axis" button, set the horizontal scroll axis parameter
* Jscrollpane. horizontal_scrollbar_always. If you press the "Do Not Display horizontal scroll axis" button, Set
* The horizontal scroll axis parameter is jscrollpane. horizontal_scrollbar_never. If you press "show horizontal scroll axis in due time"
*, Set the horizontal scroll axis parameter to horizontal_scrollbar_as_needed.
*/
Public void actionreceivmed (actionevent e ){
If (E. getactioncommand (). Equals ("show horizontal scroll axis "))
Scrollpane. sethorizontalscrollbarpolicy (jscrollpane. horizontal_scrollbar_always );
If (E. getactioncommand (). Equals ("Do Not Display horizontal scroll axis "))
Scrollpane. sethorizontalscrollbarpolicy (jscrollpane. horizontal_scrollbar_never );
If (E. getactioncommand (). Equals (" "))
Scrollpane. sethorizontalscrollbarpolicy (jscrollpane. horizontal_scrollbar_as_needed );
Scrollpane. revalidate (); // re-display the jscrollpane shape.
}
Public static void main (string [] ARGs ){
New jscrollpane1 ();
}
}
Next, let's take a look at the common features of jscrollpane. In addition to allowing you to scroll through the axis, jscrollpane can also set the header name and corner
(Corner) pattern and scrollpane outer frame. Let's modify the example above. Make jscrollpane more variable: jscrollpane2.java
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;
Import javax. Swing. Border .*;
Public class jscrollpane2 implements actionlistener {
Jscrollpane scrollpane;
Public jscrollpane2 (){
Jframe F = new jframe ("jscrollpanedemo ");
Container contentpane = f. getcontentpane ();
Jlabel label1 = new jlabel (New imageicon (". // icons // flower.jpg "));
Jpanel Panel1 = new jpanel ();
Panel1.add (label1 );
Scrollpane = new jscrollpane ();
/* Set the display component in the window to Panel1.
*/
Scrollpane. setviewportview (Panel1 );
/* Set the horizontal and vertical Headers
*/
Scrollpane. setcolumnheaderview (New jlabel ("Horizontal Header "));
Scrollpane. setrowheaderview (New jlabel ("vertical header "));
/* Set the border of scrollpane to sag the three-dimensional border. The border (Border) Section will be introduced later.
*/
Scrollpane. setviewportborder (borderfactory. createbevelborder (bevelborder. lowered ));
/* Set the corner pattern of scrollpane. Because jscrollpane is a rectangle shape, there are four positions to place the corner (Corner)
* Component. The four locations are top left, bottom left, top right, and bottom right. The corresponding parameters are as follows:
* Jscrollpane. upper_left_corner
* Jscrollpane. lower_left_corner
* Jscrollpane. upper_right_corner
* Jscrollpane. lower_right_corner
*/
Scrollpane. setcorner (jscrollpane. upper_left_corner, new jlabel (New imageicon (". // icons/glass.jpg ")));
Scrollpane. setcorner (jscrollpane. upper_right_corner, new jlabel (New imageicon (". // icons/glass.jpg ")));
Jpanel panel2 = new jpanel (New gridlayout (3, 1 ));
Jbutton B = new jbutton ("display horizontal scroll axis ");
B. addactionlistener (this );
Panel2.add (B );
B = new jbutton ("do not display the horizontal scroll axis ");
B. addactionlistener (this );
Panel2.add (B );
B = new jbutton ("Displaying horizontal scroll axes in due time ");
B. addactionlistener (this );
Panel2.add (B );
Contentpane. Add (panel2, borderlayout. West );
Contentpane. Add (scrollpane, borderlayout. center );
F. setsize (new dimension (350,220 ));
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
}
);
}
Public void actionreceivmed (actionevent e ){
If (E. getactioncommand (). Equals ("show horizontal scroll axis "))
Scrollpane. sethorizontalscrollbarpolicy (jscrollpane. horizontal_scrollbar_always );
If (E. getactioncommand (). Equals ("Do Not Display horizontal scroll axis "))
Scrollpane. sethorizontalscrollbarpolicy (jscrollpane. horizontal_scrollbar_never );
If (E. getactioncommand (). Equals (" "))
Scrollpane. sethorizontalscrollbarpolicy (jscrollpane. horizontal_scrollbar_as_needed );
Scrollpane. revalidate (); // re-display the jscrollpane shape.
}
Public static void main (string [] ARGs ){
New jscrollpane2 ();
}
}
6-1-2: Use of jscrollbar:
Class hierarchy structure:
Java. Lang. Object
-- Java. AWT. Component
-- Java. AWT. Container
-- Javax. Swing. jcomponent
-- Javax. Swing. jscrollbar
In the previous section, we can see that jscrollpane uses the scrollbar function to allow it to use the scroll bar to scroll the window. At first glance, we will not directly use
Jscrollbar method, because jscrollpane can help us well, but if we want to make more detailed settings on the scroll axis, such as how many times a scroll occurs during dragging
You must understand the functions provided by jscrollbar. The rolling of jscrollbar in the processing window is not as easy as that of jscrollpane.
Jscrollpane is much simpler, So we usually take some functions provided by jscrollbar to supplement our needs for jscrollpane, instead of directly
Take the jscrollbar for rolling. The following constructor is used:
Jscrollbar constructor:
Jscrollbar (): Creates a vertical scroll axis. The default values are minimum = 0, maximum = 100, value = 0, extent = 10.
Jscrollbar (INT orientation): Creates a scroll axis in the specified direction. The default values are:
Minimum = 0, maximum = 100, value = 0, extent = 10.
Jscrollbar (INT orientation, int value, int extent, int min, int max): Create a scroll axis in the specified direction, and set value, extent,
The mimimum and maximum parameters.
The meaning of the four jscrollbar parameters is as follows:
Value: the starting position of the jscrollbar. If it is set to 0, it is at the top of the scroll bar.
Extent: extended area, which limits the scroll range of the scroll axis. For example, if the minimum value is set to 0, the maximan value is set to 100, and the extent value is set to 20, the scroll axis can scroll
The area size is 100-20-0 = 80 scales, and the rolling range is from 0 ~ 80. If the minimum value is set to 20, the maximan value is set to 100, and the extent value is set to 30, the scroll axis can be rolled.
The moving area is 100-30-20 = 50 scales, and the rolling range is from 20 ~ 70. Therefore, the larger the extension area, the smaller the rolling range.
Minimum: sets the minimum scale value.
Maximum: sets the maximum scale value.
The most commonly used jscrollbar event is the adjustmentevent event, which is triggered when you drag the scroll axis. Therefore, to handle such events, you must implement
Adjustmentevent interface. This interface defines an adjustmentvaluechanged () method to obtain information about the scroll axis.
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;
Public class jscrollbar1 implements adjustmentlistener {
Jscrollbar scrollbar1;
Jscrollbar scrollbar2;
Jpanel Panel1;
Jlabel label2 = new jlabel ("scale:", jlabel. center );
Public jscrollbar1 (){
Jframe F = new jframe ("jscrollbardemo ");
Container contentpane = f. getcontentpane ();
Jlabel label1 = new jlabel (New imageicon (". // icons // flower.jpg "));
Panel1 = new jpanel ();
Panel1.add (label1 );
/* Generate a vertical scroll axis. By default, the scroll axis is located at 10 scales, extent is set to 10, and minimum is set to 0,
* The maximan value is 100. Therefore, when the scroll axis starts at the scale 10 position, the area size that can be rolled is 100-10-0 = 90.
* Scale, with a rolling range of 0 ~ 90.
*/
Scrollbar1 = new jscrollbar (jscrollbar. Vertical, 0,100 );
Scrollbar1.setunitincrement (1); // you can specify the scale change of the scroll axis when dragging the scroll axis.
Scrollbar1.setblockincrement (10); // you can specify the block size when the mouse clicks on the scroll axis column.
Scrollbar1.addadjustmentlistener (this );
Scrollbar2 = new jscrollbar (); // create an empty jscrollbar
Scrollbar2.setorientation (jscrollbar. Horizontal); // you can specify horizontal orientation for the scroll bar.
Scrollbar2.setvalue (0); // set the default scroll axis position to 0.
Scrollbar2.setvisibleamount (20); // set the extent value to 20.
Scrollbar2.setminimum (10); // set the minmum value to 10.
Scrollbar2.setmaximum (60); // set the maximan value to 60. Because the minmum value is set to 10, the area size that can be rolled is 60-20-10 = 30.
// Scale, with a rolling range of 10 ~ 40.
Scrollbar2.setblockincrement (5); // when you press the mouse on the scroll axis column, the block size that the scroll axis jumps at a time is 5 scales
Scrollbar2.addadjustmentlistener (this );
Contentpane. Add (Panel1, borderlayout. center );
Contentpane. Add (scrollbar1, borderlayout. East );
Contentpane. Add (scrollbar2, borderlayout. South );
Contentpane. Add (label2, borderlayout. North );
F. setsize (new dimension (200,200 ));
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
}
);
}
// Implement the adjustmentvaluechanged method. When you change the axis position, the current scroll axis scale is written on labe2.
Public void adjustmentvaluechanged (adjustmentevent e ){
If (jscrollbar) E. getsource () = scrollbar1)
Label2.settext ("vertical scale" + E. getvalue (); // the value obtained by E. getvalue () is the same as that obtained by scrollbar1.getvalue.
If (jscrollbar) E. getsource () = scrollbar2)
Label2.settext ("horizontal scale" + E. getvalue ());
}
Public static void main (string [] ARGs ){
New jscrollbar1 ();
}
}