Java Swing
Java Swing
import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
Constructor Description
JButton() It creates a button with no text and icon.
JButton(String s) It creates a button with the specified text.
JButton(Icon i) It creates a button with the specified icon object.
Commonly used Methods of AbstractButton class
Methods Description
void setText(String s) It is used to set specified text on button
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b) It is used to set the specified Icon on the button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the button.
void It is used to add the action listener to this object.
addActionListener(ActionList
ener a)
Java Button Example
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
• Java JButton Example with ActionListener
• import java.awt.event.*;
• import javax.swing.*;
• public class ButtonExample {
• public static void main(String[] args) {
• JFrame f=new JFrame("Button Example");
• final JTextField tf=new JTextField();
• tf.setBounds(50,50, 150,20);
• JButton b=new JButton("Click Here");
• b.setBounds(50,100,95,30);
• b.addActionListener(new ActionListener(){
• public void actionPerformed(ActionEvent e){
• tf.setText("Welcome to Javatpoint.");
• }
• });
• f.add(b);f.add(tf);
• f.setSize(400,400);
• f.setLayout(null);
• f.setVisible(true);
• }
• }
import javax.swing.*;
public class ButtonExample{
ButtonExample(){
JFrame f=new JFrame("Button Example");