Java image interface development simple example-Application of ImageIO, jfilechooser, jmenu, and jpanel

Source: Internet
Author: User

Java image interface development example

In the ImageIO, jfilechooser, jmenu, jpanel application, jlabel, jfilechooser, and jmenu application examples, images are browsed by setting the icon attribute of jlabel. In this example, images are browsed through jpanel, use ImageIO to read the corresponding file and display the image. The Code is as follows:

Import java. AWT. graphics;
Import java. AWT. image;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;
Import java. Io. file;
Import java. Io. ioexception;

Import javax. ImageIO. ImageIO;
Import javax. Swing. jfilechooser;
Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. jmenu;
Import javax. Swing. jmenubar;
Import javax. Swing. jmenuitem;
Import javax. Swing. jpanel;

/**
* Simple ImageIO application instance
* @ Author Zuo Jie JDK 5.0
*
*/
Public class example7frame extends jframe {

Private Static final long serialversionuid = 1l;
Private jlabel label;
Private jfilechooser filechooser;
Private imagepanel panel;
Public example7frame (){
Settitle ("Image Browsing"); // sets the Form title
Setsize (500,400); // set the form size
// Create a tag object
Label = new jlabel ();
Add (Label); // Add a label on the form
// Create a file selector object
Filechooser = new jfilechooser ();
Filechooser. setcurrentdirectory (new file ("."); // set the default path to the current directory.
// Create a menu bar
Jmenubar menubar = new jmenubar ();
Setjmenubar (menubar); // Add a menu bar to the form
// Add a menu item
Jmenu menu = new jmenu ("file ");
Menubar. Add (menu); // Add a menu item in the menu bar
// Add the "open" sub-menu item
Jmenuitem openitem = new jmenuitem ("open ");
Menu. Add (openitem); // Add a sub-menu item to the menu item
Panel = new imagepanel (); // create a display image panel
Add (panel); // Add an image panel to the form
// Add events and listeners for the Open menu
Openitem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
// Display the file Selector
Int result = filechooser. showopendialog (null );
// If a file is selected, it is displayed in the tag.
If (result = jfilechooser. approve_option ){
String name = filechooser. getselectedfile (). getpath (); // obtain the path of the selected file
Panel. setimage (name); // you can specify the image path.
Panel. repaint (); // update the Panel
}
}
});
// Add the "exit" sub-menu item
Jmenuitem exititem = new jmenuitem ("quit ");
Menu. Add (exititem); // Add a sub-menu item to the menu item
// Add events and listeners for the "exit" menu
Exititem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
System. Exit (0 );
}
});
}

Public static void main (string [] ARGs ){
Example7frame frame = new example7frame ();
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Frame. setvisible (true );
}
}

/**
* Create a panel for displaying images
*/
Class imagepanel extends jpanel {

Private Static final long serialversionuid = 1l;
Private image;
Private int showwdith;
Private int showheight;
Public void setimage (string filename ){
// Read image files
Try {
Image = ImageIO. Read (new file (filename ));
} Catch (ioexception e ){
E. printstacktrace ();
}
}

Public void paintcomponent (Graphics g ){
Super. paintcomponent (g );
If (image = NULL)
Return;
// Obtain the width and height of the image file
Int imagewidth = image. getwidth (this );
Int imageheight = image. getheight (this );
// Obtain the Panel height and width
Int width = getwidth ();
Int Height = getheight ();
// The maximum display size of the image is the Panel size.
If (imagewidth> width ){
This. showwdith = width;
} Else {
This. showwdith = imagewidth;
}
If (imageheight> height ){
This. showheight = height;
} Else {
This. showheight = imageheight;
}
G. drawimage (image, 0, 0, showwdith, showheight, null, null); // draw the image content on the panel
}
}

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.