JButton in java | JButton क्या है? सीखें hindi में 

JButton in java | JButton क्या है?

JButton in java – जावा स्विंग में JButton एक बनी बनाई क्लास है जिस के माध्यम से हम ग्राफिकल इंटरफेस याने JFrame पर एक बटन लगाते है, और इसके अंतर्गत अलग-अलग एक्शन परफॉर्म करते है. 

जब JButton पर क्लिक होता है तब इस से कई तरह के एक्शन हम परफॉर्म कर सकते है. JButton एक JComponent की एक चाइल्ड क्लास है, इसलिए JComponent क्लास की सभी सुविधाओं का उपयोग करती है. 

जावा में JButton क्लास का उपयोग कैसे करें?

जावा में JButton बनाने के लिए हमें jawax.swing.JButton पैकेज को इम्पोर्ट करना होता है, और इस के बाद हमें JButton क्लास का एक ऑब्जेक्ट बनाना होता है. 

JButton button = new JButton ();

इस तरह से हम JButton क्लास का ऑब्जेक्ट बनाते है. एक प्रोग्रामर बटन के रूप में एक स्क्रीन पर स्ट्रिंग के रूप में या आइकॉन के रूप में JButton प्रदर्शित कर सकते है. इस के लिए JButton के कंस्ट्रक्टर में हमें स्ट्रिंग या आइकॉन इमेज पास करनी होती है. 

आपको पता है JButton एक कंपोनेंट है इसलिए इसे JFrame, JDialog, या Applet screen पर ही प्रदर्शित किया जाता है. 

JButton in java
import javax.swing.*;
import java.awt.*;

class JButtonExample extends JFrame {

JFrame frame;
JButton button;

JButtonExample(){

frame = new JFrame("JButton Example");

frame.setLayout(null);
frame.setSize(350, 350);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);


button = new JButton("Click Here");
button.setBounds(110, 150, 100, 50);
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
frame.add(button);


frame.setVisible(true);

}

public static void main (String[]args){

new JButtonExample();

}
}

उपरोक्त कोड में हमने एक GUI(JFrame) बनाया है जिसकी साइज़ 350 * 350 है , और साथ ही हमने इस पर एक सिंपल button ऐड किया है. जो फ्रेम के टॉप में सेंटर में है. button की साइज़ 100 * 20 पिक्सेल है, और बैकग्राउंड कलर ब्लैक और text कलर वाइट दिया है. निचे image में हम देख सकते है जोउपरोक्त कोड द्वारा बनाया गया है.

JButton के साथ एक्शन परफॉर्म करना (interface ActionListener)

ActionListener इंटरफेस की मदद से हम JButton पर हर तरह की एक्शन परफॉर्म करते है, जैसे हम उपरोक्त frame में मौजूद button की मदद से JFrame का कलर चेंज करेंगे.

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

class JButtonExample extends JFrame implements ActionListener{

JFrame frame;
JButton button;

JButtonExample(){

frame = new JFrame("JButton Example");

frame.setLayout(null);
frame.setSize(350, 350);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);


button = new JButton("Click Here");
button.setBounds(110, 150, 100, 50);
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
button.addActionListener(this);
frame.add(button);


frame.setVisible(true);

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==button){
frame.getContentPane().setBackground(Color.RED);

}


}


public static void main (String[]args){

new JButtonExample();

}
}

इस तरह से हम जावा में एक बटन बनाकर उसपर किसी भी तरह की एक्शन परफॉर्म कर सकते है. उपरोक्त कोड के साथ पूरा प्रोग्राम कम्पाइल और रन होते हुए आप इस विडियो में देख सकते है और सिख सकते है.

Java Runtime Environment इंस्टॉलेशन प्रक्रिया

JButton in java

Leave a Comment