JButton in Java Swing: Complete Tutorial with Examples
JButton in Java Swing: Complete Tutorial with Examples
Introduction
Java Swing में JButton एक बहुत ही commonly used component है, जो graphical user interface (GUI) में button की तरह काम करता है। जब user इस button को click करता है, तो कोई specific action perform किया जाता है — जैसे data save करना, form submit करना या window close करना।
JButton, javax.swing package का हिस्सा है और यह AbstractButton class को extend करता है। Swing में हर button पूरी तरह customizable होता है, यानी आप उसका color, font, icon और text सब कुछ बदल सकते हैं।
What is JButton in Java?
JButton एक GUI component है जिसका use event-driven programming में होता है। इसका काम होता है किसी action को trigger करना जब user उस पर click करता है।
साधारण शब्दों में, JButton एक clickable area देता है जहाँ user interaction होता है और उससे कोई process या function execute किया जाता है।
Syntax of JButton
JButton button = new JButton("Click Me");
यहाँ “Click Me” वो text है जो button पर दिखाई देगा।
Constructors of JButton
JButton class के कुछ commonly used constructors नीचे दिए गए हैं:
| Constructor | Description |
|---|---|
JButton() |
एक empty button create करता है जिसमें कोई text नहीं होता। |
JButton(String text) |
Button पर दिया गया text दिखाता है। |
JButton(Icon icon) |
Button पर image या icon set करता है। |
JButton(String text, Icon icon) |
Text और Icon दोनों के साथ button बनाता है। |
Basic Example of JButton
आइए एक simple example देखें जिसमें एक button बनाया गया है और उस पर click करने से एक message दिखता है।
import javax.swing.*;
import java.awt.event.*;
public class JButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JButton Example");
JButton button = new JButton("Click Me");
button.setBounds(100,100,120,40);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(frame,"Button Clicked!");
}
});
frame.add(button);
frame.setSize(350,250);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output:
जब आप “Click Me” button को press करते हैं, एक dialog box खुलता है जिसमें लिखा होता है “Button Clicked!”.
Important Methods of JButton Class
JButton में कई useful methods होते हैं जिनकी मदद से हम button के behavior को customize कर सकते हैं।
| Method | Description |
|---|---|
setText(String text) |
Button पर नया text set करता है। |
getText() |
Button पर लिखा हुआ text return करता है। |
setIcon(Icon icon) |
Button पर icon जोड़ता है। |
setEnabled(boolean b) |
Button को enable या disable करता है। |
setToolTipText(String text) |
Mouse hover करने पर tooltip दिखाता है। |
addActionListener(ActionListener l) |
Click event के लिए listener जोड़ता है। |
JButton with ActionListener
जब user JButton पर click करता है, तो ActionListener event trigger होता है। ActionListener interface के अंदर actionPerformed() method होता है जो define करता है कि click के बाद क्या action होना चाहिए।
Example:
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button Clicked!");
}
});
यह code console में message print करेगा जब भी button पर click होगा।
Customizing JButton Appearance
JButton को हम अपनी जरूरत के अनुसार customize कर सकते हैं ताकि UI बेहतर दिखे।
Example: Change Font, Color and Background
JButton b = new JButton("Submit");
b.setBackground(Color.BLUE);
b.setForeground(Color.WHITE);
b.setFont(new Font("Arial", Font.BOLD, 16));
ऊपर का code button का background blue, text white और font bold Arial में set करता है।
Using Icons in JButton
JButton में हम images या icons भी लगा सकते हैं ताकि UI और attractive दिखे।
Example:
ImageIcon icon = new ImageIcon("save.png");
JButton saveButton = new JButton("Save", icon);
यह code button में “Save” text और “save.png” icon दोनों दिखाएगा।
Multiple Buttons in One Frame
आप एक JFrame में कई buttons add कर सकते हैं। हर button के लिए अलग ActionListener define किया जा सकता है।
Example:
JButton b1 = new JButton("OK");
JButton b2 = new JButton("Cancel");
b1.addActionListener(e -> JOptionPane.showMessageDialog(null, "OK Pressed"));
b2.addActionListener(e -> JOptionPane.showMessageDialog(null, "Cancelled"));
यह code दो अलग-अलग buttons बनाता है और दोनों के लिए अलग message दिखाता है।
JButton with Layout Managers
JButton को Frame में place करने के लिए हमें layout managers का use करना पड़ता है। Commonly used layout managers हैं — FlowLayout, BorderLayout, और GridLayout।
Example using FlowLayout:
JFrame f = new JFrame("Layout Example");
f.setLayout(new FlowLayout());
f.add(new JButton("Yes"));
f.add(new JButton("No"));
f.add(new JButton("Maybe"));
f.setSize(300,200);
f.setVisible(true);
यह code तीन buttons को एक row में FlowLayout के साथ arrange करता है।
Disable or Enable JButton
अगर आपको किसी स्थिति में button को disable करना है, तो आप setEnabled(false) का use कर सकते हैं।
JButton b = new JButton("Submit");
b.setEnabled(false); // अब यह button disable रहेगा
बाद में इसे enable करने के लिए आप b.setEnabled(true); का use कर सकते हैं।
Image Only Buttons
कई बार हमें ऐसा button चाहिए जिसमें केवल image हो, text नहीं।
Example:
JButton imgButton = new JButton(new ImageIcon("icon.png"));
imgButton.setBorder(null);
imgButton.setContentAreaFilled(false);
यह code image-only button बनाता है जो modern look देता है।
Advanced Features of JButton
JButton के कुछ advanced features इस प्रकार हैं:
- Mnemonic keys assign करना (Alt + Key से trigger)
- Default button set करना जो Enter key से active हो
- ActionCommand set करना ताकि event पहचानना आसान हो
Example with Mnemonic and ActionCommand:
JButton save = new JButton("Save");
save.setMnemonic(KeyEvent.VK_S);
save.setActionCommand("SAVE_ACTION");
अब user Alt+S दबाकर button trigger कर सकता है और event handler “SAVE_ACTION” से उसे पहचान सकता है।
Real World Use Case
JButton का सबसे ज्यादा use forms, dialog boxes, और file operations में किया जाता है — जैसे Login forms में “Submit”, “Cancel” buttons, या File Explorer में “Open”, “Save”, “Delete” जैसे buttons।
Example: Simple Login Form
import javax.swing.*;
import java.awt.event.*;
public class LoginExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Login Form");
JTextField user = new JTextField();
JPasswordField pass = new JPasswordField();
JButton login = new JButton("Login");
user.setBounds(100,50,150,30);
pass.setBounds(100,100,150,30);
login.setBounds(100,150,100,30);
login.addActionListener(e -> {
JOptionPane.showMessageDialog(frame, "Login Successful!");
});
frame.add(user); frame.add(pass); frame.add(login);
frame.setSize(400,300);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
यह एक basic GUI login example है जिसमें JButton का use login action perform करने के लिए किया गया है।
Advantages of JButton
- Simple और easy to use component।
- Completely customizable — text, color, font, icon आदि सब बदला जा सकता है।
- Event handling आसान होता है।
- All Swing Layout Managers के साथ compatible है।
Key Points to Remember
- JButton, javax.swing package का हिस्सा है।
- ActionListener के साथ use करना जरूरी है ताकि event trigger हो सके।
- Button को enable या disable किया जा सकता है।
- Text और Icon दोनों साथ में लगाए जा सकते हैं।
- Look और feel को customize किया जा सकता है।
Summary
JButton Java Swing का एक core component है जो GUI में user interaction को handle करने में मदद करता है। इसका उपयोग बहुत ही आसान है और इसके साथ event handling system पूरी तरह integrate रहता है। Exam के point of view से JButton के constructors, methods और ActionListener की working सबसे ज्यादा important होती है।