validate(), repaint funktionieren nicht!

  • Themenstarter Themenstarter Ro0kie
  • Beginndatum Beginndatum
R

Ro0kie

Gast
Liebes Java-Forum! 🙂

Ich habe schon wieder Probleme mit meinem Applet nur dieses mal funktionieren weder
Code:
validate()
noch
Code:
repaint()
!😡:wuerg:


lottotippgrafikfehler.jpg


Obwohl "mit Joker" angehakt ist wird es nicht angezeigt und der rest erklärt sich von selbst...

Java:
import javax.swing.JApplet;

/**
 * Applet,welches dem Benutzer einen Lottotipp gibt.
 *
 * @author Martin S
 * @version 2011-05-10
 */
public class LottotippApplet extends JApplet {
	/**
	 * Methode,welche nach dem laden des Applets gestartet wird.
	 */
	public void init(){
	    add (new LottotippPanel());
	}
}

Java:
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author Martin
 *
 */
public class Lottotipp {
    private Set<Integer> tipp = new TreeSet<Integer>();

    public  String getLottotipp(){
       while(tipp.size()<6){
            tipp.add((int) (Math.random()*45+1));
       }
    String txt = "";
    //For-each Schleife
    for(Integer i : tipp) {
        txt += i + ", ";
    }
    return txt.substring(0, txt.length() - 2);
    }

    public String getJoker(){

        String txt2 = "";
        //Zusatztzahl erzeugen
        int joker = (int) (Math.random()*45+1);
     //Prfen ob Zusatztzahl schon vorhanden
        while(tipp.contains(joker)){
            joker = (int) (Math.random()*45+1);
        }
        //TreeSet zerlegen und in String wandeln
        Iterator it =tipp.iterator();
        while(it.hasNext()){
           txt2=txt2 +it.next();
           txt2 = txt2 + ", ";

        }


        txt2 =txt2 + "| " + joker;
        return txt2;
    }
}
Java:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Set;
import java.util.TreeSet;


/**
 * Panel für das Applet "LottotippApplet".
 *
 * @author Martin Suschny
 * @version 2011-05-10
 */
public class LottotippPanel extends JPanel {

	private JButton newtipp;
        private JCheckBox joker;
	private JTextField anzeige;
        private JPanel panel;        


	public LottotippPanel(){

		//Layot setzen
		this.setLayout(new GridLayout(2,1));


		//Grafische Elemente init und hinzufügen.
		newtipp = new JButton("Neuer Tipp");
		anzeige = new JTextField();
                joker = new JCheckBox("mit Joker");
                
                
                //Panel
                panel = new JPanel();
                panel.setLayout (new FlowLayout());
                panel.add (newtipp);
                panel.add (joker);
                add (anzeige);
		add (panel);

		//Grafische Elemente beim ActionLister regestrieren.
		LottotippAction h = new LottotippAction();
		newtipp.addActionListener(h);
		anzeige.addActionListener(h);
                joker.addActionListener(h);
	}
	public class LottotippAction implements ActionListener{
		public void actionPerformed(ActionEvent e){
                    Lottotipp tipp = new Lottotipp();
                    if(e.getSource()==newtipp){
			String txt = tipp.getLottotipp();
			anzeige.setText(txt);
                         
                    }
                     
                    if((e.getSource()==newtipp) &&(joker.isSelected()==true)){
			String txt2 = tipp.getJoker();
			anzeige.setText(txt2);                        
                         
                    }
                    
                }
        }
}
Ich habe die Methoden
Code:
validate()
und
Code:
repaint()
schon an ziemlich jeder Stelle ausprobiert 😉
Jede Möglcihe Idee wäre hilfreich !
Danke im vorhinein

MfG Ro0kie
 
Funktioniert's so?
Java:
import javax.swing.JApplet;

/**
 * Applet,welches dem Benutzer einen Lottotipp gibt.
 * 
 * @author Martin S
 * @version 2011-05-10
 */
public class LottotippApplet extends JApplet {

  /** Creates a new {@code LottotippApplet}. */
  public LottotippApplet() {
    add(new LottotippPanel());
  }

  /**
   * Methode,welche nach dem laden des Applets gestartet wird.
   */
  public void init() {}
}

Oder so?
Java:
import javax.swing.JApplet;
import javax.swing.SwingUtilities;

/**
 * Applet,welches dem Benutzer einen Lottotipp gibt.
 * 
 * @author Martin S
 * @version 2011-05-10
 */
public class LottotippApplet extends JApplet {

  /**
   * Methode,welche nach dem laden des Applets gestartet wird.
   */
  public void init() {
    SwingUtilities.invokeLater(new Runnable() {

      public void run() {
        add(new LottotippPanel());
      }
    });
  }
}

Oder hab ich Dein Problem nicht richtig verstanden?

Ebenius
 
Swing wird per Software gerendert (davon abgesehen, dass es sehr unwahrscheinlich ist, das er deshalb den alten Zustand zeichnen würde).
Verwendest du echt nur den oben geposteten Source?
 
Ich benutze den integrierten von Netbeans, komischerweise stürtzt der GraKa-treiber öfters ab beim Ausführen von Applets, meist klappt das Applet anfangs immer inwandfrei aber nach ein paar Tagen nicht mehr 😡

Die verlinkten Applets scheinen zu klappen 🙂
ja ich benutze den obrigen Source
 

Zurück
Oben