Java learn the use of icons in swing chapter tag JLabel

Source: Internet
Author: User
Tags stub

 Packagecom.swing;ImportJava.awt.Color;Importjava.awt.Component;ImportJava.awt.Container;ImportJava.awt.Graphics;ImportJavax.swing.Icon;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;Importjavax.swing.SwingConstants;Importjavax.swing.WindowConstants;/*** 1: The way to display text or hint information in swing is to use the label, which supports text strings and icons * Focus is the label contains a text string and an icon * 2: The label can display a line of read-only text, an image or text with an image, it does not produce any type of event * It's simple to display text and pictures, but you can use the label's attributes to specify the alignment of the text on the label * The focus is simply to display text and pictures * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * , and set the level of the label content to its way * * 4:swing above the icon can be placed on the buttons, labels, and other components, used to describe the purpose of the component * * 5:swing through the icon interface to achieve the creation of the chart, you can create a given icon size, color and other features * If you use the icon interface, you must implement the icon's three interfaces *@authorBiexiansheng **/ Public classDrawIconImplementsicon{//To implement the icon interface, you must implement the three methods of icon    Private intWidth//declare the width of the icon    Private intHeight//declare the length of the icon//Defining construction Methods     PublicDrawIcon (intWidthintheight) {         This. width=width;  This. height=height; } @Override Public voidPainticon (Component C, Graphics G,intXinty) {//TODO auto-generated Method StubG.filloval (x,y,width,height); //The learning difficulties here are in the graphics section. } @Override Public intGeticonwidth () {//implementing the Geticonwidth () method//TODO auto-generated Method Stub        return  This. Width; } @Override Public intGeticonheight () {//implementing the Geticonheight () method//TODO auto-generated Method Stub        return  This. Height; }         Public Static voidMain (string[] args) {DrawIcon icon=NewDrawIcon (150,150);//Initialize and pass values to the construction method//Create a label and set the text on the label right in the middle of the labelJLabel jl=NewJLabel ("Test", Icon,swingconstants.center); JFrame JF=NewJFrame ();//Create a jframe windowContainer Container=jf.getcontentpane ();//Convert a window to a containerContainer.setbackground (Color.Blue);        Container.add (JL); //set the external properties of a containerJf.settitle ("Use of tags, create icons"); Jf.setvisible (true); Jf.setsize (340,400);    Jf.setdefaultcloseoperation (Windowconstants.exit_on_close); }    }

The results of the instance run are

Java learn the use of icons in swing chapter tag JLabel

Related Article

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.