Java Desktop Application Development Study Note 2

Source: Internet
Author: User

Learning Java Desktop Application Development _ main form

Generally, all applications are login forms. After successful login, the main interface is displayed. First, check the sketch of the main interface:

It can be seen that the main interface can be roughly divided into four parts, the top is the menu bar and the Picture toolbar, the left is the tree navigation, the status bar below.

Menu Bar: jmenubar, jmenu, and jmenuitem)

Tool bar: toolbar, jbutton (Button + image)

Tree navigation: jtree

Status Bar: toolbar, jlable

 

Code for implementing the interface:

Menu bar:

 

Code

 1 Final jmenubar menubar = new jmenubar ();
2 menubar. setname ("menubar ");
3 setjmenubar (menubar );
4
5 Final jmenu jmenufile = new jmenu ();
6 jmenufile. settext ("file (f )");
7 jmenufile. setname ("menufile ");
8 menubar. Add (jmenufile );
9
10 final jmenuitem newproject = new jmenuitem ();
11 newproject. setname ("newproject ");
12 newproject. settext ("new project ");
13 jmenufile. Add (newproject );
14
15 final jmenu jmenutool = new jmenu ();
16 jmenutool. settext ("tool (t )");
17 jmenutool. setname ("jmenutool ");
18 menubar. Add (jmenutool );

 

 

Toolbar:

 

Code

 Final jtoolbar toolbar = new jtoolbar ();
Toolbar. setname ("toolbar ");
Getcontentpane (). Add (toolbar, borderlayout. North );

Final jbutton jbtnnew = new jbutton ();
Jbtnnew. addactionlistener (New actionlistener (){
Public void actionreceivmed (final actionevent arg0 ){
// Click the new button to trigger the event
}
});
Jbtnnew. setname ("jbtnnew ");
Jbtnnew. seticon (swingresourcemanager. getIcon (main. Class,
"Image/filenew.png "));
Jbtnnew. settext ("new ");
Toolbar. Add (jbtnnew );

Toolbar. addseparator ();

Final jbutton jbtnsave = new jbutton ();
Jbtnsave. addactionlistener (New actionlistener (){
Public void actionreceivmed (final actionevent arg0 ){
}
});
Jbtnsave. setname ("jbtnsave ");
Jbtnsave. seticon (swingresourcemanager. getIcon (main. class, "image/filesave.png "));
Jbtnsave. settext ("save ");
Toolbar. Add (jbtnsave );

 

 

Tree menu:

 

 // Root Node
Defaultmutabletreenode root = new defaultmutabletreenode ("task information ");
// Create a tree
Jtree tree = new jtree (Root );

 

 

Status Bar:

 

Code

 1 Final jtoolbar stautsbar = new jtoolbar ();
2 stautsbar. setname ("statusbar ");
3 getcontentpane (). Add (stautsbar, borderlayout. South );
4
5 lblwelcome = new jlabel ();
6 lblwelcome. settext ("welcome ");
7 stautsbar. Add (lblwelcome );
8
9 stautsbar. addseparator ();
10
11 lblsupport = new jlabel ();
12 lblsupport. settext ("technical support :");
13 stautsbar. Add (lblsupport );

 

 

The above code can be used to build a main interface framework, and friends may need to expand it themselves ..

 

Note:

1. Pay attention to the layout of the interface. You can add a jpanel container to participate in the layout.

 

2. Currently, the number of Sketch controls is relatively small. If there are more controls and more events are processed, we need to learn how to use splitting to prevent excessive code on the main interface, the combination method.

For example, the tree control or the code used to process events is a class (test1)

Panel 1 controls or code for event processing a class (Test2)

......

 

The combination in the main interface has the same effect, but the code on the main interface will be greatly reduced ..

 

3. After creating a new jframe, you must write the code for releasing resources for the interface. If you do not write dispose (); A Java W process will be added to the process once it runs (you can test it yourself)

Public void windowclosing (final login wevent e ){
Dispose ();
}

 

Related Article

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.