Java Swing How to - Set the location of a button anywhere in JFrame








Question

We would like to know how to set the location of a button anywhere in JFrame.

Answer

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
//from   w  ww.ja va  2s .  c o m
public class Main {
  public static void main(String... args) {
    JFrame f = new JFrame();
    JButton button;

    JPanel p = new JPanel();
    button = new JButton("Button");
    p.setLayout(null);
    button.setBounds(40, 100, 100, 60);
    p.add(button);

    f.add(p);
    // setLayout(null);
    f.setDefaultCloseOperation(3);
    f.setSize(400, 400);
    f.setVisible(true);
  }
}