Javaform small form example uses label and Image

Source: Internet
Author: User
Tags gety

Let's share a small program and give it an example for beginners .. I am a beginner. Function: displays the mouse position .... There are several images. After you click the icon, the text information will be changed and saved with the label, and the image will be saved with several labels to locate each label on the form. You just need to pass the code. Create an imagefolder under the root directory, put two small tables, happy.gif, unhappy.gif, or modify the path by yourself. I will put the project online.

 
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 image label2 --- 7 public jlabel label3; Public jlabel label4; public jlabel label5; Public jlabel label6; Public jlabel label7; Public jlabel label8; // hide it because it appears on the far left. Public String filename1 = "image/happy.gif" is annoying "; // file path Public String filename2 = "image/unhappy.gif"; Public imageicon image1 = NULL; Public imageicon image2 = NULL; int X [] = new int [9]; // record the image public winform () {for (INT I = 0; I <9; I ++) x [I] = 0; settitle ("window applet"); setsize (500,400); setlocationrelativeto (null); // setdefaclocloseoperation (jframe. exit_on_close); // click red X to exit the program label1 = new jlabel (); label1.settext ("cursor position:"); label1.setfont (New Java. AWT. font ("dialog",); // sets the sub-format, // label1.sethorizontalalignment (jlabel. left); // label1.setverticalignment (jlabel. top); label1.setbounds (10, 10,300, 40); // X, Y, width, height, coordinate X, Y, width, height // label1.setlocation ); getcontentpane (). add (label1); // Add image1 = new imageicon (filename1) to form; // obtain the image image2 = new imageicon (filename2); label2 = new jlabel (); label2.setbounds (300,100, 30, 30); label2.seticon (image1); // set the label image // 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 (mouseevent e) {TT (E. getx (), E. gety ();} // enter the event public void mouseentered (mouseevent e) {TT (E. getx (), E. gety ();} // exit public void mouseexited (mouseevent e) {TT (E. getx (), E. gety ();} // click the event public void mouseclicked (mouseevent e) {TT (E. getx (), E. gety ();} // press the event public void mousepressed (mouseevent e) {TT (E. getx (), E. gety () ;}}) ;}public void TT (int x, int y) {label1.settext ("cursor position:" + x + ":" + y ); if (300 + 5 <X & x <300 + 30 & 100 + 30 <Y & Y <100 + 60) // locate label2.seticon (changeimage (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) {// determine the current image, modify the changes, and return the image to be changed to 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.