The 14th week of Java programming: A preliminary study of GUI programming

Source: Internet
Author: User

"Purpose"
1. Master the use of graphical windowbuilder.
2. Understand the event mechanism in Java.

"Target"
1. Mastering Installation in Eclipsemethod of Windowbuilder.
2. Learn about the event mechanism in Java.
3. Learn about reusing controls in the GUI.
4. Complete a GUI job, the job requirements are as follows.
(1) function: There is a button (JButton) and a label (JLabel), when you click the button, you can display the next picture. Click on the image to download and get a sample image.
Import Java.awt.eventqueue;import java.awt.image;import Javax.imageio.imageio;import Javax.swing.ImageIcon;import Javax.swing.jframe;import Javax.swing.jpanel;import Javax.swing.border.emptyborder;import Javax.swing.JButton; Import Javax.swing.jlabel;import Java.awt.color;import Java.awt.event.actionlistener;import Java.awt.event.actionevent;import Java.io.file;import Java.io.ioexception;public class ImgDemo extends JFrame { Private JPanel contentpane;private int idimg = 1;/** * Launch the application. */public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () {public void run () {try {Imgdemo frame = n EW Imgdemo (); frame.setvisible (true);} catch (Exception e) {e.printstacktrace ();}}});} /** * Create the frame. */public Imgdemo () {settitle ("\u56fe\u7247\u663e\u793a"); Setdefaultcloseoperation (Jframe.exit_on_close); SetBounds (576, 604), ContentPane = new JPanel (); Contentpane.setborder (New Emptyborder (5, 5, 5, 5)); Setcontentpane (ContentPane); Contentpane.setlayout (nULL); final JLabel Lblnewlabel = new JLabel ("new label"); Lblnewlabel.setbackground (Color.yellow); Lblnewlabel.setforeground (Color.pink); Lblnewlabel.setbounds (n., (); Contentpane.add (Lblnewlabel); JButton Btnnewbutton = new JButton ("\u663e\u793a\u4e0b\u4e00\u5f20\u56fe\u7247"); Btnnewbutton.addactionlistener ( New ActionListener () {public void actionperformed (ActionEvent e) {ImageIcon Icon;try {//Get picture path string strimgfile = "E:/de mophoto/";//Get the full path of the picture Strimgfile = Strimgfile + string.valueof (idimg) +". jpg ";//Read picture icon = new ImageIcon (Imageio.read (n EW File (Strimgfile));//Gets the picture from the chart image image = Icon.getimage (); Zoom image smallimage = Image.getscaledinstance (400,400,image.scale_fast);//Convert image file to Imageiconicon = new ImageIcon (Smallimage); Lblnewlabel.seticon (icon);//prepare for the next picture, altogether 9 pictures, display the 9th post, re-display the first idimg = Idimg+1;if (idimg==10) idimg = 1;} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}}); Btnnewbutton.setbounds (194, Panax Notoginseng, 153); Contentpane.add (btnNewbutton);}} 

The 14th week of Java programming: A preliminary study of GUI programming

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.