Thursday, September 6, 2018

JLabel in java (How we create JLabel in java Swing)

Java JLabel

The object of JLabel category could be a element for putting text in a very instrumentality. it's accustomed show one line of scan solely text. The text are often modified by AN application however a user cannot edit it directly. It inherits JComponent category.

Commonly used Methods:


MethodsDescription
String getText()t returns the text string that a label displays.
void setText(String text)It defines the one line of text this component will show.
void setHorizontalAlignment(int alignment)It sets the alignment of the label's contents on the X axis.
Icon getIcon()It returns the graphic image that the label displays.
int getHorizontalAlignment()It returns the alignment of the label's contents on the X axis.




package lable_action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Lable_Action extends JFrame implements ActionListener {


JLabel l;
JButton b;
public static void main(String[] args) {

new Lable_Action();

}


public Lable_Action()
{


l=new JLabel();
l.setBounds(50,100, 250,20);
b=new JButton("ok");
b.setBounds(50,150,95,30);
b.addActionListener(this);
add(b);

add(l);
setSize(400,400);
setLayout(null);
setVisible(true);

}

@Override
public void actionPerformed(ActionEvent e) {
String word = e.getActionCommand();
if(word.equals("ok"))
{
l.setText("Blue Hat World");
}
}
}




                                 







0 comments:

Post a Comment


Top