Problems with setBounds

0

I'm trying to manipulate a button but even though I use setBounds and setLayout (null) the button appears on my screen where I do not specify it (centered to the top), so it's obviously ignoring the setBounds     public class Ventana extends JFrame {

public Ventana(){

    super("El laberinto");//Establece el nombre de la ventana
    setSize(700, 700);//Establece el tamaño
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Establece una operacion por deafaul al cerrar
    setLocationRelativeTo(null);//Hace que la pantalla no pueda manipularse de tamaño
    setVisible(true);
    setResizable(false);
    Fondo f = new Fondo();
    setContentPane(f);
    Botones b = new Botones();
    add(b.b1v1);


}
}


  public class Botones extends JFrame implements ActionListener{

   ImageIcon ib1v1;
   JButton b1v1;

   public Botones(){

     ib1v1 = new 
    ImageIcon(getClass().getResource("/Imagenes/siguiente.png"));
    b1v1 = new JButton(ib1v1);
    b1v1.setIcon(ib1v1);
    b1v1.setBounds(100,340,100,110);
   b1v1.setLayout(null);
   b1v1.addActionListener(this);





  }

     @Override
    public void actionPerformed(ActionEvent e) {
       if (e.getSource()==b1v1) {
  try{

  Ventana2 v2 = new Ventana2();
  v2.setVisible(true);
  v2.setSize(700,700); //Le damos tamaño al frame

  v2.setLocationRelativeTo(null);
  } catch(Exception excep) {
 System.exit(0);
   }
  }
  }
  }



 public class Juego extends JFrame{


  public static void main(String[] args) {

    Ventana v = new Ventana();



    }

   }


 public class Ventana2 extends JFrame{

    public Ventana2(){
      super("¿Listo?");//Establece el nombre de la ventana
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Establece una 
      operacion por deafaul al cerrar
       setLocationRelativeTo(null);//Hace que la pantalla no pueda 
     manipularse de tamaño
    setResizable(false);
   }


  }


    public class Fondo extends JPanel{


    public Fondo(){

    }
   @Override
  public void paintComponent (Graphics g){
  Dimension tamanio = getSize();
  ImageIcon imagenFondo = new 
  ImageIcon(getClass().getResource("/Imagenes/laberinto3.png"));
 g.drawImage(imagenFondo.getImage(),0,0,tamanio.width, tamanio.height, 
   null);
setOpaque(false);
super.paintComponent(g);
}




}
    
asked by ManvsMachine 22.06.2017 в 19:10
source

1 answer

0

setBounds is ok ... what really happens is that by not applying any layout to the contentPane of your class Ventana , by default Java through its Layout Manager will adjust the FlowLayout and the components will be seen online (side by side) without taking into account the setBounds(100,340,100,110);

Just after Fondo f = new Fondo(); add f.setLayout(null);

    
answered by 23.06.2017 в 00:39