Problem with JButton in Java

0

I have put an image on my button and with setBounds I have positioned it on the screen but for some reason sometimes it goes well and sometimes the button occupies the whole screen Images   link   link

Why does this happen?

 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();
    add(f);
    setContentPane(f);
    setLayout(null);
    Botones b = new Botones();


   setContentPane(b.b1v1);
  /*  b.b1v1.setBounds(300,600,50,50);
    setContentPane(b.b1v1);*/





}
public class Juego extends JFrame{


public static void main(String[] args) throws MalformedURLException {

    Ventana v = new Ventana();




}

}

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.setLayout(null);
b1v1.addActionListener(this);
add(b1v1);
b1v1.setLayout(null);
b1v1.setBounds(300,600,50,50);


 }

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource()==b1v1) {
  try{
  Ventana v;
 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);
}
}
}
}
    
asked by ManvsMachine 20.06.2017 в 21:56
source

1 answer

1

Leave setContentPane(f); remove add(f) ; and change setContentPane(b.b1v1); for add(b.b1v1); , setContentPane is for assigning the background of the Jframe and there can only be one background but with add you can add several components to the Jframe, if you add several backgrounds they will overlap and you will only see the last one you added.

    
answered by 20.06.2017 / 22:49
source