Here you can find the source of createJLabel(Container c, String caption, int x, int y)
JLabel
and adding it to its container class.
Parameter | Description |
---|---|
c | the container |
caption | the caption (text) of the label |
x | x of its upper left corner |
y | y of its upper left corner |
JLabel
instance
public static JLabel createJLabel(Container c, String caption, int x, int y)
//package com.java2s; /*//from ww w .j a v a 2 s. c om This file is part of Coach Assistant Copyright (C) 2004-2006 Sina Iravanian <sina_iravanian@yahoo.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ import java.awt.Container; import java.awt.FontMetrics; import javax.swing.JLabel; public class Main { /** * This method makes it easy (and also neat) creating a <code>JLabel</code> * and adding it to its container class. * <p> * The <code>BorderLayout</code> of the container class must be set to * <code>null</code>. * <p> * This method makes the label autosized. This is done according to the * container's font. * * @param c * the container * @param caption * the caption (text) of the label * @param x * x of its upper left corner * @param y * y of its upper left corner * @return a <code>JLabel</code> instance */ public static JLabel createJLabel(Container c, String caption, int x, int y) { FontMetrics fm = c.getFontMetrics(c.getFont()); JLabel lbl = new JLabel(caption); lbl.setBounds(x, y, fm.stringWidth(caption) + 20, 20); c.add(lbl); return lbl; } }