Java Simple Electronic album

Source: Internet
Author: User

Knowledge points involved: GUI interface, event listening, learning Java knowledge through a simple instance

Package mytest; import Java. AWT. *; import Java. AWT. event. *; import javax. swing. *; public class myimg extends jframe {testpanel TP = NULL; jpanel JP = NULL; jbutton jb1, jb2, jb3, jb4; public static void main (string [] ARGs) {myimg IMG = new myimg ();} public myimg () {TP = new testpanel (6, 1); // set the clip size, by default, the first image JP = new jpanel () is displayed. // jpanel default streaming layout jb1 = new jbutton ("Homepage"); JP. add (jb1); jb2 = new jbutton ("Previous Page"); JP. add (jb2); jb3 = new jbutton ("next page"); JP. add (jb3); jb4 = new jbutton ("last page"); JP. add (jb4); // register the listener jb1.addactionlistener (TP); jb1.setactioncommand ("first"); jb2.addactionlistener (TP); jb2.setactioncommand ("Prev"); jb3.addactionlistener (TP ); jb3.setactioncommand ("Next"); jb4.addactionlistener (TP); jb4.setactioncommand ("last"); this. add (TP); // jframe default border layout this. add (JP, borderlayout. south); this. setsize (400,300); this. setdefaclocloseoperation (jframe. exit_on_close); this. setvisible (true) ;}} class testpanel extends jpanel implements actionlistener {int I, size; string imgpath, srcpath = "/mytest/"; // photo path string imgsuffix = ". jpg "; // image suffix public testpanel (INT size, int I) {This. size = size; this. I = I; imgpath = srcpath + I + imgsuffix;} public void paint (Graphics g) {super. paint (g); image im = toolkit. getdefatooltoolkit (). getimage (panel. class. getresource (imgpath); G. drawimage (IM, 90, 45,200,150, this) ;}@ overridepublic void actionreceivmed (actionevent e) {If (E. getactioncommand (). equals ("first") {I = 1; this. imgpath = srcpath + I + imgsuffix;} else if (E. getactioncommand (). equals ("Prev") {if (I> 1) {// determine whether to cross the border -- I; this. imgpath = srcpath + I + imgsuffix;} else if (E. getactioncommand (). equals ("Next") {if (I <size) {// determine whether to cross the border ++ I; this. imgpath = srcpath + I + imgsuffix;} else if (E. getactioncommand (). equals ("last") {I = size; this. imgpath = srcpath + I + imgsuffix;} // system. out. println (imgpath); this. repaint (); // testpanel repainting }}

 

 

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.