Problème avec button et image
Salut à tous,
J'ai un petit problème : pour tester mes compétences java j'ai commencé à faire un petit jeu.
J'aimerais avoir un menu, pour cela j'ai commencé à mettre une image en "background" (qui s'affichait très bien...), puis j'est commencé à poser mes boutons et là j'est un problème : si je met mon bouton l'image en fond n'est plus là ! :/
Je voudrais savoir comment mettre les 2 en même temps
Je remercie tout ceux qui voudraient bien m'aider :D
PS: j'est pas mal cherché sur internet mais ce qui sort quand je mais "java put a button on a background" c'est "swing - How to set background color of a button in Java " donc pas ce que je cherche pareil en francais
mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package frame;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Jframe extends JFrame{ private static final long serialVersionUID = 8342554104719139670L;
//background
ImageIcon img = new ImageIcon("img/fond/fond.jpg");
public Jframe() {
this.setTitle("basicgame_framename");
this.setSize(1300, 700);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//background
this.add(new Img(img.getImage()));
//bouton et label
JPanel p = new JPanel();
JButton button;
button = new JButton("Start the game");
p.setLayout(null);
button.setBounds(getWidth()/2-(getWidth()/3)/2,getHeight()/2-33,getWidth()/3,66);
p.add(button);
getContentPane().add(p);
//updater
this.setSize(1299, 699);
this.setSize(1300, 700);
this.setVisible(true);
}
//background
public class Img extends JPanel {
private static final long serialVersionUID = -9129537057351390955L;
private Image img;
public Img(Image img) {
this.img = img;
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
} |
Et le main si ca peut vous aider :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package basicgame;
import javax.swing.JFrame;
import frame.Jframe;
public class Main {
public static void main(String[] args) {
Jframe f = new Jframe();
}
} |