new JLabel(Icon i, int alignment) : JLabel « javax.swing « Java by API






new JLabel(Icon i, int alignment)

 
import java.awt.Component;
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("java2s.com", 0, 20);
  }
}

           
         
  








Related examples in the same category

1.JLabel.BOTTOM
2.JLabel.CENTER
3.JLabel.LEFT
4.JLabel.RIGHT
5.JLabel.TOP
6.new JLabel(Icon icon)
7.new JLabel(String htmlString) (Multi-line Label)
8.new JLabel(String text, int horizontalAlignment)
9.new JLabel(String text, Icon icon, int horizontalAlignment)
10.JLabel: addMouseMotionListener(MouseMotionListener l)
11.JLabel: getTransferHandler()
12.JLabel: setBoder(Border border)
13.JLabel: setBounds(int x, int y, int width, int height)
14.JLabel: setDisplayedMnemonic(int key) (KeyEvent.VK_U)
15.JLabel: setDisplayedMnemonicIndex(int index)
16.JLabel: setLabelFor(Component c)
17.JLabel: setText(String str)
18.JLabel: setHorizontalTextPosition(int align)
19.JLabel: setIcon(Icon icon)
20.JLabel: setOpaque(boolean isOpaque)
21.JLabel: setPreferredSize(Dimension preferredSize)
22.JLabel: setToolTipText(String text)
23.JLabel: setVerticalAlignment(int alignment)
24.JLabel: setVerticalTextPosition(int align)
25.extends JLabel