add Text to the JLabel
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 java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
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 no if you run this so it would look like
method for the jlabel is below
public void label(){ JLabel label = new JLabel(); label.setText("this is a label"); label.setBackground(Color.blue); panel.add(label); }
cxcafdd
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