Swing Es wird nichts angezeigt

NecroniX

Mitglied
Ich hab mir hier ein kleines System geschrieben, das verschiedene GUI managen soll.
Das soll ermöglichen von überall ohne probleme auf die GUI Element drauf zu zugreifen.
Nun wird aber keines dieser Element angezeigt,

View.java
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.util.Random;
import java.util.Hashtable;
import Core.Interfaces.Item;
import Core.Exceptions.*;

public  class View {
    long id;
    String name;
    JFrame mainFrame;
    Hashtable<String,ViewContainer> container = new Hashtable();
    Hashtable<String,String> _ = new Hashtable();
    
    public View () {
        this.mainFrame = new JFrame();
        
        this.getMainFrame().setBounds(100, 100, 400, 400);
        this.getMainFrame().setLayout(null);
        this.getMainFrame().setVisible(true);
        
        Random random = new Random();
        this.id = random.nextLong();
    }
    
    public View (JFrame frame) {
        this.mainFrame = frame;
    }
    
    public JFrame getMainFrame() {
        return this.mainFrame;
    }
    
    public void setMainFrame(JFrame frame) {
        this.mainFrame = frame;
    }
    
    public void addContainer(String name,ViewContainer container) {
        this.container.put(name, container);
        this.mainFrame.getContentPane().add(container.getContainer());
    }
    
    public ViewContainer getContainer(String containerName) {
        return this.container.get(containerName);
    }

    public void showView() {
    /*
        JLabel label2 =(JLabel) this.getContainer("Programm").getComponent("label2");
        System.out.println(label2.getText());
        System.out.println(this.getContainer("Programm").getComponent("label2").toString());
    */    System.out.println("Test");
    
    }


}

ViewContainer
Java:
import java.awt.Container;
import java.awt.Component;
import java.util.Hashtable;
import java.util.Set;
import java.util.Random;

public class ViewContainer {

    String name;
    long id;
    Container container;
    Hashtable<String,Component> components = new Hashtable();
    Hashtable<String,String> _ = new Hashtable();

    public ViewContainer(Container cp,String name) {
        Random random = new Random();

        this.container = cp;
        this.id = random.nextLong();
        this.name = name;

    }
    public ViewContainer(String name) {
        Random random = new Random();
        
        this.container = new Container();
        this.id = random.nextLong();
        this.name = name;

    }
    public void addComponent(String componentName,Component component) {
        this.components.put(componentName, component);
        this.container.add(component);
    }
    public Component getComponent(String componentName) {
        return this.components.get(componentName);
    }
    public Set getAllComponents() {
        return this.components.entrySet();
    }
    public Container getContainer() {
        return this.container;
    }
    public void addValue(String name, String value) {
        this._.put(name, value);
    }
    public String getValue(String name) {
        return this._.get(name);
    }
}

Wenn ich dies nun so aufrufe wird nichts angezeigt und ich weiß einfach nicht warum ?!
Java:
import java.awt.*;
import javax.swing.*;
/**
 *
 * @author GRADY
 */
public class MainClass {
    
    public static void main(String args[]) {
        View mainView = new View();
        JLabel label2 = new JLabel("nix");
        ViewContainer cp = new ViewContainer("content");

        label2.setBounds(200, 100, 50,20);
        cp.addComponent("label2",label2);
        mainView.addContainer("Programm", cp );
        mainView.showView();     
    }
}

Ich hoffe ihr könnt mir helfen. Bin echt ratlos!

Schon mal Danke im Vorraus.

Mfg
NecroniX
 
ganz schönes Hin und Her an Komponenten, ich vermute aber erstmal dass das alles so stimmt,
dann gilt es noch, nach Hinzufügen der Komponenten die Anzeige neu zu initialisieren

am einfachsten ist, alles VOR dem setVisible(true);-Aufruf einzufügen, der kümmert sich darum,
diesen Aufruf also in die bisher recht leere showView()-Methode verschieben

alternativ:
fügt man nach setVisible(true); noch was hinzu, dann am JFrame validate(); + repaint(); aufrufen

----
ach, doch was gefunden:
setLayout(null);
auf das ContentPane des JFrames bedeutet, dass alle dort eingefügten Komponenten setBounds() haben müssen
(nicht das JFrame selber, auch nicht dessen ContentPane),
streiche setLayout(null); oder lerne vorher kennen, was das bedeutet

Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
 
Zuletzt bearbeitet von einem Moderator:
Hmm hab grad mal alle deine Tipps ausprobiert, aber es funktioniert nichts von alle dem der Frame bleibt leer ... warum auch immer ?!

Aber Danke für deinen Tipp mit Layout, wusste dies vorher nicht 🙂
 
@NecroniX
hast du den zweiten Teil meines Postings gelesen, leicht später hinzugefügt?

Zeile 22 von View weg, setVisible(true) nach showView() verschoben, schon läuft es bei mir

Java:
public class Test {

	public static void main(String arg[]) {
		View mainView = new View();
		JLabel label2 = new JLabel("nix");
		ViewContainer cp = new ViewContainer("content");

		label2.setBounds(200, 100, 50, 20);
		cp.addComponent("label2", label2);
		mainView.addContainer("Programm", cp);
		mainView.showView();

	}

}

class View {
	long id;
	String name;
	JFrame mainFrame;
	Hashtable<String, ViewContainer> container = new Hashtable();
	Hashtable<String, String> _ = new Hashtable();

	public View() {
		this.mainFrame = new JFrame();

		this.getMainFrame().setBounds(100, 100, 400, 400);
		// this.getMainFrame().setLayout(null);

		Random random = new Random();
		this.id = random.nextLong();
	}

	public View(JFrame frame) {
		this.mainFrame = frame;
	}

	public JFrame getMainFrame() {
		return this.mainFrame;
	}

	public void setMainFrame(JFrame frame) {
		this.mainFrame = frame;
	}

	public void addContainer(String name, ViewContainer container) {
		this.container.put(name, container);
		this.mainFrame.getContentPane().add(container.getContainer());
	}

	public ViewContainer getContainer(String containerName) {
		return this.container.get(containerName);
	}

	public void showView() {
		/*
		 * JLabel label2 =(JLabel)
		 * this.getContainer("Programm").getComponent("label2");
		 * System.out.println(label2.getText());
		 * System.out.println(this.getContainer
		 * ("Programm").getComponent("label2").toString());
		 */System.out.println("Test");
		this.getMainFrame().setVisible(true);

	}

}

class ViewContainer {

	String name;
	long id;
	Container container;
	Hashtable<String, Component> components = new Hashtable();
	Hashtable<String, String> _ = new Hashtable();

	public ViewContainer(Container cp, String name) {
		Random random = new Random();

		this.container = cp;
		this.id = random.nextLong();
		this.name = name;

	}

	public ViewContainer(String name) {
		Random random = new Random();

		this.container = new Container();
		this.id = random.nextLong();
		this.name = name;

	}

	public void addComponent(String componentName, Component component) {
		this.components.put(componentName, component);
		this.container.add(component);
	}

	public Component getComponent(String componentName) {
		return this.components.get(componentName);
	}

	public Set getAllComponents() {
		return this.components.entrySet();
	}

	public Container getContainer() {
		return this.container;
	}

	public void addValue(String name, String value) {
		this._.put(name, value);
	}

	public String getValue(String name) {
		return this._.get(name);
	}
}
 

Neue Themen


Zurück
Oben