Javaform Small Form example use label and image

Source: Internet
Author: User
Tags gety
Do a small program, share, give beginners an example bar. I'm just a beginner. Function: Show the location of the mouse .... There are a few pictures, click will change the expression with the label stored text information, with a few labels to save the picture, each label positioned on the form. Just pass the code to the test. Create an image folder under the root directory, put two small expressions, happy.gif,unhappy.gif or modify your path. I'll put the project on the Internet.
Import Javax.swing.ImageIcon;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import java.awt.event.*;
		public class Imageshow {public static void main (string[] args) {JFrame frame1 = new WinForm ();  Frame1.setvisible (TRUE);//Display}} class WinForm extends jframe{public JLabel Label1;  Label label Public JLabel Label2;
	Used to display the picture Label2---7 public JLabel label3;
	Public JLabel Label4;
	Public JLabel Label5;
	Public JLabel label6;
	Public JLabel Label7;  Public JLabel label8;//Hidden because it will appear on the far left, very annoying public string filename1 = "Image/happy.gif";/file path public string filename2 =
	"Image/unhappy.gif";
	public ImageIcon image1 = null;
	public ImageIcon image2 = null;  int x[] = new INT[9]; 
		Record I label now picture public WinForm () {for (int i=0;i<9;i++) x[i]= 0;
		Settitle ("Window applet"); 
		SetSize (500, 400); Setlocationrelativeto (NULL); Initial in the screen Central setdefaultcloseoperation (jframe.exit_on_close); 
		So click on red X to exit the program Label1 = new JLabel ();  Label1.settext (the position of the mouse pointer:   "); Label1.setfont (New Java.awt.Font ("Dialog", 1,18));
		Set the text format,//label1.sethorizontalalignment (Jlabel.left);
		Label1.setverticalalignment (Jlabel.top);
		Label1.setbounds (n),//x,y,width,height, coordinate x,y, width, height//label1.setlocation (0,0); Getcontentpane (). Add (Label1);
         
         Join Form Image1 =new ImageIcon (filename1);//Get Picture Image2 = new ImageIcon (filename2);
         Label2 = new JLabel ();
         Label2.setbounds (300, 100, 30, 30);
         Label2.seticon (image1);//Set the picture of the label//label2.setlocation (100,100);
         
         Getcontentpane (). Add (Label2);
         Label3 = new JLabel ();
         Label3.setbounds (250, 150, 30, 30);
         Label3.seticon (Image1);
         Label3.setlocation (100,100);
         
         Getcontentpane (). Add (LABEL3);
         Label4 = new JLabel ();
         Label4.setbounds (350, 150, 30, 30);
         Label4.seticon (Image1);
         Label4.setlocation (100,100); Getcontentpane (). Add (Label4);
         Label5 = new JLabel ();
         Label5.setbounds (200, 200, 30, 30);
         Label5.seticon (Image1);
         Label5.setlocation (100,100);
         
         Getcontentpane (). Add (Label5);
         Label6 = new JLabel ();
         Label6.setbounds (300, 200, 30, 30);
         Label6.seticon (Image1);
         Label6.setlocation (100,100);
         
         Getcontentpane (). Add (LABEL6);
         LABEL7 = new JLabel ();
         Label7.setbounds (400, 200, 30, 30);
         Label7.seticon (Image1);
         Label7.setlocation (100,100);
         
         Getcontentpane (). Add (LABEL7);
         Label8 = new JLabel ();
         Label8.setbounds (0, 0, 0, 0);
         Label8.seticon (Image1);
         
         Label8.setlocation (000,000);
         
        
        Getcontentpane (). Add (Label8); Addmouselistener (New MouseListener () {//release event public void mousereleased (Mouseeven T e) {TT (E.GETX (), E.GetY ());
		   	     //Enter event public void mouseentered (MouseEvent e) {TT (E.GETX (), e.gety ());		   	     	
		   	     //Leave public void mouseexited (MouseEvent e) {TT (E.GETX (), e.gety ()); //Click event public void mouseclicked (MouseEvent e) {TT (
	   		    E.getx (), e.gety ());
		   	    //press event public void mousepressed (MouseEvent e) {TT (E.GETX (), e.gety ());
	}
   	        }
   	         ); 
		public void tt (int x,int y) {label1.settext ("position of the mouse pointer:" +x+ ":" +y); if (300+5 < x && x < 300+30 && 100+30<y&&y<100+60)//positioning Label2.seticon (Changeima
		GE (2)); if (250+5 < x && x < 250+30 && 150+30<y&&y<150+60) Label3.seticon (Changeimage (3))
		;
		  if (350+5 < x && x < 350+30 && 150+30<y&&y<150+60)  Label4.seticon (Changeimage (4)); if (200+5 < x && x < 200+30 && 200+30<y&&y<200+60) Label5.seticon (Changeimage (5))
		; if (300+5 < x && x < 300+30 && 200+30<y&&y<200+60) Label6.seticon (Changeimage (6))
		; if (400+5 < x && x < 400+30 && 200+30<y&&y<200+60) Label7.seticon (Changeimage (7))
   	;
			Public ImageIcon changeimage (int y) {//judge the current picture, and modify the changed case, and return the image to be changed if (x[y] = = 1) {X[y] = 2;
		return image2;
			else {X[y] = 1;
		return image1;

 }
	}
}

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.