Pages

Thursday, September 8, 2016

Add some text to a label in java

how to add text in java

Today i am gonna be showing you.how to add text in java frame / panel pretty simple ...below is the source code copy them and paste it to eclips...play with it by yourself ..And you will clear i am sure
IN these code this is a whole process and in this we'll do all stuff to be excutable
 
 package myPackage;

import java.awt.Color; 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

 
public class myClass {
  
  
 public static void main(String[]args){
  
JFrame frame = new JFrame();

JPanel panel = new JPanel();

/*create object first */

JLabel label = new JLabel();
 

frame.setSize(400,600);
 
frame.setDefaultCloseOperation
          
   (JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

frame.setTitle("JLabel");

frame.add(panel);

/*add the label into the
* panel*/

panel.add(label);

/*set label text*/

label.setText("who are you?");

/*set background 
  * of label*/
label.setBackground(Color.red);

 }
 
}
 
 
and if you run so it will look like this
Mehtod for the simple JLabel is below
 public void label(){
 
 
 JLabel label = new JLabel();
 
 label.setText("this is a label");
 
 label.setBackground(Color.blue);

 panel.add(label);

 }
 
 
YOu can also create some cool looking border..Source code below
 
 public void smartLabel(){
/*create a label*/
JLabel label = new JLabel();
 
  /*set text of label*/
label.setText("this is a label");
  
  /*set border of 
    label */
Border border =
BorderFactory.createLineBorder(Color.red,2);
label.setBorder(border);

/*setBackground of label*/
label.setBackground(Color.WHITE);

/*set Size of label*/
label.setPreferredSize(new Dimension(44,10));
 }
 
}
 

No comments:

Post a Comment