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 }}