Java Swing How to - Put object in x,y position JFrame








Question

We would like to know how to put object in x,y position JFrame.

Answer

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
/*from  w w w. j  a  va 2s .  c o m*/
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Main {

  public static void main(String[] args) {
    int specificX = 40;
    int specificY = 20;

    JPanel gui = new JPanel(new BorderLayout());
    JTextField tf = new JTextField(10);
    JPanel borderPanel = new JPanel(new GridLayout());
    borderPanel.add(tf);
    borderPanel.setBorder(new EmptyBorder(specificX, specificY, specificX,
        specificY));
    borderPanel.setBackground(Color.GREEN);
    gui.add(borderPanel);

    JOptionPane.showMessageDialog(null, gui);

  }
}