Java by API/javax.swing/JLabel

Материал из Java эксперт
Перейти к: навигация, поиск

extends JLabel

   <source lang="java">

import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class ImageTest {

 public static void main(String[] args) {
   JPanel panel = new JPanel();
   ImageLabel label = new ImageLabel(new ImageIcon("images/reactor.png"));
   label.setLocation(29, 37);
   panel.add(label);
   JFrame frame = new JFrame();
   frame.getContentPane().add(panel);
   frame.pack();
   frame.setVisible(true);
 }

} class ImageLabel extends JLabel {

 public ImageLabel(String img) {
   this(new ImageIcon(img));
 }
 public ImageLabel(ImageIcon icon) {
   setIcon(icon);
   // setMargin(new Insets(0,0,0,0));
   setIconTextGap(0);
   // setBorderPainted(false);
   setBorder(null);
   setText(null);
   setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
 }

}


 </source>
   
  
 
  



JLabel: addMouseMotionListener(MouseMotionListener l)

   <source lang="java">

import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; import javax.swing.JLabel; class CustomListener implements MouseMotionListener {

 public void mouseMoved(MouseEvent me) {
   System.out.println("mouseMoved");
 }
 public void mouseDragged(MouseEvent me) {
   System.out.println("mouseDragged");
 }

} public class Main {

 public static void main(String[] a) {
   JFrame frame = new JFrame("Popup JComboBox");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JLabel label = new JLabel();
   label.addMouseMotionListener(new CustomListener() {
   });
   frame.add(label);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}

 </source>
   
  
 
  



JLabel.BOTTOM

   <source lang="java">

import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.LineBorder; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Label Text Pos");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new GridLayout(2, 2));
   Border border = LineBorder.createGrayLineBorder();
   Icon warnIcon = MetalIconFactory.getTreeComputerIcon();
   JLabel label1 = new JLabel(warnIcon);
   label1.setText("Left-Bottom");
   label1.setHorizontalTextPosition(JLabel.LEFT);
   label1.setVerticalTextPosition(JLabel.BOTTOM);
   label1.setBorder(border);
   frame.add(label1);
   JLabel label2 = new JLabel(warnIcon);
   label2.setText("Right-TOP");
   label2.setHorizontalTextPosition(JLabel.RIGHT);
   label2.setVerticalTextPosition(JLabel.TOP);
   label2.setBorder(border);
   frame.add(label2);
   JLabel label3 = new JLabel(warnIcon);
   label3.setText("Center-Center");
   label3.setHorizontalTextPosition(JLabel.CENTER);
   label3.setVerticalTextPosition(JLabel.CENTER);
   label3.setBorder(border);
   frame.add(label3);
   JLabel label4 = new JLabel(warnIcon);
   label4.setText("Center-Bottom");
   label4.setHorizontalTextPosition(JLabel.CENTER);
   label4.setVerticalTextPosition(JLabel.BOTTOM);
   label4.setBorder(border);
   frame.add(label4);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel.CENTER

   <source lang="java">

import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.LineBorder; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Label Text Pos");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new GridLayout(2, 2));
   Border border = LineBorder.createGrayLineBorder();
   Icon warnIcon = MetalIconFactory.getTreeComputerIcon();
   JLabel label1 = new JLabel(warnIcon);
   label1.setText("Left-Bottom");
   label1.setHorizontalTextPosition(JLabel.LEFT);
   label1.setVerticalTextPosition(JLabel.BOTTOM);
   label1.setBorder(border);
   frame.add(label1);
   JLabel label2 = new JLabel(warnIcon);
   label2.setText("Right-TOP");
   label2.setHorizontalTextPosition(JLabel.RIGHT);
   label2.setVerticalTextPosition(JLabel.TOP);
   label2.setBorder(border);
   frame.add(label2);
   JLabel label3 = new JLabel(warnIcon);
   label3.setText("Center-Center");
   label3.setHorizontalTextPosition(JLabel.CENTER);
   label3.setVerticalTextPosition(JLabel.CENTER);
   label3.setBorder(border);
   frame.add(label3);
   JLabel label4 = new JLabel(warnIcon);
   label4.setText("Center-Bottom");
   label4.setHorizontalTextPosition(JLabel.CENTER);
   label4.setVerticalTextPosition(JLabel.BOTTOM);
   label4.setBorder(border);
   frame.add(label4);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel: getTransferHandler()

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Container; import java.awt.Image; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.TransferHandler; public class Main {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Clip Image");
   Container contentPane = frame.getContentPane();
   final Clipboard clipboard = frame.getToolkit().getSystemClipboard();
   Icon icon = new ImageIcon("jaeger.jpg");
   final JLabel label = new JLabel(icon);
   label.setTransferHandler(new ImageSelection());
   JScrollPane pane = new JScrollPane(label);
   contentPane.add(pane, BorderLayout.CENTER);
   JButton copy = new JButton("Copy");
   copy.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       TransferHandler handler = label.getTransferHandler();
       handler.exportToClipboard(label, clipboard,
           TransferHandler.COPY);
     }
   });
   JButton clear = new JButton("Clear");
   clear.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       label.setIcon(null);
     }
   });
   JButton paste = new JButton("Paste");
   paste.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       Transferable clipData = clipboard.getContents(clipboard);
       if (clipData != null) {
         if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) {
           TransferHandler handler = label.getTransferHandler();
           handler.importData(label, clipData);
         }
       }
     }
   });
   JPanel p = new JPanel();
   p.add(copy);
   p.add(clear);
   p.add(paste);
   contentPane.add(p, BorderLayout.SOUTH);
   frame.setSize(300, 300);
   frame.show();
 }

} class ImageSelection extends TransferHandler implements Transferable {

 private static final DataFlavor flavors[] = { DataFlavor.imageFlavor };
 private JLabel source;
 private Image image;
 public int getSourceActions(JComponent c) {
   return TransferHandler.COPY;
 }
 public boolean canImport(JComponent comp, DataFlavor flavor[]) {
   if (!(comp instanceof JLabel)) {
     return false;
   }
   for (int i = 0, n = flavor.length; i < n; i++) {
     for (int j = 0, m = flavors.length; j < m; j++) {
       if (flavor[i].equals(flavors[j])) {
         return true;
       }
     }
   }
   return false;
 }
 public Transferable createTransferable(JComponent comp) {
   // Clear
   source = null;
   image = null;
   if (comp instanceof JLabel) {
     JLabel label = (JLabel) comp;
     Icon icon = label.getIcon();
     if (icon instanceof ImageIcon) {
       image = ((ImageIcon) icon).getImage();
       source = label;
       return this;
     }
   }
   return null;
 }
 public boolean importData(JComponent comp, Transferable t) {
   if (comp instanceof JLabel) {
     JLabel label = (JLabel) comp;
     if (t.isDataFlavorSupported(flavors[0])) {
       try {
         image = (Image) t.getTransferData(flavors[0]);
         ImageIcon icon = new ImageIcon(image);
         label.setIcon(icon);
         return true;
       } catch (UnsupportedFlavorException ignored) {
       } catch (IOException ignored) {
       }
     }
   }
   return false;
 }
 // Transferable
 public Object getTransferData(DataFlavor flavor) {
   if (isDataFlavorSupported(flavor)) {
     return image;
   }
   return null;
 }
 public DataFlavor[] getTransferDataFlavors() {
   return flavors;
 }
 public boolean isDataFlavorSupported(DataFlavor flavor) {
   return flavor.equals(DataFlavor.imageFlavor);
 }

}

 </source>
   
  
 
  



