Thursday, October 30, 2014

Java Jlabel Pro



Create JLabel with Image Icon and Text

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
public class JLabelWithIconTextExample extends JApplet
{
public void init()
{
ImageIcon icon = new ImageIcon("images/copy.gif");JLabel copyLabel = new JLabel("Copy", icon, JLabel.Center);
add(copyLabel);
   }
}

Create JLabel with Text

import javax.swing.JApplet;
import javax.swing.JLabel;
public class JLabelWithTextExample extends JApplet
{
public void init()
{
JLabel label1 = new JLabel("This is JLabel Example.");
add(label1);
   }
}

Disable JLabel

import javax.swing.JApplet;
import javax.swing.JLabel;
public class DisableJLabelExample extends JApplet
{
public void init()
{
JLabel label1 = new JLabel("JLabel Disable Example.");
add(label1);label1.setEnabled(false);
   }
}

Get Horizontal Alignment of JLabel

import javax.swing.JApplet;
import javax.swing.JLabel;
public class GetHorizontalAlignmentExample extends JApplet
{      
public void init()
{
JLabel label1 = new JLabel("JLabel Get Horizontal Alignment Example.");  add(label1);
int alignment = label1.getHorizontalAlignment();       
switch(alignment)
{          
case JLabel.Left:
label1.setText("Left");
break;
case JLabel.Center:
label1.setText("Center");
break;
case JLabel.Right:
label1.setText("Right");
break;
case JLabel.Leading:
label1.setText("Leading");
break;
case JLabel.Trailing:
label1.setText("Trailing");
break;                       
      }             
    }
}

Get JLabel's Text

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
public class GetLabelTextExample extends JApplet
{
public void init()
{
JLabel label1 = new JLabel("This is JLabel Get Text Example."); add(label1);
String text = label1.getText();
    }
}

Get X and Y Coordinates of JLabel

import javax.swing.JApplet;
import javax.swing.JLabel;
public class JLabelGetXYCoordinatesExample extends JApplet
{
public void init()
{
JLabel label1 = new JLabel("This is JLabel Example.");
add(label1);
label1.setText(label1.getX() + ", " + label1.getY());
  }
}

Get or Set Gap between JLabel's Icon and Text

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
public class JLabelGapBetweenTextAndIconExample extends JApplet
{    
public void init()
{
ImageIcon icon = new ImageIcon("images/copy.gif");
JLabel copyLabel = new JLabel("Copy", icon, JLabel.CENTER);
add(copyLabel);
int gap = copyLabel.getIconTextGap();
copyLabel.setIconTextGap(50);                         
   }       
}

No comments:

Post a Comment