"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