textarea lässt panel verschwinden

flopps

Mitglied
Hallo,

ich hab ein JPanel auf welches ich eine JTextArea adden möchte. das soll dann zusammen auf einem JFrame angezeigt werden. Lasse ich das JPanel ohne die TextArea anzeigen funktioniert es, sobald ich nun aber diese wieder per .add hinzufüge verschwindet das JPanel sofort.
Wo kann das problem liegen?

grüße flopps
 
das ist der code der panel klasse, sobald man die textarea auskommentiert funktioniert wie gesagt alles gut

Java:
package StoneHeart;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class CardBase extends JPanel
{
    JLabel    jLName, jLMana, jLRarity, jLDescription, jLAtt, jLDeff, jLArtist;
    JPanel    jPPic, jPDescription;

    // Attribute f�r die lange beschreibung initialisieren
    JTextArea TAdescription;
    String text = "Das ist eine sehr m�chtige Kreatur die im Feuer geboren wurde. Sie kennt nur die Zerst�rung und will as Unheil �ber die Welt bringen";

    Card c;

    BufferedImage cardBase, cardPic, background;

    public CardBase(Card card)
    {
        this.c = card;
        cardBase   = loadPics("images/CardBase.png");
        cardPic    = loadPics("images/" + c.getName() + ".png");
        background = loadPics("images/" + c.getFaction() + ".png");

        initCard();
    }

    public void initCard()
    {
        // standartattribute initilaisieren
        setBounds(500,200,125,176);
        setOpaque(false);


        setLayout(null);
        Font f = new Font("Comic Sans MS", Font.BOLD, 8);
        // Font descriptionShort = new Font("Comic Sana MS", Font.LAYOUT_RIGHT_TO_LEFT, 8);
        Font descriptionLong = new Font("Comic Sana MS", Font.ITALIC, 10);

        jLName = new JLabel(c.getName());
        jLName.setFont(f);
        jLName.setBounds(12, 14, 101, 6);
        this.add(jLName);

        jLRarity = new JLabel(c.getRarity());
        jLRarity.setFont(f);
        jLRarity.setBounds(75,101,50,6);
        this.add(jLRarity);

        jLArtist = new JLabel(c.getArtist());
        jLArtist.setFont(f);
        jLArtist.setBounds(45, 163, 80, 6);
        this.add(jLArtist);

        if(c.getType().equals("Creature"))
        {
        // kreaturen spezifische karten attribute initialisieren


        jLMana = new JLabel("" + ((Creature)c).getMana());
        jLMana.setFont(f);
        jLMana.setBounds(100,14,30,6);
        this.add(jLMana);

        jLDescription = new JLabel("" + ((Creature)c).getDescriptionShort());
        jLDescription.setFont(f);
        jLDescription.setBounds(12,101,69,6);
        this.add(jLDescription);



        jPDescription = new JPanel();
        jPDescription.setBounds(12, 108, 100, 52);
        TAdescription = new JTextArea();
        TAdescription.setBounds(12, 108, 100, 52);			//Warum verschwindet die ganze karte????
        TAdescription.setFont(descriptionLong);
        TAdescription.setLineWrap(true);
        TAdescription.setWrapStyleWord(true);
        TAdescription.setFocusable(false);
        TAdescription.setText(text);
        this.add(TAdescription);


        jLAtt = new JLabel("" + ((Creature)c).getOffense());		// zahl gruen machen wenn geringer als standartwert
        jLAtt.setFont(f);											// zahl rot machen wenn hoeher als standartwert
        jLAtt.setBounds(7,159,10,10);
        this.add(jLAtt);

        jLDeff = new JLabel("" + ((Creature)c).getDefense());		// zahl rot machen wenn geringer als standartwert
        jLDeff.setFont(f);											// zahl gruen machen wenn hoeher als standartwert
        jLDeff.setBounds(107,159,10,10);
        this.add(jLDeff);
        System.out.println("kreatur wurde erstellt");
        }
        else if(c.getType().equals("Spell"))
        {
        jLMana = new JLabel("" + ((Spell)c).getMana());
        jLMana.setFont(f);
        jLMana.setBounds(100,14,30,6);
        this.add(jLMana);

        // textarea fuer den effekt noch adden

        System.out.println("Zauber wurde erstellt");
        }
        else
        {
        // textarea mit anzahl an zu generierendem mana adden
        }

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(background,6 ,6 ,114 ,164, this);
        g.drawImage(cardBase, 0, 0, this);
        g.drawImage(cardPic, 12, 21,100,79, null);
    }

    private BufferedImage loadPics(String path)
    {
        BufferedImage source = null;

        URL pic_Url = getClass().getClassLoader().getResource(path);

        try
        {
        source = ImageIO.read(pic_Url);
        }
        catch(IOException e)
        {
        JOptionPane.showMessageDialog(null, "Bild konnte nicht geladen werden.");
        }

        return source;
    }
}
/*
 * +++NEUE KARTENMA�E+++
 * Karte: 			  126x176
 * Hintergrund:		  6,6,114,164
 * name:			  12,14,101,6
 * Bild: 			  12,21,100,79
 * Beschreibung kurz: 12,101,69,6
 * Seltenheit:		  85,101,30,6
 * Beschreibung lang: 12,108,100,52
 * angriff: 		  8,160,8,8 (kreis)
 * verteidigung: 	  108,160,8,8 (kreis)
 * Kuenstler:		  45, 163, 80, 6
 * alle werte muessen evtl noch etwas angepasst werden
 */



/* set card dimension with a modifier
 * standard dimension is 1920X1024 there the modifier is 1
 * need getter method for Wideness of the panel
 * one card need 8% of the Wideness of the panel
 * the gaps between the cards are 3%
 * borders have 10% space for the deck, graveyard etc
 * reason: 100% - 2*10% borders = 80%
 * 7*8% = 56%     80% - 56% = 24%
 * 8 gaps / 24% = 3% 
 * basic dimension: 126x176
 * 1/13 is 20 pixel
 * 1/8 is 20 pixel
 */
 
eine möglichkeit die ich mir vorstellen kann ist da das alles auf ein weiteres panel gesetzt wird und dieses durch deren paint component neu gemalt wird überschreibt es das oben geschriebene jpanel sobal es mit der textarea befüllt wird. um dies nun zu vermeiden würde ich die obere klasse nicht von jpanel erben lassen sondern von rectangle und in dieses dann ein panel packen, so kann ich in der hauptklasse per drawrectangle das panel jedes mal gleichzeitig malen ohne das es übermalt wird.
 
ich hab mir den code bis zu folgender zeile angesehen

[java=44]//...
setLayout(null);
//...[/code]

danach war mir klar : layout-problem

anstatt also sich irgendwas selbst zusammenkleben zu wollen würde ich mich erstmal richtig mit layout-managern befassen und dann über gewisse methoden die swing bietet arbeiten

auch erstellst du völlig sinnlos ein JPanel das du gar nicht nutzt ...

das das add() der TextArea alles andere "verschwinden" lässt kann ich dir jetzt so spontan nicht beantworten weil ich schlicht keine lust habe mich durch so schlechten code zu wühlen ...


ich empfehle dir wie bereits gesagt erstmal ein anständiges grundlagen-studium von layout-managern

NullLayout nutzt man nicht !
 

Neue Themen


Zurück
Oben