Jpanel bleibt leer

zipfelQeh

Grünschnabel
Hallo,
zu aller erst : Ich bin kein Profi.
Habe mit Eclipse und dem Plugin WindowBuilder (bitte nicht schimpfen) ein Design gebaut.
Das wird mir auch Prima in diesen Testfenster angezeigt, aber wenn ich das Applet compiliere zeigt er mir das Jpanel komplett leer an.

Hier der code: (Viele Imports sind überflüssig)
Java:
package HSFulda;

import java.applet.*;
import java.awt.*;
import javax.swing.JApplet;
import net.sourceforge.jFuzzyLogic.FIS;
import javax.swing.JPanel;

import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Image;

import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Insets;

import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;
import javax.swing.DebugGraphics;

public class Helikoptergui extends JApplet{


	/**
	 * 
	 */

	static JSlider WindvonLinks = new JSlider();
	static JSlider WindvonRechts = new JSlider();
	static JSlider HelikopterHoehe = new JSlider();
	static JPanel Feld = new JPanel();

	public void init(){
    	setSize(800,600);
    	GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{33, 84, 32, 500, 33, 98, 20, 0};
		gridBagLayout.rowHeights = new int[]{24, 15, 27, 450, 48, 18, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblHelikopter = new JLabel(" Helikopter Landung");
		lblHelikopter.setFont(new Font("Tahoma", Font.PLAIN, 23));
		GridBagConstraints gbc_lblHelikopter = new GridBagConstraints();
		gbc_lblHelikopter.anchor = GridBagConstraints.WEST;
		gbc_lblHelikopter.insets = new Insets(0, 0, 5, 5);
		gbc_lblHelikopter.gridx = 3;
		gbc_lblHelikopter.gridy = 0;
		getContentPane().add(lblHelikopter, gbc_lblHelikopter);
		
		JLabel lblBylabel = new JLabel("by me");
		GridBagConstraints gbc_lblBylabel = new GridBagConstraints();
		gbc_lblBylabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblBylabel.gridx = 3;
		gbc_lblBylabel.gridy = 1;
		getContentPane().add(lblBylabel, gbc_lblBylabel);
		
		JLabel lblWindVonLinks = new JLabel("Wind von Links");
		GridBagConstraints gbc_lblWindVonLinks = new GridBagConstraints();
		gbc_lblWindVonLinks.insets = new Insets(0, 0, 5, 5);
		gbc_lblWindVonLinks.gridx = 1;
		gbc_lblWindVonLinks.gridy = 2;
		getContentPane().add(lblWindVonLinks, gbc_lblWindVonLinks);
		
		JLabel lblWindVonRechts = new JLabel("Wind von Rechts");
		GridBagConstraints gbc_lblWindVonRechts = new GridBagConstraints();
		gbc_lblWindVonRechts.insets = new Insets(0, 0, 5, 5);
		gbc_lblWindVonRechts.gridx = 5;
		gbc_lblWindVonRechts.gridy = 2;
		getContentPane().add(lblWindVonRechts, gbc_lblWindVonRechts);
		WindvonLinks.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				checkWind(false);
			}
		});
		
		WindvonLinks.setMajorTickSpacing(10);
		WindvonLinks.setToolTipText("Wind kann nur von einer Richtung kommen");
		WindvonLinks.setPaintLabels(true);
		WindvonLinks.setPaintTicks(true);
		WindvonLinks.setValue(100);
		WindvonLinks.setMaximum(200);
	
		WindvonLinks.setSnapToTicks(true);
		WindvonLinks.setOrientation(SwingConstants.VERTICAL);
		GridBagConstraints gbc_WindvonLinks = new GridBagConstraints();
		gbc_WindvonLinks.fill = GridBagConstraints.BOTH;
		gbc_WindvonLinks.insets = new Insets(0, 0, 5, 5);
		gbc_WindvonLinks.gridx = 1;
		gbc_WindvonLinks.gridy = 3;
		getContentPane().add(WindvonLinks, gbc_WindvonLinks);
		
		WindvonRechts.setValue(0);
		WindvonRechts.setToolTipText("Wind kann nur von einer Richtung kommen");
		WindvonRechts.setMajorTickSpacing(10);
		WindvonRechts.setMaximum(200);
		WindvonRechts.setOrientation(SwingConstants.VERTICAL);
		WindvonRechts.setSnapToTicks(true);
		WindvonRechts.setPaintTicks(true);
		WindvonRechts.setPaintLabels(true);
		GridBagConstraints gbc_WindvonRechts = new GridBagConstraints();
		gbc_WindvonRechts.fill = GridBagConstraints.BOTH;
		gbc_WindvonRechts.insets = new Insets(0, 0, 5, 5);
		gbc_WindvonRechts.gridx = 5;
		gbc_WindvonRechts.gridy = 3;
		getContentPane().add(WindvonRechts, gbc_WindvonRechts);
		WindvonRechts.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				checkWind(true);
			}
		});
		
		HelikopterHoehe.setValue(450);
		HelikopterHoehe.setMajorTickSpacing(50);
		HelikopterHoehe.setMaximum(450);
		HelikopterHoehe.setSnapToTicks(true);
		HelikopterHoehe.setPaintTicks(true);
		HelikopterHoehe.setPaintLabels(true);
		GridBagConstraints gbc_HelikopterHoehe = new GridBagConstraints();
		gbc_HelikopterHoehe.fill = GridBagConstraints.BOTH;
		gbc_HelikopterHoehe.insets = new Insets(0, 0, 5, 5);
		gbc_HelikopterHoehe.gridx = 3;
		gbc_HelikopterHoehe.gridy = 4;
		getContentPane().add(HelikopterHoehe, gbc_HelikopterHoehe);
		
		JButton btnStart = new JButton("START!");
		GridBagConstraints gbc_btnStart = new GridBagConstraints();
		gbc_btnStart.insets = new Insets(0, 0, 5, 5);
		gbc_btnStart.gridx = 5;
		gbc_btnStart.gridy = 4;
		getContentPane().add(btnStart, gbc_btnStart);
		
		JLabel lblHelikopterHoehe = new JLabel("Helikopter Hoehe");
		GridBagConstraints gbc_lblHelikopterHoehe = new GridBagConstraints();
		gbc_lblHelikopterHoehe.insets = new Insets(0, 0, 0, 5);
		gbc_lblHelikopterHoehe.gridx = 3;
		gbc_lblHelikopterHoehe.gridy = 5;
		getContentPane().add(lblHelikopterHoehe, gbc_lblHelikopterHoehe);

		GridBagConstraints gbc_Feld = new GridBagConstraints();
		gbc_Feld.fill = GridBagConstraints.BOTH;
		gbc_Feld.insets = new Insets(0, 0, 5, 5);
		gbc_Feld.gridx = 3;
		gbc_Feld.gridy = 3;		
		
		JPanel Feld = new JPanel();
		Feld.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		Feld.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		Feld.setLayout(null);
		Feld.setBackground(new Color(153, 255, 255));
		getContentPane().add(Feld, gbc_Feld);
		
		JLabel Helikopter = new JLabel("");
		Helikopter.setSize(111, 46);
		Helikopter.setLocation(183, 0);
		Helikopter.setIcon(new ImageIcon("PFAD ZUM BILD"));
		Feld.add(Helikopter);

		JLabel bg = new JLabel("");
		bg.setIcon(new ImageIcon("PFAD ZUM BILD"));
		bg.setBounds(0, 346, 492, 100);
		Feld.add(bg);
		

	}
	
	public void checkWind(boolean i){
		if(i){WindvonLinks.setValue(0);}
		else{WindvonRechts.setValue(0);	}

	}
	public void paint(Graphics g){

	}
	
}

Ich bin mir sicher, dass jemand, der sich mit Applets auskennt sofort den Fehler sieht. Aber mir bleibt das Licht der Erkenntnis seit 2 Tagen aus.

lG
 
Hi und willkommen im Forum!
zuerst: wenn du paint() aufrufst, solltest du darin super.paint() aufrufen. Außerdem solltest du die Methode paint() in Swing (wo JPanel, JApplet, ... dazugehören) nicht benutzen und nicht überschreiben.
Treten außerdem noch irgend welche Fehlermeldungen auf? (Exceptions?)

(PS: dein Code-Posting und die Fehlerbeschreibung sind wirklich hilfreich!)
 
Hallo,
erstmal danke für die Antwort und für das P.S. :)
Habe alles ausprobiert. Mit oder ohne paint() und bestimmt 10 mal. Alles soll am Ende ein JApplet werden.
Keine Exception wird geworfen. Ich bekomme einfach keinen Inhalt in mein Jpanel.
Ich benutze auf das gesamte Layout GridBagLayout und im Panel das Null Layout um meine Objekte frei zu bewegen.
Kann es sein, dass mein Jpanel von irgendetwas überschrieben wird, oder das irgendetwas sich auf mein Panel legt? Habe Panel und Jpanel probiert bei beiden derselbe weise Fleck. Noch nicht einmal die Hintergrundfarbe wird gesetzt.
Nur noch mal verständlicher. So sollte es aussehen :

Ich komme seit einigen Tagen jetzt schon nicht mehr weiter und würde ungern aufgeben :)

Hier nochmal ein bisschen abgewandelter Code, wo aber dasselbe Problem auftritt:
Java:
package HSFulda;

import java.applet.*;
import java.awt.*;
import javax.swing.JApplet;
import net.sourceforge.jFuzzyLogic.FIS;
import javax.swing.JPanel;

import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Image;

import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;
import javax.swing.DebugGraphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class Helikoptergui extends JApplet{
	
public Helikoptergui() {
	}


	/**
	 * 
	 */
	static JPanel Feld = new JPanel();
	static JSlider WindvonLinks = new JSlider();
	static JSlider WindvonRechts = new JSlider();
	static JSlider HelikopterHoehe = new JSlider();
	static JLabel Helikopter = new JLabel("");
	static GridBagConstraints gbc_Feld = new GridBagConstraints();



	public void init(){
		setSize(800,600);
    	GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{33, 84, 32, 500, 33, 98, 20, 0};
		gridBagLayout.rowHeights = new int[]{24, 15, 27, 450, 48, 18, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblHelikopter = new JLabel(" Helikopter Landung");
		lblHelikopter.setFont(new Font("Tahoma", Font.PLAIN, 23));
		GridBagConstraints gbc_lblHelikopter = new GridBagConstraints();
		gbc_lblHelikopter.anchor = GridBagConstraints.WEST;
		gbc_lblHelikopter.insets = new Insets(0, 0, 5, 5);
		gbc_lblHelikopter.gridx = 3;
		gbc_lblHelikopter.gridy = 0;
		getContentPane().add(lblHelikopter, gbc_lblHelikopter);
		
		
		
		JLabel lblWindVonLinks = new JLabel("Wind von Links");
		GridBagConstraints gbc_lblWindVonLinks = new GridBagConstraints();
		gbc_lblWindVonLinks.insets = new Insets(0, 0, 5, 5);
		gbc_lblWindVonLinks.gridx = 1;
		gbc_lblWindVonLinks.gridy = 2;
		getContentPane().add(lblWindVonLinks, gbc_lblWindVonLinks);
		
		JLabel lblWindVonRechts = new JLabel("Wind von Rechts");
		GridBagConstraints gbc_lblWindVonRechts = new GridBagConstraints();
		gbc_lblWindVonRechts.insets = new Insets(0, 0, 5, 5);
		gbc_lblWindVonRechts.gridx = 5;
		gbc_lblWindVonRechts.gridy = 2;
		getContentPane().add(lblWindVonRechts, gbc_lblWindVonRechts);
		WindvonLinks.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				checkWind(false);
			}
		});
		
		WindvonLinks.setMajorTickSpacing(10);
		WindvonLinks.setToolTipText("Wind kann nur von einer Richtung kommen");
		WindvonLinks.setPaintLabels(true);
		WindvonLinks.setPaintTicks(true);
		WindvonLinks.setValue(100);
		WindvonLinks.setMaximum(200);
	
		WindvonLinks.setSnapToTicks(true);
		WindvonLinks.setOrientation(SwingConstants.VERTICAL);
		GridBagConstraints gbc_WindvonLinks = new GridBagConstraints();
		gbc_WindvonLinks.fill = GridBagConstraints.BOTH;
		gbc_WindvonLinks.insets = new Insets(0, 0, 5, 5);
		gbc_WindvonLinks.gridx = 1;
		gbc_WindvonLinks.gridy = 3;
		getContentPane().add(WindvonLinks, gbc_WindvonLinks);
		
		WindvonRechts.setValue(0);
		WindvonRechts.setToolTipText("Wind kann nur von einer Richtung kommen");
		WindvonRechts.setMajorTickSpacing(10);
		WindvonRechts.setMaximum(200);
		WindvonRechts.setOrientation(SwingConstants.VERTICAL);
		WindvonRechts.setSnapToTicks(true);
		WindvonRechts.setPaintTicks(true);
		WindvonRechts.setPaintLabels(true);
		GridBagConstraints gbc_WindvonRechts = new GridBagConstraints();
		gbc_WindvonRechts.fill = GridBagConstraints.BOTH;
		gbc_WindvonRechts.insets = new Insets(0, 0, 5, 5);
		gbc_WindvonRechts.gridx = 5;
		gbc_WindvonRechts.gridy = 3;
		getContentPane().add(WindvonRechts, gbc_WindvonRechts);
		WindvonRechts.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				checkWind(true);
			}
		});
		
		HelikopterHoehe.setValue(450);
		HelikopterHoehe.setMajorTickSpacing(50);
		HelikopterHoehe.setMaximum(450);
		HelikopterHoehe.setSnapToTicks(true);
		HelikopterHoehe.setPaintTicks(true);
		HelikopterHoehe.setPaintLabels(true);
		GridBagConstraints gbc_HelikopterHoehe = new GridBagConstraints();
		gbc_HelikopterHoehe.fill = GridBagConstraints.BOTH;
		gbc_HelikopterHoehe.insets = new Insets(0, 0, 5, 5);
		gbc_HelikopterHoehe.gridx = 3;
		gbc_HelikopterHoehe.gridy = 4;
		getContentPane().add(HelikopterHoehe, gbc_HelikopterHoehe);
		
		JButton btnStart = new JButton("START!");
		btnStart.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				start();
			}
		});
		GridBagConstraints gbc_btnStart = new GridBagConstraints();
		gbc_btnStart.insets = new Insets(0, 0, 5, 5);
		gbc_btnStart.gridx = 5;
		gbc_btnStart.gridy = 4;
		getContentPane().add(btnStart, gbc_btnStart);
		
		JLabel lblHelikopterHoehe = new JLabel("Helikopter Hoehe");
		GridBagConstraints gbc_lblHelikopterHoehe = new GridBagConstraints();
		gbc_lblHelikopterHoehe.insets = new Insets(0, 0, 0, 5);
		gbc_lblHelikopterHoehe.gridx = 3;
		gbc_lblHelikopterHoehe.gridy = 5;
		getContentPane().add(lblHelikopterHoehe, gbc_lblHelikopterHoehe);
		
		gbc_Feld.fill = GridBagConstraints.BOTH;
		gbc_Feld.insets = new Insets(0, 0, 5, 5);
		gbc_Feld.gridx = 3;
		gbc_Feld.gridy = 3;		
		Feld.setSize(new Dimension(500, 450));
		Feld.setMaximumSize(new Dimension(500, 450));
		Feld.setBackground(new Color(135, 206, 235));
		Feld.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		Feld.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		Feld.setLayout(null);
		Helikopter.setBounds(183, 0, 111, 46);
		Helikopter.setIcon(new ImageIcon(Helikoptergui.class.getResource( "Heli.png")));
		Feld.add(Helikopter);

		JLabel bg = new JLabel("");
		bg.setBounds(0, 346, 492, 100);
		bg.setIcon(new ImageIcon(Helikoptergui.class.getResource("bg.png")));
		Feld.add(bg);
		
		getContentPane().add(Feld, gbc_Feld);
		
		}
	
	public void checkWind(boolean i){
		if(i){WindvonLinks.setValue(0);}
		else{WindvonRechts.setValue(0);	}

	}

	
	public void start(){
		Helikopter.setBounds(HelikopterHoehe.getValue(),0,111, 46);
		Feld.add(Helikopter);
		getContentPane().add(Feld, gbc_Feld);

	}	

	



}
 
Zuletzt bearbeitet:
Hallo, ich konnte den "abgewandelten Code" in meiner Umgebung starten und erhielt zuerst zwei NullPointerExceptions wegen den nicht vorhandenen Heli.png und bg.pgn. Nachdem ich das korrigiert habe, wurde sowohl das Bild im Heli.pgn wie auch der Hintergrund in bg.png im Applet richtig dargestellt. Es liegt also kaum an den Panels. Bist Du sicher, dass beide Dateien Heli.pgn und bg.pgn im gleichen Verzeichnis wie das File Helikoptergui.class liegen?
 
Hallo,
erstmal vielen dank für deine Antwort.
Ich bekomme das Programm nicht zum laufen. Die Heli.png und bg.png liegen im selben Verzeichnis und ich bekomme auch keine Exception.
In was für einer Umgebung hast du es denn zum laufen gebracht? Ich benutze Eclipse.
 
Ich komme nicht mehr weiter. Könntest du mir sagen, was du genau geändert hast, oder mir den Code senden. Ich weiß nicht, was bei mir falsch sein soll.

okay. wow :D meine Eclipse Umgebaung war irgendwie total verammelt.
Habe Eclipse neu installiert und es lief mit demselben Code. Dennoch vielen Dank. Ich habe bestimmt noch weitere Fragen.

lG zipfelQ
 
Zuletzt bearbeitet von einem Moderator:

Neue Beiträge

Zurück