JLabel.LEFT

   <source lang="java">

import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MainClass extends JPanel {

 public MainClass() {
   JLabel lblMarried = new JLabel("Are you married?", JLabel.LEFT);
   JLabel lblGolf = new JLabel("Do you play golf?", JLabel.RIGHT);
   
   add(lblMarried);
   add(lblGolf);
 }
 public static void main(String[] args) {
   JFrame frame = new JFrame();
   frame.getContentPane().add(new MainClass());
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(200, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel.RIGHT

   <source lang="java">

import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MainClass extends JPanel {

 public MainClass() {
   JLabel lblMarried = new JLabel("Are you married?", JLabel.LEFT);
   JLabel lblGolf = new JLabel("Do you play golf?", JLabel.RIGHT);
   
   add(lblMarried);
   add(lblGolf);
 }
 public static void main(String[] args) {
   JFrame frame = new JFrame();
   frame.getContentPane().add(new MainClass());
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(200, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel: setBoder(Border border)

   <source lang="java">

import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.LineBorder; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Label Text Pos");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new GridLayout(2, 2));
   Border border = LineBorder.createGrayLineBorder();
   Icon warnIcon = MetalIconFactory.getTreeComputerIcon();
   JLabel label1 = new JLabel(warnIcon);
   label1.setText("Left-Bottom");
   label1.setHorizontalTextPosition(JLabel.LEFT);
   label1.setVerticalTextPosition(JLabel.BOTTOM);
   label1.setBorder(border);
   frame.add(label1);
   JLabel label2 = new JLabel(warnIcon);
   label2.setText("Right-TOP");
   label2.setHorizontalTextPosition(JLabel.RIGHT);
   label2.setVerticalTextPosition(JLabel.TOP);
   label2.setBorder(border);
   frame.add(label2);
   JLabel label3 = new JLabel(warnIcon);
   label3.setText("Center-Center");
   label3.setHorizontalTextPosition(JLabel.CENTER);
   label3.setVerticalTextPosition(JLabel.CENTER);
   label3.setBorder(border);
   frame.add(label3);
   JLabel label4 = new JLabel(warnIcon);
   label4.setText("Center-Bottom");
   label4.setHorizontalTextPosition(JLabel.CENTER);
   label4.setVerticalTextPosition(JLabel.BOTTOM);
   label4.setBorder(border);
   frame.add(label4);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel: setBounds(int x, int y, int width, int height)

   <source lang="java">

import java.awt.Color; import java.awt.FlowLayout; import javax.swing.BoxLayout; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Main extends JFrame {

 public Main() {
   super("JLayeredPane Demo");
   setSize(256, 256);
   JPanel content = new JPanel();
   content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
   content.setOpaque(false);
   JLabel label1 = new JLabel("Username:");
   label1.setForeground(Color.white);
   content.add(label1);
   JTextField field = new JTextField(15);
   content.add(field);
   JLabel label2 = new JLabel("Password:");
   label2.setForeground(Color.white);
   content.add(label2);
   JPasswordField fieldPass = new JPasswordField(15);
   content.add(fieldPass);
   setLayout(new FlowLayout());
   add(content);
   ((JPanel) getContentPane()).setOpaque(false);
   ImageIcon earth = new ImageIcon("largejexpLogo.png");
   JLabel backlabel = new JLabel(earth);
   getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
   backlabel.setBounds(0, 0, earth.getIconWidth(), earth.getIconHeight());
   super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setVisible(true);
 }
 public static void main(String[] args) {
   new Main();
 }

}

 </source>
   
  
 
  



JLabel: setDisplayedMnemonicIndex(int index)

   <source lang="java">

import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Main {

 public static void main(String[] args) {
   JTextField firstField = new JTextField(10);
   JTextField middleField = new JTextField(10);
   JTextField lastField = new JTextField(10);
   JLabel firstLabel = new JLabel("First Name", JLabel.RIGHT);
   firstLabel.setDisplayedMnemonic("F");
   firstLabel.setLabelFor(firstField);
   JLabel middleLabel = new JLabel("Middle Initial", JLabel.RIGHT);
   middleLabel.setDisplayedMnemonic("I");
   middleLabel.setDisplayedMnemonicIndex(7);
   middleLabel.setLabelFor(middleField);
   JLabel lastLabel = new JLabel("Last Name", JLabel.RIGHT);
   lastLabel.setDisplayedMnemonic("L");
   lastLabel.setLabelFor(lastField);
   JPanel p = new JPanel();
   p.setLayout(new GridLayout(3, 2, 5, 5));
   p.add(firstLabel);
   p.add(firstField);
   p.add(middleLabel);
   p.add(middleField);
   p.add(lastLabel);
   p.add(lastField);
   JFrame f = new JFrame("MnemonicLabels");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.setContentPane(p);
   f.pack();
   f.setVisible(true);
 }

}

 </source>
   
  
 
  



JLabel: setDisplayedMnemonic(int key) (KeyEvent.VK_U)

   <source lang="java">

import java.awt.BorderLayout; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class MainClass {

 public static void main(final String args[]) {
   JFrame frame = new JFrame("Password Example");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JPanel userPanel = new JPanel(new BorderLayout());
   JLabel userLabel = new JLabel("Username: ");
   userLabel.setDisplayedMnemonic(KeyEvent.VK_U);
   JTextField userTextField = new JTextField();
   userLabel.setLabelFor(userTextField);
   userPanel.add(userLabel, BorderLayout.WEST);
   userPanel.add(userTextField, BorderLayout.CENTER);
   JPanel passPanel = new JPanel(new BorderLayout());
   JLabel passLabel = new JLabel("Password: ");
   passLabel.setDisplayedMnemonic(KeyEvent.VK_P);
   JPasswordField passTextField = new JPasswordField();
   passLabel.setLabelFor(passTextField);
   passPanel.add(passLabel, BorderLayout.WEST);
   passPanel.add(passTextField, BorderLayout.CENTER);
   JPanel panel = new JPanel(new BorderLayout());
   panel.add(userPanel, BorderLayout.NORTH);
   panel.add(passPanel, BorderLayout.SOUTH);
   frame.add(panel, BorderLayout.NORTH);
   frame.setSize(250, 150);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel: setHorizontalTextPosition(int align)

   <source lang="java">

import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.LineBorder; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Label Text Pos");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new GridLayout(2, 2));
   Border border = LineBorder.createGrayLineBorder();
   Icon warnIcon = MetalIconFactory.getTreeComputerIcon();
   JLabel label1 = new JLabel(warnIcon);
   label1.setText("Left-Bottom");
   label1.setHorizontalTextPosition(JLabel.LEFT);
   label1.setVerticalTextPosition(JLabel.BOTTOM);
   label1.setBorder(border);
   frame.add(label1);
   JLabel label2 = new JLabel(warnIcon);
   label2.setText("Right-TOP");
   label2.setHorizontalTextPosition(JLabel.RIGHT);
   label2.setVerticalTextPosition(JLabel.TOP);
   label2.setBorder(border);
   frame.add(label2);
   JLabel label3 = new JLabel(warnIcon);
   label3.setText("Center-Center");
   label3.setHorizontalTextPosition(JLabel.CENTER);
   label3.setVerticalTextPosition(JLabel.CENTER);
   label3.setBorder(border);
   frame.add(label3);
   JLabel label4 = new JLabel(warnIcon);
   label4.setText("Center-Bottom");
   label4.setHorizontalTextPosition(JLabel.CENTER);
   label4.setVerticalTextPosition(JLabel.BOTTOM);
   label4.setBorder(border);
   frame.add(label4);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel: setIcon(Icon icon)

   <source lang="java">

import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class ImageTest {

 public static void main(String[] args) {
   JPanel panel = new JPanel();
   ImageLabel label = new ImageLabel(new ImageIcon("images/reactor.png"));
   label.setLocation(29, 37);
   panel.add(label);
   JFrame frame = new JFrame();
   frame.getContentPane().add(panel);
   frame.pack();
   frame.setVisible(true);
 }

} class ImageLabel extends JLabel {

 public ImageLabel(String img) {
   this(new ImageIcon(img));
 }
 public ImageLabel(ImageIcon icon) {
   setIcon(icon);
   // setMargin(new Insets(0,0,0,0));
   setIconTextGap(0);
   // setBorderPainted(false);
   setBorder(null);
   setText(null);
   setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
 }

}


 </source>
   
  
 
  



JLabel: setLabelFor(Component c)

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.KeyEvent; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class MainClass {

 public static void main(String[] a) {
   JFrame frame = new JFrame("LabelFor Sample");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JLabel label = new JLabel("Username");
   JTextField textField = new JTextField();
   label.setDisplayedMnemonic(KeyEvent.VK_U);
   label.setLabelFor(textField);
   Container box = Box.createHorizontalBox();
   box.add(label);
   box.add(textField);
   frame.add(box, BorderLayout.NORTH);
   frame.add(new JButton("Submit"), BorderLayout.SOUTH);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel: setOpaque(boolean isOpaque)

   <source lang="java">

import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; public class Main extends JFrame {

 public Main() {
   super("JLabel Demo");
   setSize(600, 100);
   JPanel content = new JPanel(new GridLayout(1, 4, 4, 4));
   JLabel label = new JLabel("jexp");
   label.setBackground(Color.white);
   content.add(label);
   label = new JLabel("jexp", SwingConstants.CENTER);
   label.setOpaque(true);
   label.setBackground(Color.white);
   content.add(label);
   label = new JLabel("jexp");
   label.setFont(new Font("Helvetica", Font.BOLD, 18));
   label.setOpaque(true);
   label.setBackground(Color.white);
   content.add(label);
   ImageIcon image = new ImageIcon("jexpLogo.gif");
   label = new JLabel("jexp", image, SwingConstants.RIGHT);
   label.setVerticalTextPosition(SwingConstants.TOP);
   label.setOpaque(true);
   label.setBackground(Color.white);
   content.add(label);
   getContentPane().add(content);
   setVisible(true);
 }
 public static void main(String args[]) {
   new Main();
 }

}

 </source>
   
  
 
  



JLabel: setPreferredSize(Dimension preferredSize)

   <source lang="java">

/* From http://java.sun.ru/docs/books/tutorial/index.html */ /*

* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
*  list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
*  this list of conditions and the following disclaimer in the documentation
*  and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/

import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JLabel; /* FrameDemo.java requires no other files. */ public class Main {

 /**
  * Create the GUI and show it. For thread safety, this method should be
  * invoked from the event-dispatching thread.
  */
 private static void createAndShowGUI() {
   //Suggest that the L&F (rather than the system)
   //decorate all windows. This must be invoked before
   //creating the JFrame. Native look and feels will
   //ignore this hint.
   JFrame.setDefaultLookAndFeelDecorated(true);
   //Create and set up the window.
   JFrame frame = new JFrame("FrameDemo");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JLabel emptyLabel = new JLabel("");
   emptyLabel.setPreferredSize(new Dimension(175, 100));
   frame.add(emptyLabel, BorderLayout.CENTER);
   //Display the window.
   frame.pack();
   frame.setVisible(true);
 }
 public static void main(String[] args) {
   //Schedule a job for the event-dispatching thread:
   //creating and showing this application"s GUI.
   javax.swing.SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       createAndShowGUI();
     }
   });
 }

}


 </source>
   
  
 
  



JLabel: setText(String str)

   <source lang="java">

import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.LineBorder; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Label Text Pos");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new GridLayout(2, 2));
   Border border = LineBorder.createGrayLineBorder();
   Icon warnIcon = MetalIconFactory.getTreeComputerIcon();
   JLabel label1 = new JLabel(warnIcon);
   label1.setText("Left-Bottom");
   label1.setHorizontalTextPosition(JLabel.LEFT);
   label1.setVerticalTextPosition(JLabel.BOTTOM);
   label1.setBorder(border);
   frame.add(label1);
   JLabel label2 = new JLabel(warnIcon);
   label2.setText("Right-TOP");
   label2.setHorizontalTextPosition(JLabel.RIGHT);
   label2.setVerticalTextPosition(JLabel.TOP);
   label2.setBorder(border);
   frame.add(label2);
   JLabel label3 = new JLabel(warnIcon);
   label3.setText("Center-Center");
   label3.setHorizontalTextPosition(JLabel.CENTER);
   label3.setVerticalTextPosition(JLabel.CENTER);
   label3.setBorder(border);
   frame.add(label3);
   JLabel label4 = new JLabel(warnIcon);
   label4.setText("Center-Bottom");
   label4.setHorizontalTextPosition(JLabel.CENTER);
   label4.setVerticalTextPosition(JLabel.BOTTOM);
   label4.setBorder(border);
   frame.add(label4);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel: setToolTipText(String text)

   <source lang="java">


import java.awt.FlowLayout; import java.awt.HeadlessException; import javax.swing.JFrame; import javax.swing.JLabel; public class Main extends JFrame {

 public Main() throws HeadlessException {
   setSize(150, 300);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setLayout(new FlowLayout());
   JLabel label1 = new JLabel("Username :", JLabel.RIGHT);
   JLabel label2 = new JLabel("Password :", JLabel.RIGHT);
   JLabel label3 = new JLabel("Confirm Password :", JLabel.RIGHT);
   JLabel label4 = new JLabel("Remember Me!", JLabel.LEFT);
   JLabel label5 = new JLabel("Hello.", JLabel.CENTER);
   label5.setVerticalAlignment(JLabel.TOP);
   label5.setToolTipText("A tool tip with me!");
   getContentPane().add(label1);
   getContentPane().add(label2);
   getContentPane().add(label3);
   getContentPane().add(label4);
   getContentPane().add(label5);
 }
 public static void main(String[] args) {
   new Main().setVisible(true);
 }

}

 </source>
   
  
 
  



JLabel: setVerticalAlignment(int alignment)

   <source lang="java">


import java.awt.FlowLayout; import java.awt.HeadlessException; import javax.swing.JFrame; import javax.swing.JLabel; public class Main extends JFrame {

 public Main() throws HeadlessException {
   setSize(150, 300);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setLayout(new FlowLayout());
   JLabel label1 = new JLabel("Username :", JLabel.RIGHT);
   JLabel label2 = new JLabel("Password :", JLabel.RIGHT);
   JLabel label3 = new JLabel("Confirm Password :", JLabel.RIGHT);
   JLabel label4 = new JLabel("Remember Me!", JLabel.LEFT);
   JLabel label5 = new JLabel("Hello.", JLabel.CENTER);
   label5.setVerticalAlignment(JLabel.TOP);
   label5.setToolTipText("A tool tip with me!");
   getContentPane().add(label1);
   getContentPane().add(label2);
   getContentPane().add(label3);
   getContentPane().add(label4);
   getContentPane().add(label5);
 }
 public static void main(String[] args) {
   new Main().setVisible(true);
 }

}

 </source>
   
  
 
  



JLabel: setVerticalTextPosition(int align)

   <source lang="java">

import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.LineBorder; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Label Text Pos");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new GridLayout(2, 2));
   Border border = LineBorder.createGrayLineBorder();
   Icon warnIcon = MetalIconFactory.getTreeComputerIcon();
   JLabel label1 = new JLabel(warnIcon);
   label1.setText("Left-Bottom");
   label1.setHorizontalTextPosition(JLabel.LEFT);
   label1.setVerticalTextPosition(JLabel.BOTTOM);
   label1.setBorder(border);
   frame.add(label1);
   JLabel label2 = new JLabel(warnIcon);
   label2.setText("Right-TOP");
   label2.setHorizontalTextPosition(JLabel.RIGHT);
   label2.setVerticalTextPosition(JLabel.TOP);
   label2.setBorder(border);
   frame.add(label2);
   JLabel label3 = new JLabel(warnIcon);
   label3.setText("Center-Center");
   label3.setHorizontalTextPosition(JLabel.CENTER);
   label3.setVerticalTextPosition(JLabel.CENTER);
   label3.setBorder(border);
   frame.add(label3);
   JLabel label4 = new JLabel(warnIcon);
   label4.setText("Center-Bottom");
   label4.setHorizontalTextPosition(JLabel.CENTER);
   label4.setVerticalTextPosition(JLabel.BOTTOM);
   label4.setBorder(border);
   frame.add(label4);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JLabel.TOP

   <source lang="java">

import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.LineBorder; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Label Text Pos");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new GridLayout(2, 2));
   Border border = LineBorder.createGrayLineBorder();
   Icon warnIcon = MetalIconFactory.getTreeComputerIcon();
   JLabel label1 = new JLabel(warnIcon);
   label1.setText("Left-Bottom");
   label1.setHorizontalTextPosition(JLabel.LEFT);
   label1.setVerticalTextPosition(JLabel.BOTTOM);
   label1.setBorder(border);
   frame.add(label1);
   JLabel label2 = new JLabel(warnIcon);
   label2.setText("Right-TOP");
   label2.setHorizontalTextPosition(JLabel.RIGHT);
   label2.setVerticalTextPosition(JLabel.TOP);
   label2.setBorder(border);
   frame.add(label2);
   JLabel label3 = new JLabel(warnIcon);
   label3.setText("Center-Center");
   label3.setHorizontalTextPosition(JLabel.CENTER);
   label3.setVerticalTextPosition(JLabel.CENTER);
   label3.setBorder(border);
   frame.add(label3);
   JLabel label4 = new JLabel(warnIcon);
   label4.setText("Center-Bottom");
   label4.setHorizontalTextPosition(JLabel.CENTER);
   label4.setVerticalTextPosition(JLabel.BOTTOM);
   label4.setBorder(border);
   frame.add(label4);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



new JLabel(Icon icon)

   <source lang="java">

import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.LineBorder; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Label Text Pos");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLayout(new GridLayout(2, 2));
   Border border = LineBorder.createGrayLineBorder();
   Icon warnIcon = MetalIconFactory.getTreeComputerIcon();
   JLabel label1 = new JLabel(warnIcon);
   label1.setText("Left-Bottom");
   label1.setHorizontalTextPosition(JLabel.LEFT);
   label1.setVerticalTextPosition(JLabel.BOTTOM);
   label1.setBorder(border);
   frame.add(label1);
   JLabel label2 = new JLabel(warnIcon);
   label2.setText("Right-TOP");
   label2.setHorizontalTextPosition(JLabel.RIGHT);
   label2.setVerticalTextPosition(JLabel.TOP);
   label2.setBorder(border);
   frame.add(label2);
   JLabel label3 = new JLabel(warnIcon);
   label3.setText("Center-Center");
   label3.setHorizontalTextPosition(JLabel.CENTER);
   label3.setVerticalTextPosition(JLabel.CENTER);
   label3.setBorder(border);
   frame.add(label3);
   JLabel label4 = new JLabel(warnIcon);
   label4.setText("Center-Bottom");
   label4.setHorizontalTextPosition(JLabel.CENTER);
   label4.setVerticalTextPosition(JLabel.BOTTOM);
   label4.setBorder(border);
   frame.add(label4);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



new JLabel(Icon i, int alignment)

   <source lang="java">

import java.awt.ruponent; import java.awt.Graphics; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MainClass extends JPanel {

 public MainClass() {
   JLabel jl = new JLabel( new MyIcon(), JLabel.CENTER);
   add(jl);
 }
 public static void main(String[] args) {
   JFrame frame = new JFrame();
   frame.getContentPane().add(new MainClass());
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(200, 200);
   frame.setVisible(true);
 }

} class MyIcon implements Icon {

 public int getIconWidth() {
   return 32;
 }
 public int getIconHeight() {
   return 32;
 }
 public void paintIcon(Component c, Graphics g, int x, int y) {
   g.drawString("jexp.ru", 0, 20);
 }

}


 </source>
   
  
 
  



new JLabel(String htmlString) (Multi-line Label)

   <source lang="java">

import javax.swing.JFrame; import javax.swing.JLabel; public class MainClass {

 public static void main(String[] a) {
   JFrame frame = new JFrame();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   String htmlLabel = "<html>HTML Label
" + "Multi-line"; JLabel label = new JLabel(htmlLabel); frame.add(label); frame.setSize(300, 200); frame.setVisible(true); }

}


 </source>
   
  
 
  



new JLabel(String text, Icon icon, int horizontalAlignment)

   <source lang="java">

import java.awt.FlowLayout; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; public class Main {

 public static void main(String[] args) {
   JFrame.setDefaultLookAndFeelDecorated(true);
   JFrame frame = new JFrame();
   frame.setTitle("JLabel Test");
   frame.setLayout(new FlowLayout());
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   ImageIcon imageIcon = new ImageIcon("yourFile.gif");
   JLabel label = new JLabel("Mixed", imageIcon, SwingConstants.RIGHT);
   frame.add(label);
   frame.pack();
   frame.setVisible(true);
 }

}

 </source>
   
  
 
  



new JLabel(String text, int horizontalAlignment)

   <source lang="java">

import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Main {

 public static void main(String[] args) {
   JTextField firstField = new JTextField(10);
   JTextField middleField = new JTextField(10);
   JTextField lastField = new JTextField(10);
   JLabel firstLabel = new JLabel("First Name", JLabel.RIGHT);
   firstLabel.setDisplayedMnemonic("F");
   firstLabel.setLabelFor(firstField);
   JLabel middleLabel = new JLabel("Middle Initial", JLabel.RIGHT);
   middleLabel.setDisplayedMnemonic("I");
   middleLabel.setDisplayedMnemonicIndex(7);
   middleLabel.setLabelFor(middleField);
   JLabel lastLabel = new JLabel("Last Name", JLabel.RIGHT);
   lastLabel.setDisplayedMnemonic("L");
   lastLabel.setLabelFor(lastField);
   JPanel p = new JPanel();
   p.setLayout(new GridLayout(3, 2, 5, 5));
   p.add(firstLabel);
   p.add(firstField);
   p.add(middleLabel);
   p.add(middleField);
   p.add(lastLabel);
   p.add(lastField);
   JFrame f = new JFrame("MnemonicLabels");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.setContentPane(p);
   f.pack();
   f.setVisible(true);
 }

}

 </source>