Hi,
ich habe mit netbeans und der gui toolbox eine oberfläche aufgebaut und
wollte im hauptfenster also das JFrame ein hintergrundbild einfügen. da
netbeans das frame automtisch erstellt hat habe ich erst mal gesucht wie
ich auf das fenster zugreifen kann. ich habe folgenden aufbau:
1 | public class Main extends javax.swing.Jframe implements PropertyChangeListener{
|
2 |
|
3 | public Main client = new Main();
|
4 | public Image img = Toolkit.getDefaultToolkit().getImage("/bilder/logo7.png");
|
5 |
|
6 | public Main()
|
7 | {
|
8 | initComponents();
|
9 | //auf folgendes reagiert er
|
10 | this.setTitle("Test");
|
11 | this.getContentPane().setBackground(Color.DARK_GRAY);
|
12 | this.setLocation(100,700);
|
13 | this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
14 | //und das hier geht nicht
|
15 | this.setIconImage(img);
|
16 | }
|
17 |
|
18 | public static void main(String args[]) throws IOException
|
19 | {
|
20 | java.awt.EventQueue.invokeLater(new Runnable()
|
21 | {
|
22 | public void run()
|
23 | {
|
24 | client.setVisible(true);
|
25 | }
|
26 | }
|
27 | }
|
28 | }
|
nach langem suchen habe ich einen weg gefunden das hintergrundbild doch
einzufügen undzwar über eine extra klasse:
1 | public class Backgroundpanel extends JPanel
|
2 | {
|
3 | Image img;
|
4 |
|
5 | public Backgroundpanel()
|
6 | {
|
7 | loadImage();
|
8 | }
|
9 |
|
10 | @Override
|
11 | public void paintComponent(Graphics g)
|
12 | {
|
13 | super.paintComponent(g);
|
14 | int width = getWidth();
|
15 | int height = getHeight();
|
16 | int imagewidth = img.getWidth(this);
|
17 | int imageheight = img.getHeight(this);
|
18 | int x = (width - imagewidth) / 2;
|
19 | int y = (height - imageheight) / 2 ;
|
20 | g.drawImage(image,x, y, this);
|
21 | }
|
22 |
|
23 | public void loadImage()
|
24 | {
|
25 | String filename = "/bilder/logo7.png";
|
26 | try{
|
27 | URL url = getClass().getResource(filename);
|
28 | img = ImageIO.read(url);
|
29 | }catch (IOException e)
|
30 | {
|
31 | System.out.println(e);
|
32 | }
|
33 | }
|
34 | }
|
und dann in der main:
1 | public Main()
|
2 | {
|
3 | initComponents();
|
4 | this.setContentPane( new Backgroundpanel);
|
5 | }
|
jetzt sehe ich das hintergrundbild zwar wenn ich das programm starte
aber meine buttons, textareas, labels etc... sind alle verdeckt und
nicht mehr erreichbar hoffe jemand weiß rat.