Use cases for toolbars (JToolBar) and split-face (JSplitPane) components in Java swing _java

Source: Internet
Author: User

One: Toolbars (JToolBar)

code example:

Copy Code code as follows:

Import javax.swing.*;

Usage cases for toolbars
public class Jtoolbardemo2_jigloo extends Javax.swing.JFrame {

Private JToolBar Myjtoolbar;
Private JButton Jb_file;
Private JButton Jb_edit;
Private JButton Jb_tools;
Private JButton jb_help;

public static void Main (string[] args) {
Swingutilities.invokelater (New Runnable () {
public void Run () {
Jtoolbardemo2_jigloo Inst = new Jtoolbardemo2_jigloo ();
Inst.setlocationrelativeto (NULL);
Inst.setvisible (TRUE);
}
});
}

Public Jtoolbardemo2_jigloo () {
Super ();
Initgui ();
}

private void Initgui () {
try {
Setdefaultcloseoperation (Windowconstants.dispose_on_close);
Getcontentpane (). setlayout (NULL);
Getcontentpane (). SetBackground (New Java.awt.Color (255, 128, 255));
{
Myjtoolbar = new JToolBar ();
Getcontentpane (). Add (Myjtoolbar);
Myjtoolbar.setbounds (29, 12, 320, 38);
Myjtoolbar.setbackground (New Java.awt.Color (255, 255, 255));
{
Jb_file = new JButton ();
Myjtoolbar.add (Jb_file);
Jb_file.settext ("\u6587\u4ef6");
Jb_file.setpreferredsize (New Java.awt.Dimension (80, 34));
Jb_file.seticon (New ImageIcon ("images//addnode.jpg"));
Jb_file.setfont (New Java.awt.Font ("italics", 0, 14));
Jb_file.settooltiptext ("Click here to add one node at a time");
Jb_file.setbackground (New Java.awt.Color (255, 255, 128));
}
{
Jb_edit = new JButton ();
Myjtoolbar.add (Jb_edit);
Jb_edit.settext ("\u7f16\u8f91");
Jb_edit.settooltiptext ("Click here to add one node at a time");
Jb_edit.seticon (New ImageIcon ("images//addside.jpg"));
Jb_edit.setfont (New Java.awt.Font ("italics", 0, 14));
Jb_edit.setbackground (New Java.awt.Color (255, 255, 128));
Jb_edit.settooltiptext ("First click End node input, then add a side");
Jb_edit.setpreferredsize (New Java.awt.Dimension (78, 34));
}
{
Jb_tools = new JButton ();
Myjtoolbar.add (Jb_tools);
Jb_tools.settext ("\u89c6\u56fe");
Jb_tools.seticon (New ImageIcon ("images//drawfigure.jpg"));
Jb_tools.setfont (New Java.awt.Font ("italics", 0, 14));
Jb_tools.setbackground (New Java.awt.Color (255, 255, 128));
Jb_tools.settooltiptext ("First click on the end of the input, and then draw Graphics");
Jb_tools.setpreferredsize (New Java.awt.Dimension (94, 34));
}
{
Jb_help = new JButton ();
Myjtoolbar.add (JB_HELP);
Jb_help.settext ("\u5e2e\u52a9");
Jb_help.seticon (New ImageIcon ("images//shortestpath.jpg"));
Jb_help.setfont (New Java.awt.Font ("italics", 0, 14));
Jb_help.setbackground (New Java.awt.Color (255, 255, 128));
Jb_help.settooltiptext ("First click the Transform button, select the starting point to find its shortest path");
Jb_help.setpreferredsize (New Java.awt.Dimension (95, 34));
}
}
Pack ();
SetSize (400, 300);
catch (Exception e) {
E.printstacktrace ();
}
}
}

4. Run Screenshots:

Two: Split Pane (split-face version):

1. Basic Description:

Split Pane (split-face version) can display two components simultaneously in two display areas, and if you want to display components in multiple display areas at once, you must use multiple split Pane at the same time. JSplitPane provides two constants that let you set whether you want to split horizontally or vertically. The two constants are: Horizontal_spit,vertical_split. In addition to these two important constants, JSplitPane also provides many class constants that allow you to use

2. Common methods:

(1). JSplitPane Construction Method:

JSplitPane (): Creates a new JSplitPane that contains two default buttons and is arranged horizontally. But there is no continuous layout function.

For example: JSplitPane jSplitPane1 = new JSplitPane ();

Screenshots:

JSplitPane (int neworientation): establishes a specified horizontal or vertical direction for cutting jsplitpane, but no continuous layout function.

Jsplitpnae (int Neworientation,boolean newcontinuouslayout): Creates a jsplitpane with a specified horizontal or vertical cut, and specifies whether to have continuous layout function.

JSplitPane (int neworientation,component newleftcomponent,component newrightcomponent): Creates a JSplitPane with a specified horizontal or vertical direction for cutting, and specifies the component to display for the display area, but does not have continuous layout functionality.

For example: JSplitPane = new JSplitPane (Jsplitpane.horizontal_split, Jscrollpane,jlabel);

Screenshots:

JSplitPane (int neworientation,boolean newcontinuouslayout,component newleftcomponent,component newRightComponent): Creates a jsplitpane that specifies horizontal or vertical cutting, specifies the component to display for the display area, and sets whether to continuous the layout function.

The above continuous layout means that when you drag the cutting surface divider, the component within the window will dynamically resize as the divider line drags.

3. code example:

Copy Code code as follows:

Package com. jsplitpanedemo.com;

Import java.awt.*;
Import javax.swing.*;

public class JSplitPaneDemo3 {
JFrame F;
Container ContentPane;
Public JSplitPaneDemo3 () {
f = new JFrame ("JSplitPaneDemo2");
ContentPane = F.getcontentpane ();

JLabel Label1 = new JLabel ("Label 1", jlabel.center);
Label1.setbackground (Color.green);
The purpose of the Setopaque (Ture) method is to make the component opaque so that the colors we set on the JLabel are displayed.
Label1.setopaque (TRUE);

JLabel Label2 = new JLabel ("Label 2", jlabel.center);
Label2.setbackground (Color.pink);
Label2.setopaque (TRUE);

JLabel label3 = new JLabel ("Label 3", jlabel.center);
Label3.setbackground (Color.yellow);
Label3.setopaque (TRUE);

/*
* Add Label1,label2 to SplitPane1 and set this splitPane1 as horizontal split with continuous layout
function
*/

JSplitPane splitPane1 = new JSplitPane (Jsplitpane.horizontal_split,
False, Label1, Label2);
/*
* Set SPLITPANE1 divider position, 0.3 is relative to the size of the splitPane1, so the range of this value in 0.0~1.0
In If you use an integer value to set the divider position of the Splitpane, as shown in line 34th, the value defined is pixel as a calculated unit
*/
Splitpane1.setdividerlocation (0.3);
Splitpane1.setresizeweight (0.3);

Sets whether JSplitPane can be expanded or closed (as in File Explorer), set to True to turn on this feature.
Splitpane1.setonetouchexpandable (TRUE);
Splitpane1.setdividersize (10);//Set the width of the separator line to pixel as the unit of calculation.

JSplitPane splitPane2 = new JSplitPane (Jsplitpane.vertical_split,
False, SplitPane1, LABEL3);
Splitpane2.setdividerlocation (35);
Sets whether JSplitPane can be expanded or closed (as in File Explorer), set to True to turn on this feature.
Splitpane2.setonetouchexpandable (FALSE);
Splitpane2.setdividersize (5);

Contentpane.add (SplitPane2);
F.setsize (250, 200);
F.setlocation (300, 200);
F.setvisible (TRUE);
F.setdefaultcloseoperation (Jframe.exit_on_close);
}

public static void Main (string[] args) {
New JSplitPaneDemo3 ();
}
}

4. Run Screenshots:

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.