Graphical buttons for the Java graphical interface

Source: Internet
Author: User

To visualize the button, simply create a imageicon object, Assign the graphics path to the ImageIcon object, and pass the object to the Button.

Here are the path settings for the graphics in Eclipse , including (under project path, non-project path, relative path, absolute path), relative to the path, where the relative path is relative to the project folder in Eclipse. The absolute path is the exact path where the graph is unknown. Take picture1.jpg(under the H:/java/workspace/study/src/picture Path) as an example:

1. If the picture folder is placed under the STUDY/SRC path (non-project path):

1.1 Absolute Path: H:/java/workspace/study/src/picture/picture1.jpg

1.2 Relative Path: Src/picture/picture1.jpg

2. If the picture folder is placed under the study path (project path):

2.1 Absolute Path Invariant: h:/java/workspace/study/picture/picture1.jpg

2.2 Relative Path: Picture/picture1.jpg

 packagetest;Importjavax.swing.*;Importjava.awt.*;Importjava.awt.event.*;Import StaticNet.mindview.util.swingconsole.*; public classPicturelabelextendsjframe{Private Staticicon[] pictures; PrivateJButton JB,JB1 =NewJButton ("Disable"); Private BooleanMad =false;  publicPicturelabel () {pictures=Newicon[]{NewImageIcon ("src/picture/picture1.jpg"), // relative path                 NewImageIcon ("h:/java/workspace/study/src/picture/picture2.jpg"), //absolute path NewImageIcon ("src/picture/picture3.jpg"),                NewImageIcon ("src/picture/picture4.jpg"),                NewImageIcon ("src/picture/picture5.jpg")        }; //no/relative path before pathJB=NewJButton ("JButton", pictures[3]); SetLayout (NewFlowLayout ()); Jb.addactionlistener (NewActionListener () {@Override//ensure that the method being labeled does overwrite the method of the base class, or the compilation will go wrong             public voidactionperformed (actionevent E) {if(mad) {jb.seticon (pictures[3]); Mad=false; }Else{jb.seticon (pictures[0]); Mad=true;                } jb.setverticalalignment (jbutton.top);            Jb.sethorizontalalignment (jbutton.left);                }        }); Jb.setrolloverenabled (true); //allow rollover icon Jb.setrollovericon (pictures[1]); Jb.setpressedicon (pictures[2]); Jb.setdisabledicon (pictures[4]); Jb.settooltiptext ("Yow"); Add (jb);
If setrolloverenabled is true, then when the mouse moves over the button, the contents of the Setrollovericon are used on the graphic of the button, that is, picture[1];
When the button is pressed, the contents of the Setpressedicon are used on the graphic of the button, i.e. picture[2]; when the button is disabled, the contents of the Setdisabledicon are applied to the button,
i.e. picture[4].
Jb1.addactionlistener (NewActionListener () { public voidactionperformed (actionevent E) {if(jb.isenabled ()) {jb.setenabled (false); Jb1.settext ("Enable"); }Else{jb.setenabled (true); Jb1.settext ("Disable"); } } }); Add (jb1); } public Static voidmain (string[] Args) {run (NewPicturelabel (), 500,200); }}

The Compile-available button has an animated Effect.

Note:1, The relative path does not add before/;

2, in eclipse, the path of the file introduced in the program is relative to the project folder;

Graphical buttons for the Java graphical interface

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.