JAVA learning Swing chapter label JLabel
Package com. swing; import java. awt. color; import java. awt. component; import java. awt. container; import java. awt. graphics; import javax. swing. icon; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. swingConstants; import javax. swing. windowConstants;/*** 1: the label is used to display text or prompt information in Swing, it supports text strings and icons * the key is that labels contain text strings and icons ** 2. Labels can display a row of read-only text, one image, or text with images, it does not generate any types of events * It simply displays text and images, but you can use the Tag feature to specify the tag Text alignment mode * the focus is to simply display text and images ** 3: JLabel label constructor, with icons, and horizontal icon alignment, with text, set the horizontal alignment mode of the text * With the icon and text, and set the horizontal way of the label content ** 4: The icons on the Swing can be placed in buttons, labels, and other components, used to describe the purpose of the component ** 5: using the Icon interface in Swing to create a chart, you can specify the size of the Icon at the time of creation, color and other features * if you use the Icon interface, you must implement the three interfaces of the Icon * @ author biexiansheng **/public class DrawIcon implements Icon {// implement the Icon interface, three methods of the Icon must be implemented: private int width; // declare the width of the Icon private int height; // declare the length of the Icon // define the constructor public DrawIcon (int wi Dth, int height) {this. width = width; this. height = height;} @ Override public void paintIcon (Component c, Graphics g, int x, int y) {// TODO Auto-generated method stub g. fillOval (x, y, width, height); // The learning difficulty here is in Graphics.} @ Override public int getIconWidth () {// implement the getIconWidth () method // TODO Auto-generated method stub return this. width ;}@ Override public int getIconHeight () {// implement the getIconHeight () method // TODO Auto-generated method stub return this. height;} public static void main (String [] args) {DrawIcon icon = new DrawIcon (150,150); // initialize and pass a value to the constructor // create a tag, and set the text on the label to JLabel jl = new JLabel ("test", icon, SwingConstants in the middle of the label. CENTER); JFrame jf = new JFrame (); // create a JFrame window Container container = jf. getContentPane (); // convert the window to container iner. setBackground (Color. BLUE); container. add (jl); // set the container's external attributes jf. setTitle ("use of tags, Create Icons"); jf. setVisible (true); jf. setSize (340,400); jf. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE );}}
The instance running result is as follows:
Package com. swing; import java. awt. container; import java.net. URL; import javax. swing. icon; import javax. swing. imageIcon;/*** 1: The icons in Swing can be created using a specific image in addition to being drawn. * Swing uses javax. swing. the ImageIcon class creates icons based on existing images * The ImageIcon class implements the Icon interface, and Java supports multiple image formats ** 2: the ImageIcon class has multiple constructor methods. ** first, create a common ImageIcon object. when you really need to set an Image, use the ImageIcon object * to call setImage (image Image) method To operate * Second, directly create a chart from the image source * Third, you can create a chart directly from the image source, you can also add a brief description for this icon * Fourth, create an icon using an image file located on the Computer Network */import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. swingConstants; import javax. swing. windowConstants; public class MyImageIcon extends JFrame {public MyImageIcon () {JFrame jf = new JFrame (); // create a form Container container = jf. getContentPane (); // convert the form to container // create a label // five constructor types of the label. JLabel jl = new JLabel ("This Is A JFrame form", JLabel. CENTER); // obtain the URL of the image = MyImageIcon. class. getResource ("imageButton.jpg"); // java. lang. the getResource () method in the Class can obtain the URL path of the resource file // The region is imagebutton.jpg, this path is relative to the MyImageIcon class file // you can put the imagebutton.jpg image in the same folder as this class Icon icon = new ImageIcon (url); // instantiate the Icon object jl. setIcon (icon); // set the image for the label // set the text to be placed in the middle of the label jl. setHorizontalAlignment (SwingConstants. CENTER); jl. setOpaque (true); // set the label to an opaque container. add (jl); // add the label to the container jf. setTitle ("container upper left corner"); jf. setSize (250,100); jf. setVisible (true); // set the form close mode jf. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub MyImageIcon icon = new MyImageIcon (); // The instantiated object is the implementation constructor }}
The instance running result is as follows: