Swing kann window nicht übergeben

Jatoll

Bekanntes Mitglied
hallo, wüsste gerne wo mein fehler liegt

hier mal ein teil des codes:

Java:
public Display(int[]positionen) {
		this.setSize(new Dimension(640, 350));
		this.setTitle("Sheep-Defender");
		{
			jWindow1 = new JWindow();
			getContentPane().add(jWindow1, BorderLayout.CENTER); // HIER GIBTS PROBLEME
			jWindow1.getContentPane().setLayout(null);
			{
				restart = new JButton();
				jWindow1.getContentPane().add(restart);
				restart.setText("Restart");
				restart.setBounds(12, 6, 48, 21);
			}
			{
				as = new JLabel(new ImageIcon("schaf.png"));
				jWindow1.getContentPane().add(as);
				as.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						System.out.println("as.mouseClicked, event="+evt);
						if (ab == false){
							as = new JLabel (new ImageIcon("schaf2.png"));
							ab = true;
						}else if ( ab == true){
							as = new JLabel (new ImageIcon("schaf.png"));
							ab = false;
						}
						
					}
				});
 
so sieht die main dazu aus

Java:
public static void main(String[] args) {
	int [] a = {0,1,3,5};
	Display ab = new Display (a);
}
 
Moin,

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container

tja, wie hier schon steht :
Du versucht ein JWindow in einen Container zu legen!
Das funktioniert so sicher nicht, allenfalls mit Panels, Panes etc.

Was genau soll das mit dem JWindow denn werden?
Willst Du dies als Anzeige auf dem Bildschirm haben ???:L

Gruß
Klaus

EDIT
Top-Level-Container
 
Zuletzt bearbeitet:
Das JWindow ist wie der JFrame nicht dafür gedacht in der Contentpane von einem anderen Element platziert zu werden:
[c]A Window object is a top-level window with no borders and no menubar[/c]
 
ich hab das teilweise mit dem jigloo editor gemacht und weiß gar nicht wieso da jetzt sowas steht... ich will einfach nur mein spiel angezeigt bekommen ^^

sorry bin noch etwas unerfahren was gui angeht
 
Was soll das JWindow denn darstellen? Soll es in eigenständiges Fenster sein? Wenn es nur ein Element in einem bereits bestehenden Fenster ist kannst du ein JPanel nehmen.
 
also in der klasse display wird die ausgabe des spiels definiert wenn ich das mal so sagen darf ... also hier soll ein fenster erzeugt werden auf dem man dann die figuren sieht und anklicken kann ...
was genau du jetzt meinst weiß ich nicht darum erklär ichs mal so... das spiel bzw die oberfläche besteht auch nur aus diesem fenster und sonst nix
 
Und was willst du dann mit dem JWindow? Zeig doch mal deinen ganzen Code. Es reicht wenn z.B. deine Spielklasse von JFrame erbt. Dann kannst du einfach auf die ContentPane deine Elemente platzieren.
 
das is der code soweit ich ihn bis jetzt habe.. hab versucht mich in dem wirrwarr zurecht zu finden aber kriegs nicht hin ... 🙁

Java:
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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

import com.cloudgarden.resource.SWTResourceManager;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit [url=http://www.cloudgarden.com]Cloud Garden (Java Resources)[/url] for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Display extends JFrame {
	private JWindow jWindow1;
	private JButton restart;
	private JLabel as;
	private boolean ab;
	private JLabel bs;
	private boolean bb;
	private JLabel cs;
	private boolean cb;
	
	private JLabel fw;
	
	private JLabel feld;
	public Display(int[]positionen) {
		this.setSize(new Dimension(640, 350));
		this.setTitle("Sheep-Defender");
		{
			jWindow1 = new JWindow();
			//getContentPane().add(jWindow1, BorderLayout.CENTER);
			this.add(jWindow1);
			jWindow1.getContentPane().add(this);
			{
				restart = new JButton();
				jWindow1.getContentPane().add(restart);
				restart.setText("Restart");
				restart.setBounds(12, 6, 48, 21);
			}
			{
				as = new JLabel(new ImageIcon("schaf.png"));
				jWindow1.getContentPane().add(as);
				as.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						System.out.println("as.mouseClicked, event="+evt);
						if (ab == false){
							as = new JLabel (new ImageIcon("schaf2.png"));
							ab = true;
						}else if ( ab == true){
							as = new JLabel (new ImageIcon("schaf.png"));
							ab = false;
						}
						
					}
				});
				if (positionen[0]==0){
					as.setBounds(39, 143, 75, 64);
				}else if (positionen[0]==1){
					as.setBounds(147, 31, 75, 64);
				}else if (positionen[0]==4){
					as.setBounds(277, 28, 75, 64);
				}else if (positionen[0]==7){
					as.setBounds(413, 34, 75, 64);
				}else if (positionen[0]==2){
					as.setBounds(147, 148, 75, 64);
				}else if (positionen[0]==5){
					as.setBounds(276, 147, 75, 64);
				}else if (positionen[0]==8){
					as.setBounds(413, 148, 75, 64);
				}else if (positionen[0]==10){
					as.setBounds(530, 148, 75, 64);
				}else if (positionen[0]==3){
					as.setBounds(148, 267, 75, 64);
				}else if (positionen[0]==6){
					as.setBounds(283, 267, 75, 64);
				}else if (positionen[0]==9){
					as.setBounds(413, 267, 75, 64);
				}
			}
			{
				bs = new JLabel(new ImageIcon("schaf.png"));
				jWindow1.getContentPane().add(bs);
				if (positionen[1]==0){
					bs.setBounds(39, 143, 75, 64);
				}else if (positionen[1]==1){
					bs.setBounds(147, 31, 75, 64);
				}else if (positionen[1]==4){
					bs.setBounds(277, 28, 75, 64);
				}else if (positionen[1]==7){
					bs.setBounds(413, 34, 75, 64);
				}else if (positionen[1]==2){
					bs.setBounds(147, 148, 75, 64);
				}else if (positionen[1]==5){
					bs.setBounds(276, 147, 75, 64);
				}else if (positionen[1]==8){
					bs.setBounds(413, 148, 75, 64);
				}else if (positionen[1]==10){
					bs.setBounds(530, 148, 75, 64);
				}else if (positionen[1]==3){
					bs.setBounds(148, 267, 75, 64);
				}else if (positionen[1]==6){
					bs.setBounds(283, 267, 75, 64);
				}else if (positionen[1]==9){
					bs.setBounds(413, 267, 75, 64);
				}
			}
			{
				cs = new JLabel(new ImageIcon("schaf.png"));
				jWindow1.getContentPane().add(cs);
				if (positionen[2]==0){
					cs.setBounds(39, 143, 75, 64);
				}else if (positionen[2]==1){
					cs.setBounds(147, 31, 75, 64);
				}else if (positionen[2]==4){
					cs.setBounds(277, 28, 75, 64);
				}else if (positionen[2]==7){
					cs.setBounds(413, 34, 75, 64);
				}else if (positionen[2]==2){
					cs.setBounds(147, 148, 75, 64);
				}else if (positionen[2]==5){
					cs.setBounds(276, 147, 75, 64);
				}else if (positionen[2]==8){
					cs.setBounds(413, 148, 75, 64);
				}else if (positionen[2]==10){
					cs.setBounds(530, 148, 75, 64);
				}else if (positionen[2]==3){
					cs.setBounds(148, 267, 75, 64);
				}else if (positionen[2]==6){
					cs.setBounds(283, 267, 75, 64);
				}else if (positionen[2]==9){
					cs.setBounds(413, 267, 75, 64);
				}
			}
			{
				fw = new JLabel(new ImageIcon("wolf.png"));
				jWindow1.getContentPane().add(fw);
				if (positionen[3]==0){
					fw.setBounds(42, 119, 75, 64);
				}else if (positionen[3]==1){
					fw.setBounds(150, 7, 75, 64);
				}else if (positionen[3]==4){
					fw.setBounds(280, 4, 75, 64);
				}else if (positionen[3]==7){
					fw.setBounds(416, 10, 75, 64);
				}else if (positionen[3]==2){
					fw.setBounds(150, 124, 75, 64);
				}else if (positionen[3]==5){
					fw.setBounds(279, 123, 75, 64);
				}else if (positionen[3]==8){
					fw.setBounds(416, 124, 75, 64);
				}else if (positionen[3]==10){
					fw.setBounds(533, 124, 75, 64);
				}else if (positionen[3]==3){
					fw.setBounds(151, 243, 75, 64);
				}else if (positionen[3]==6){
					fw.setBounds(286, 243, 75, 64);
				}else if (positionen[3]==9){
					fw.setBounds(416, 243, 75, 64);
				}
			}
			{
				feld = new JLabel(new ImageIcon("feld2.png"));
				jWindow1.getContentPane().add(feld);
				feld.setBounds(0, 0, 640, 350);
			
			}

		}
	}
	
   public static void main(String[] args) {
	int [] a = {0,1,3,5};
	Display ab = new Display (a);
   }
}
 
Naja, dann wäre ein JPanel schon nicht verkehrt:
Java:
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
 
//import com.cloudgarden.resource.SWTResourceManager;
 
 
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit [url=http://www.cloudgarden.com]Cloud Garden (Java Resources)[/url] for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Display extends JFrame {
    private JPanel jpanel1;
    private JButton restart;
    private JLabel as;
    private boolean ab;
    private JLabel bs;
    private boolean bb;
    private JLabel cs;
    private boolean cb;
    
    private JLabel fw;
    
    private JLabel feld;
    public Display(int[]positionen) {
        this.setSize(new Dimension(640, 350));
        this.setTitle("Sheep-Defender");
        {
            jpanel1 = new JPanel();
            this.getContentPane().add(jpanel1, BorderLayout.CENTER);
            this.add(jpanel1);
            
            {
                restart = new JButton();
                jpanel1.add(restart);
                restart.setText("Restart");
                restart.setBounds(12, 6, 48, 21);
            }
            {
                as = new JLabel(new ImageIcon("schaf.png"));
                jpanel1.add(as);
                as.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent evt) {
                        System.out.println("as.mouseClicked, event="+evt);
                        if (ab == false){
                            as = new JLabel (new ImageIcon("schaf2.png"));
                            ab = true;
                        }else if ( ab == true){
                            as = new JLabel (new ImageIcon("schaf.png"));
                            ab = false;
                        }
                        
                    }
                });
                if (positionen[0]==0){
                    as.setBounds(39, 143, 75, 64);
                }else if (positionen[0]==1){
                    as.setBounds(147, 31, 75, 64);
                }else if (positionen[0]==4){
                    as.setBounds(277, 28, 75, 64);
                }else if (positionen[0]==7){
                    as.setBounds(413, 34, 75, 64);
                }else if (positionen[0]==2){
                    as.setBounds(147, 148, 75, 64);
                }else if (positionen[0]==5){
                    as.setBounds(276, 147, 75, 64);
                }else if (positionen[0]==8){
                    as.setBounds(413, 148, 75, 64);
                }else if (positionen[0]==10){
                    as.setBounds(530, 148, 75, 64);
                }else if (positionen[0]==3){
                    as.setBounds(148, 267, 75, 64);
                }else if (positionen[0]==6){
                    as.setBounds(283, 267, 75, 64);
                }else if (positionen[0]==9){
                    as.setBounds(413, 267, 75, 64);
                }
            }
            {
                bs = new JLabel(new ImageIcon("schaf.png"));
                jpanel1.add(bs);
                if (positionen[1]==0){
                    bs.setBounds(39, 143, 75, 64);
                }else if (positionen[1]==1){
                    bs.setBounds(147, 31, 75, 64);
                }else if (positionen[1]==4){
                    bs.setBounds(277, 28, 75, 64);
                }else if (positionen[1]==7){
                    bs.setBounds(413, 34, 75, 64);
                }else if (positionen[1]==2){
                    bs.setBounds(147, 148, 75, 64);
                }else if (positionen[1]==5){
                    bs.setBounds(276, 147, 75, 64);
                }else if (positionen[1]==8){
                    bs.setBounds(413, 148, 75, 64);
                }else if (positionen[1]==10){
                    bs.setBounds(530, 148, 75, 64);
                }else if (positionen[1]==3){
                    bs.setBounds(148, 267, 75, 64);
                }else if (positionen[1]==6){
                    bs.setBounds(283, 267, 75, 64);
                }else if (positionen[1]==9){
                    bs.setBounds(413, 267, 75, 64);
                }
            }
            {
                cs = new JLabel(new ImageIcon("schaf.png"));
                jpanel1.add(cs);
                if (positionen[2]==0){
                    cs.setBounds(39, 143, 75, 64);
                }else if (positionen[2]==1){
                    cs.setBounds(147, 31, 75, 64);
                }else if (positionen[2]==4){
                    cs.setBounds(277, 28, 75, 64);
                }else if (positionen[2]==7){
                    cs.setBounds(413, 34, 75, 64);
                }else if (positionen[2]==2){
                    cs.setBounds(147, 148, 75, 64);
                }else if (positionen[2]==5){
                    cs.setBounds(276, 147, 75, 64);
                }else if (positionen[2]==8){
                    cs.setBounds(413, 148, 75, 64);
                }else if (positionen[2]==10){
                    cs.setBounds(530, 148, 75, 64);
                }else if (positionen[2]==3){
                    cs.setBounds(148, 267, 75, 64);
                }else if (positionen[2]==6){
                    cs.setBounds(283, 267, 75, 64);
                }else if (positionen[2]==9){
                    cs.setBounds(413, 267, 75, 64);
                }
            }
            {
                fw = new JLabel(new ImageIcon("wolf.png"));
                jpanel1.add(fw);
                if (positionen[3]==0){
                    fw.setBounds(42, 119, 75, 64);
                }else if (positionen[3]==1){
                    fw.setBounds(150, 7, 75, 64);
                }else if (positionen[3]==4){
                    fw.setBounds(280, 4, 75, 64);
                }else if (positionen[3]==7){
                    fw.setBounds(416, 10, 75, 64);
                }else if (positionen[3]==2){
                    fw.setBounds(150, 124, 75, 64);
                }else if (positionen[3]==5){
                    fw.setBounds(279, 123, 75, 64);
                }else if (positionen[3]==8){
                    fw.setBounds(416, 124, 75, 64);
                }else if (positionen[3]==10){
                    fw.setBounds(533, 124, 75, 64);
                }else if (positionen[3]==3){
                    fw.setBounds(151, 243, 75, 64);
                }else if (positionen[3]==6){
                    fw.setBounds(286, 243, 75, 64);
                }else if (positionen[3]==9){
                    fw.setBounds(416, 243, 75, 64);
                }
            }
            {
                feld = new JLabel(new ImageIcon("feld2.png"));
                jpanel1.add(feld);
                feld.setBounds(0, 0, 640, 350);
            
            }
 
        }
    }
    
   public static void main(String[] args) {
    int [] a = {0,1,3,5};
    Display ab = new Display (a);
    ab.pack();
    ab.setVisible(true);
   }
}

Da musst du jetzt aber noch mit dem LayoutManager die Elemente ordentlich positionieren.
 
ok dann war aber das fesnter irgendwie extrem klein also so als wäre kein inhalt da.... habs dann zu dem hier umgeändert aber immer noch weiß, die grafiken sind weg:

Java:
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
 
//import com.cloudgarden.resource.SWTResourceManager;
 
 
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit [url=http://www.cloudgarden.com]Cloud Garden (Java Resources)[/url] for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Display extends JFrame {
    private JPanel jpanel1;
    private JButton restart;
    private JLabel as;
    private boolean ab;
    private JLabel bs;
    private boolean bb;
    private JLabel cs;
    private boolean cb;
    
    private JLabel fw;
    
    private JLabel feld;
    public Display(int[]positionen) {
        this.setSize(new Dimension(640, 350));
        this.setTitle("Sheep-Defender");
        {
            jpanel1 = new JPanel();
            this.getContentPane().add(jpanel1, BorderLayout.CENTER);
            this.add(jpanel1);
            jpanel1.setLayout(null);
            jpanel1.setBounds(0, 0, 640, 350);
            jpanel1.setSize(640, 350);
            jpanel1.setPreferredSize(new java.awt.Dimension(640, 350));

            {
                restart = new JButton();
                jpanel1.add(restart);
                restart.setText("Restart");
                restart.setBounds(8, 6, 48, 21);
            }
            {
                as = new JLabel(new ImageIcon("schaf.png"));
                jpanel1.add(as);
                as.setBounds(179, 18, 78, 55);
                as.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent evt) {
                        System.out.println("as.mouseClicked, event="+evt);
                        if (ab == false){
                            as = new JLabel (new ImageIcon("schaf2.png"));
                            ab = true;
                        }else if ( ab == true){
                            as = new JLabel (new ImageIcon("schaf.png"));
                            ab = false;
                        }
                        
                    }
                });
                if (positionen[0]==0){
                    as.setBounds(39, 143, 75, 64);
                }else if (positionen[0]==1){
                    as.setBounds(147, 31, 75, 64);
                }else if (positionen[0]==4){
                    as.setBounds(277, 28, 75, 64);
                }else if (positionen[0]==7){
                    as.setBounds(413, 34, 75, 64);
                }else if (positionen[0]==2){
                    as.setBounds(147, 148, 75, 64);
                }else if (positionen[0]==5){
                    as.setBounds(276, 147, 75, 64);
                }else if (positionen[0]==8){
                    as.setBounds(413, 148, 75, 64);
                }else if (positionen[0]==10){
                    as.setBounds(530, 148, 75, 64);
                }else if (positionen[0]==3){
                    as.setBounds(148, 267, 75, 64);
                }else if (positionen[0]==6){
                    as.setBounds(283, 267, 75, 64);
                }else if (positionen[0]==9){
                    as.setBounds(413, 267, 75, 64);
                }
            }
            {
                bs = new JLabel(new ImageIcon("schaf.png"));
                jpanel1.add(bs);
                bs.setBounds(262, 18, 78, 55);
                if (positionen[1]==0){
                    bs.setBounds(39, 143, 75, 64);
                }else if (positionen[1]==1){
                    bs.setBounds(147, 31, 75, 64);
                }else if (positionen[1]==4){
                    bs.setBounds(277, 28, 75, 64);
                }else if (positionen[1]==7){
                    bs.setBounds(413, 34, 75, 64);
                }else if (positionen[1]==2){
                    bs.setBounds(147, 148, 75, 64);
                }else if (positionen[1]==5){
                    bs.setBounds(276, 147, 75, 64);
                }else if (positionen[1]==8){
                    bs.setBounds(413, 148, 75, 64);
                }else if (positionen[1]==10){
                    bs.setBounds(530, 148, 75, 64);
                }else if (positionen[1]==3){
                    bs.setBounds(148, 267, 75, 64);
                }else if (positionen[1]==6){
                    bs.setBounds(283, 267, 75, 64);
                }else if (positionen[1]==9){
                    bs.setBounds(413, 267, 75, 64);
                }
            }
            {
                cs = new JLabel(new ImageIcon("schaf.png"));
                jpanel1.add(cs);
                cs.setBounds(345, 18, 78, 55);
                if (positionen[2]==0){
                    cs.setBounds(39, 143, 75, 64);
                }else if (positionen[2]==1){
                    cs.setBounds(147, 31, 75, 64);
                }else if (positionen[2]==4){
                    cs.setBounds(277, 28, 75, 64);
                }else if (positionen[2]==7){
                    cs.setBounds(413, 34, 75, 64);
                }else if (positionen[2]==2){
                    cs.setBounds(147, 148, 75, 64);
                }else if (positionen[2]==5){
                    cs.setBounds(276, 147, 75, 64);
                }else if (positionen[2]==8){
                    cs.setBounds(413, 148, 75, 64);
                }else if (positionen[2]==10){
                    cs.setBounds(530, 148, 75, 64);
                }else if (positionen[2]==3){
                    cs.setBounds(148, 267, 75, 64);
                }else if (positionen[2]==6){
                    cs.setBounds(283, 267, 75, 64);
                }else if (positionen[2]==9){
                    cs.setBounds(413, 267, 75, 64);
                }
            }
            {
                fw = new JLabel(new ImageIcon("wolf.png"));
                jpanel1.add(fw);
                fw.setBounds(428, 5, 78, 82);
                if (positionen[3]==0){
                    fw.setBounds(42, 119, 75, 64);
                }else if (positionen[3]==1){
                    fw.setBounds(150, 7, 75, 64);
                }else if (positionen[3]==4){
                    fw.setBounds(280, 4, 75, 64);
                }else if (positionen[3]==7){
                    fw.setBounds(416, 10, 75, 64);
                }else if (positionen[3]==2){
                    fw.setBounds(150, 124, 75, 64);
                }else if (positionen[3]==5){
                    fw.setBounds(279, 123, 75, 64);
                }else if (positionen[3]==8){
                    fw.setBounds(416, 124, 75, 64);
                }else if (positionen[3]==10){
                    fw.setBounds(533, 124, 75, 64);
                }else if (positionen[3]==3){
                    fw.setBounds(151, 243, 75, 64);
                }else if (positionen[3]==6){
                    fw.setBounds(286, 243, 75, 64);
                }else if (positionen[3]==9){
                    fw.setBounds(416, 243, 75, 64);
                }
            }
            {
                feld = new JLabel(new ImageIcon("feld2.png"));
                jpanel1.add(feld);
                feld.setBounds(-4, -1, 640, 350);
                feld.setPreferredSize(new java.awt.Dimension(640, 350));

            }
 
        }
    }
    
   public static void main(String[] args) {
    int [] a = {0,1,3,5};
    Display ab = new Display (a);
    ab.pack();
    ab.setVisible(true);
   }
}
 

Zurück
Oben