Example usage for javax.swing JLabel setBounds

List of usage examples for javax.swing JLabel setBounds

Introduction

In this page you can find the example usage for javax.swing JLabel setBounds.

Prototype

public void setBounds(int x, int y, int width, int height) 

Source Link

Document

Moves and resizes this component.

Usage

From source file:NoLayoutTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("NoLayout Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);/*w w  w  .j  a  v  a2  s.  c  o  m*/
    JLabel label = new JLabel("First Name:");
    label.setBounds(20, 20, 100, 20);
    JTextField textField = new JTextField();
    textField.setBounds(124, 25, 100, 20);
    frame.add(label);
    frame.add(textField);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = (JPanel) frame.getContentPane();
    panel.setLayout(null);//from w  ww  . j a  va  2  s  .  c  o  m

    JLabel label = new JLabel("aaa");
    panel.add(label);
    Dimension size = label.getPreferredSize();
    label.setBounds(100, 100, size.width, size.height);

    frame.setSize(300, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame Main = new JFrame("Gradient Mask");
    JLabel imageLayer = new JLabel();
    JLabel maskLayer = new JLabel();
    BufferedImage image = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    BufferedImage gradientMask = new GradientImage(image.getWidth(), image.getHeight(),
            new Color[] { new Color(255, 255, 255, 125), Color.BLACK }, GradientImage.RADIAL_FROM_CENTER)
                    .getImage();/* w  w w.j ava2  s  .  c  o m*/
    Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Main.setBounds(100, 50, image.getWidth(), image.getHeight());
    imageLayer.setBounds(0, 0, Main.getWidth(), Main.getHeight());
    maskLayer.setBounds(0, 0, Main.getWidth(), Main.getHeight());
    imageLayer.setIcon(new ImageIcon((Image) image));
    maskLayer.setIcon(new ImageIcon((Image) gradientMask));
    Main.getContentPane().add(imageLayer);
    imageLayer.add(maskLayer);
    Main.setVisible(true);
}

From source file:Main.java

public static void setLabelAboveOut(JLabel label, String str, int x, int y, int width) {
    Graphics g = label.getGraphics();
    label.setText(str);/*from w  ww  . ja va2 s .  c om*/
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = x + width / 2 - strWidth / 2;
    int lY = y - g.getFont().getSize();
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
}

From source file:Main.java

public static void setLabelTop(JLabel label, String str, int width, int height) {
    Graphics g = label.getGraphics();
    label.setText(str);//from  w  w  w. j av  a2  s. co  m
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = width / 2 - strWidth / 2;
    int lY = g.getFont().getSize() / 2;
    //System.out.printf("\"label\"+%s X:%d,Y:%d\n",str,lX,lY);
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
}

From source file:Main.java

public static void setLabelCenter(JLabel label, String str, int width, int height) {
    Graphics g = label.getGraphics();
    Color c = g.getColor();//from   w ww. j av a 2s  . c  o  m
    g.setColor(Color.BLUE);
    label.setText(str);
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = width / 2 - strWidth;
    int lY = g.getFont().getSize() / 2;
    //System.out.printf("\"label\"+%s X:%d,Y:%d\n",str,lX,lY);
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
    g.setColor(c);
}

From source file:Main.java

public Main() {
    setLayout(null);//from   w w w  .  j a  va2 s  .co  m
    setSize(400, 400);

    panel1.setLayout(null);
    panel1.setBounds(0, 0, 400, 400);
    add(panel1);

    JLabel label1 = new JLabel("a label");
    label1.setBounds(15, 15, 150, 30);
    label1.setBorder(new BevelBorder(BevelBorder.RAISED));
    panel1.add(label1);

    JLabel label2 = new JLabel("a label");
    label2.setBounds(100, 100, 150, 30);
    label2.setBorder(new BevelBorder(BevelBorder.RAISED));
    panel1.add(label2);

    JLabel label3 = new JLabel("a label");
    label3.setBounds(200, 200, 150, 30);
    label3.setBorder(new BevelBorder(BevelBorder.RAISED));
    panel1.add(label3);

}

From source file:Main.java

public Main() {
    super("JLayeredPane Demo");
    setSize(256, 256);//ww  w  .  j  a va2 s.  c om

    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("largeJava2sLogo.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);
}

From source file:FramewithComponents.java

public FramewithComponents() {
    super("JLayeredPane Demo");
    setSize(256, 256);/*w w  w. j  ava2  s  .  co m*/

    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);

    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(content);
    ((JPanel) getContentPane()).setOpaque(false);

    ImageIcon earth = new ImageIcon("largeJava2sLogo.png");
    JLabel backlabel = new JLabel(earth);
    getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
    backlabel.setBounds(0, 0, earth.getIconWidth(), earth.getIconHeight());

    WindowListener l = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(l);

    setVisible(true);
}

From source file:SampleDesktop.java

protected void loadBackgroundImage() {
    ImageIcon icon = new ImageIcon("images/matterhorn.gif");
    JLabel l = new JLabel(icon);
    l.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());

    // Place the image in the lowest possible layer so nothing
    // can ever be painted under it.
    desk.add(l, new Integer(Integer.MIN_VALUE));
}