javaeclipsejframejbuttonsetbounds

buttonname.setBounds(xlocation,ylocation, width, height); doesn't work for me?


 package com.tests;

 import javax.swing.ImageIcon;
 import javax.swing.JButton;
  import javax.swing.JFrame;

 public class LevelUp {

public static void main(String[] args)  {
    // TODO Auto-generated method stub
     final int width = 1000;
     final int height = width / 16*9;

 //buttons      
 JButton button = new JButton("Yus");

 //location
 button.setBounds(0, 0, 50, 50);

   JFrame frame = new JFrame("Level");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setResizable(false);
   frame.setVisible(true);
   frame.setIconImage(new ImageIcon("Biblioteker\\Billeder\\giggle.jpeg").getImage());
   frame.setSize(width, height);
   frame.setLocationRelativeTo(null);

   //add to jframe
   frame.add(button);

} }

Why wont this work? Also I am new to java and eclipse in general, any help would be appreciated. Thanks in advance!


Solution

  • setBounds works in null layout. Add this line..

    frame.setLayout(null);