Use jfilechooser to select an image file and display it on the panel

Source: Internet
Author: User

Disadvantage: You need to click a click to display the image. For example, you can display the image only when the maximum value is minimized.

Code:
  1. Import java. AWT .*;
  2. Import java. Io. file;
  3. Import java. AWT. event .*;
  4. Import javax. Swing. filechooser .*;
  5. Import javax. Swing .*;
  6. Public class test4 {
  7. Public static void main (string [] ARGs ){
  8. Filechooser fc = new filechooser ();
  9. }
  10. }
  11. Class filechooser extends jframe implements actionlistener {
  12. Container P;
  13. Static int I = 0;
  14. Jpanel imgpane;
  15. Jlabel labeimag;
  16. Jtoolbar TB = new jtoolbar ();
  17. Jbutton BTN = new jbutton ("O ");
  18. Public filechooser (){
  19. P = getcontentpane ();
  20. BTN. settooltiptext ("open to select image"); // The button prompts
  21. BTN. setforeground (color. Red );
  22. BTN. addactionlistener (this );
  23. Imgpane = new jpanel ();
  24. TB. Add (BTN );
  25. P. Add (TB, borderlayout. North );
  26. Setbounds (100,100,800,600 );
  27. Setdefaclocloseoperation (exit_on_close );
  28. Setvisible (true );
  29. }
  30. Public void actionreceivmed (actionevent e ){
  31. If (E. getsource () = BTN ){
  32. Jfilechooser choosefile = new jfilechooser ();
  33. Choosefile. addchoosablefilefilter (New filecanchoose ());
  34. Int returnval = choosefile. showopendialog (null );
  35. If (returnval = choosefile. approve_option ){
  36. If (I! = 0 ){
  37. Imgpane. Remove (labeimag); // remove the previous image
  38. }
  39. I ++;
  40. File F = choosefile. getselectedfile ();
  41. String fname = f. getabsolutepath ();
  42. Labeimag = new jlabel ();
  43. Labeimag. seticon (New imageicon (fname ));
  44. Labeimag. sethorizontalalignment (jlabel. center); // you can specify the center of the image.
  45. Imgpane. Add (labeimag, borderlayout. center );
  46. P. Add (imgpane, borderlayout. center );
  47. }
  48. }
  49. }
  50. }
  51. Class filecanchoose extends filefilter {// file filter, set to select the corresponding type of file
  52. Public Boolean accept (File file ){
  53. String name = file. getname ();
  54. Return (name. tolowercase (). endswith (". GIF") |
  55. Name. tolowercase (). endswith (". jpg") |
  56. Name. tolowercase (). endswith (". BMP") |
  57. Name. tolowercase (). endswith (". PNG") |
  58. Name. tolowercase (). endswith (". jpeg "));
  59. }
  60. Public String getdescription (){
  61. Return "image files:. GIF,. jpg,. BMP,. PNG,. jpeg ";
  62. }
  63. }

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.