by Dinesh Thakur Category: Swing

A button, also known as push button, is an active control that has a 3-dimensional appearance. It triggers an event when it is clicked or activated. Swing also provides an ability to associate an icon, a string, or both with the buttons. A push button is an object of JButton class.

Some of the constructors defined by JButton class are as follows.

JButton ()

JButton(Icon icon)

JButton(String string)

where,

string represents the string used for the button

icon represents the icon used for the button

Note: All the buttons are derived .from Abstract Button class

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class JLocationJavaExample extends JFrame implements ActionListener
  {
      JButton ClckBtn = new JButton("Click Here");
      int x = 0, y = 0;
      final int GP = 40;
      public JLocationJavaExample()
        {
             setTitle("Change Component Location While Clicking Button In Java Swing");
             setLayout(new FlowLayout());
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             add(ClckBtn);
             ClckBtn.addActionListener(this);
        }
             public void actionPerformed(ActionEvent e)
               {
                  ClckBtn.setLocation(x, y);
                  x += GP;
                  y += GP;
               }
                  public static void main(String[] aa)
                    {
                        JLocationJavaExample frm = new JLocationJavaExample();
                        frm.setSize(190, 170);
                        frm.setVisible(true);
                    }
  }

 Change Component Location While Clicking Button In Java Swing



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles