Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

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");
}
}
}




                                 






Sunday, September 2, 2018

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

Java Button                                  
The JButton class is used to create a categorized button that has platform independent implementation. The utility bring about a few movement when the button is driven. It inherits                                             AbstractButton class.


Creating Button: 
JButton btn1 = new JButton("ok");
btn1.setBounds(50,100,95,30);
add(btn1); 
               

Commonly used Methods of AbstractButton class:



Methods                            Description
|============================================
|void setText(String s)                                  ||     it's miles used to set specific text on button
|==================================================================
|String getText()                                          ||    it's far used to go back the text of the button.
|==================================================================
|void setEnabled(boolean b)                          ||     it's miles used to permit or disable the button.
==================================================================
|void setIcon(Icon b)                                  ||   it is used to set the required Icon at the button.
==================================================================
| Icon getIcon()                                          ||   it's miles used to get the Icon of the button.
|==================================================================
|void setMnemonic(int a)                          ||     it's far used to set the mnemonic on the button.  
|==================================================================
|void addActionListener(ActionListener a)     ||     it is used to feature the action listener to this
                                                                                object . ===================================================================               
JButton Example:


Import javax.swing.*;

public class Button1 {

public static void main(String[] args) {

JFrame f = new JFrame(
"My First GUI Frame");
JButton btn1 = new JButton(
"ok");
btn1.setBounds(
50,100,95,30);
f.add(btn1);

f.setSize(
400, 400);
f.setLayout(
null);
f.setVisible(
true);

}

}

Output:


JButton Example with the Help of  OPP Concept Constructor                                                                
import javax.swing.*;

public class Button1 {

public static void main(String[] args) {

new Button1();
}

public Button1()
{
JFrame f = new JFrame(
"My First GUI Frame");
JButton btn1 = new JButton(
"ok");
btn1.setBounds(
50,100,95,30);
f.add(btn1);

f.setSize(
400, 400);
f.setLayout(
null);
f.setVisible(
true);
}

}


                   Output:                                             


Watch Video:

▲
Top