Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Thursday, 3 August 2017

Swing Button Example

Swing – JButton tutorial and example


JButton class is used for adding platform independent buttons to a swing application. In this tutorial we will learn how to create a button in Swing application and how to tweak their appearance as per the requirement. I have also shared some code snippets that may be useful for you while developing a Swing application.


Swing JButton Example

package swings;
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 Java");         
                     }     
              }
          );     
     f.add(b);
    f.add(tf);     
    f.setSize(400,400);     
    f.setLayout(null);     
    f.setVisible(true); 
  } 
}  

2 comments:

  1. No program :( it shows only:

    package swings;

    ReplyDelete
  2. Sorry for inconvenience i will sort it ASAP

    ReplyDelete