Java's swing, event processing, and file opening have their own advantages over.
The image browsing application opens an image, controls the upper and lower pictures in the current folder by clicking the button and keyboard, and scales all the pictures to fit the screen display size.
// It seems that there is a memory overflow. When many images are opened, the VM memory is insufficient, but the new lable is created every time the image is displayed.
Import Java. AWT. borderlayout; import Java. AWT. dimension; import Java. AWT. frame; import Java. AWT. gridlayout; import Java. AWT. image; import Java. AWT. toolkit; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. event. keyadapter; import Java. AWT. event. keyevent; import Java. AWT. event. keylistener; import Java. io. file; import Java. io. filenamefilter; import Java. util. jar. attributes. name; import javax. swing. imageicon; import javax. swing. jbutton; import javax. swing. jfilechooser; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. jpanel; public class xiuxiu extends jframe {jpanel panel, panel2; jlabel labelphoto; jbutton buttonlast, buttonnext, buttonopen, buttonexit; imageicon icon = NULL; // old ink, string path; // the path of the folder where the image is located. String [] filenames; // The Int I = 0, Count = 0; Public xiuxiu () {super ("yiyu Qingfeng xiuxiu"); buttonlast = new jbutton ("previous"); buttonnext = new jbutton ("Next "); buttonopen = new jbutton ("open"); buttonexit = new jbutton ("exit"); Panel = new jpanel (New gridlayout (); // grid layout is used in the panel, divided into four button panels. add (buttonopen); panel. add (buttonlast); panel. add (buttonnext); panel. add (buttonexit); buttonopen. addkeylistener (New xiukeylistener (); buttonopen. addactionlistener (New openlistener (); buttonexit. addactionlistener (New exitlistener (); buttonnext. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {if (I <count-1) I ++; else if (I = count-1) I = 0; // The First Time icon = new imageicon (path + "\" + filenames [I]); xiuxiu. this. settitle (filenames [I]); showphoto (icon) ;}}); buttonlast. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent arg0) {if (I> 0) I --; else if (I = 0) I = count-1; // The First Time icon = new imageicon (path + "\" + filenames [I]); xiuxiu. this. settitle (filenames [I]); showphoto (icon) ;}}); this. add (panel, borderlayout. south); // place the button panel in the border layout at the bottom of the window this. seticonimage (Toolkit. getdefatooltoolkit (). createimage ("logo.png"); // change the icon dimension screensize = toolkit. getdefatooltoolkit (). getscreensize (); this. setsize (screensize. width, screensize. height); // This. setresizable (false); // The setting window cannot be changed. // This. setextendedstate (this. maximized_both); // maximize the window size. setdefaclocloseoperation (jframe. exit_on_close);} public class openlistener implements actionlistener {public void actionreceivmed (actionevent e) {openfile () ;}} public class exitlistener implements actionlistener {public void actionreceivmed (actionevent e) {system. exit (1) ;}} public class xiukeylistener extends keyadapter {public void keypressed (keyevent e) {int key = E. getkeycode (); If (Key = keyevent. vk_left) {if (I> 0) I --; else if (I = 0) I = count-1; icon = new imageicon (path + "\" + filenames [I]); xiuxiu. this. settitle (filenames [I]); showphoto (icon);} else if (Key = keyevent. vk_right) {if (I <count-1) I ++; else if (I = count-1) I = 0; icon = new imageicon (path + "\" + filenames [I]); xiuxiu. this. settitle (filenames [I]); showphoto (icon) ;}} public void openfile () {jfilechooser fchooser = new jfilechooser (); int rval = fchooser. showopendialog (this); If (rval = jfilechooser. approve_option) // confirm to open {string filename = fchooser. getselectedfile (). getname (); this. settitle (filename); Path = fchooser. getcurrentdirectory (). tostring (); // file = new file (PATH); If (file. exists () & file. isdirectory () // The path exists and is the directory {I = 0; Count = 0; // record the total number of images in the current folder to prevent array out-of-bounds filenames = NULL; filenames = file. list (New filenamefilter () {public Boolean accept (File Dir, string name) {return (name. endswith (". PNG ") | Name. endswith (". jpg ") | Name. endswith (". JPEG ") | Name. endswith (". GIF ") | Name. endswith (". BMP ") ;}}); // The filter captures the image file for (string temp: filenames) Count ++;} icon = new imageicon (fchooser. getselectedfile (). getpath (); showphoto (icon) ;}} public void showphoto (imageicon icon) {This. setvisible (true); dimension screensize = toolkit. getdefatooltoolkit (). getscreensize (); double xphoto = icon. geticonwidth (); double yphoto = icon. geticonheight (); double xscreen = screensize. width; double yscreen = screensize. height; double temp = 0; temp = yphoto/yscreen; yphoto = yscreen * 0.9; xphoto = (xphoto/temp) * 0.9; icon. setimage (icon. getimage (). getscaledinstance (INT) xphoto, (INT) yphoto, image. scale_default); // The algorithm labelphoto = new jlabel (); labelphoto. seticon (icon); // use my eyes to watch the world labelphoto for you. seticon (icon); panel2 = new jpanel (); panel2.add (labelphoto); this. add (panel2); this. setvisible (true);} public static void main (string [] ARGs) {xiuxiu = new xiuxiu (); xiuxiu. setvisible (true );}}
/*
* *****
* Http://blog.csdn.net/xyydyyqf
*/