Cardlayout layout exercise (small image browser)

Source: Internet
Author: User
1/* 2 involves the loading of images in the panel, as well as the frame close method, cardlayout (INT hgap, int vgap) the size of the card panel is determined by the use of 3 anonymous classes... 4 */5 import Java. AWT. *; 6 Import Java. AWT. event. *; 7 Import javax. swing. *; 8 public class cardlayoutdemo extends frame {9 panel pcards = new Panel (); // card panel 10 cardlayout clayout = new cardlayout (120, 50 ); // set the vertical and horizontal distance between the card and panel boundary to 11 Public cardlayoutdemo () {12 setlayout (New borderlayout (100, 20); 13 panel pbtn = new Panel (); // button panel 14 15 pcards. setlayout (clayout); 16 // pcards. setpreferredsize (new dimension (); // This statement no longer works: because its parent class cardlayoutdemo uses the borderlayout layout method, 17 pcards is automatically filled. setbackground (color. red); 18 pbtn. setbackground (color. yellow); 19 pbtn. setlayout (New flowlayout (flowlayout. center, 10, 10); 20 pbtn. setpreferredsize (new dimension (200, 50); 21 22 button tmpb; 23 pbtn. add (tmpb = new button ("first"); 24 tmpb. addactionlistener (New myactionlistener (); 25 tmpb. setactioncommand ("first"); 26 pbtn. add (tmpb = new button ("Next"); 27 tmpb. addactionlistener (New myactionlistener (); 28 tmpb. setactioncommand ("Next"); 29 30 pbtn. add (tmpb = new button ("previous"); 31 tmpb. addactionlistener (New myactionlistener (); 32 tmpb. setactioncommand ("previous"); 33 34 pbtn. add (tmpb = new button ("last"); 35 tmpb. addactionlistener (New myactionlistener (); 36 tmpb. setactioncommand ("last"); 37 38 39 for (INT I = 1; I <= 4; ++ I) {40 mypanel tmpp; 41 pcards. add ("" + I, tmpp = new mypanel (I) {42 Public void paint (Graphics g) {43G. drawimage (New imageicon ("zjy" + I + ". jpg "). getimage (), 20, 0,300,400, this); 44} 45}); 46 tmpp. setbackground (color. blue); 47 // tmpp. setsize (new dimension (300,400); // tmpp accepts anonymous class objects, you can use this method to change the attributes of the anonymous class 48 // The reason is that its size is determined by cardlayout (INT hgap, int vgap) 49} 50 add (pbtn, "North"); 51 add (pcards, "center"); 52} 53 class myactionlistener implements actionlistener {54 public void actionreceivmed (actionevent e) {55 string STR = E. getactioncommand (); 56 If (Str. equals ("first") 57 clayout. first (pcards); 58 else if (Str. equals ("Next") 59 clayout. next (pcards); 60 else if (Str. equals ("previous") 61 clayout. previous (pcards); 62 else if (Str. equals ("last") 63 clayout. last (pcards); 64} 65} 66 67 public static void main (string ARGs []) {68 cardlayoutdemo mywindow = new cardlayoutdemo (); 69 mywindow. setsize (new dimension (600,600); 70 mywindow. setresizable (false); 71 mywindow. addwindowlistener (New myclosinglistener (); 72 mywindow. setvisible (true); 73} 74} 75 76 77 class myclosinglistener extends windowadapter {78 public void windowclosing (invalid wevent e) {79 system. exit (0); 80} 81} 82 83 class mypanel extends panel {84 int I; 85 public mypanel (int I) {86 This. I = I; 87} 88}

 

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.