SWT JComponent Problem

  • Themenstarter Themenstarter Joge
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
J

Joge

Gast
hi,
Ich wollte eine Erweiterung der JComponent programmieren. (Ein Rechteck)
Aber, es wird bei der Ausführung des Programms nur ein Rechteck gezeichnet!!!!!!
Code:
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

public class Zelle extends JComponent
{
	private int x,y;
	public Zelle(int x, int y)
	{
		this.x=x;
		this.y=y;
	}
	protected void paintComponent( Graphics g )
	{
		g.setColor(Color.WHITE);
		g.fillRect(x, y, 100, 100);
		g.setColor(Color.red);
		g.drawRect(x, y, 100, 100);
	}
}

++++++++++++++++++++++++
++++++++++++++++++++++++
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Editor2D extends JFrame
{
	private Zelle zelle;
	private Zelle zelle1;
	public Editor2D()
	{
		this.setSize(600, 600);
		this.setLocation(100,100);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	
		zelle= new Zelle(10,10);
		this.add(zelle);
		zelle1= new Zelle(120,10);
		this.add(zelle1);
		
		
		
		this.setVisible(true);
	}
	public static void main(String args[])
	{
		new Editor2D();
	}

}
 
Du willst ein Rechteck zeichnen, aber es wird nur ein Rechteck gezeichnet? :bahnhof: Mir ist das zu hoch. :wink:
 
Ich will 2 Rechtecke zeichnen, Aber es wird nur einen gezeichnet.
Es sollen Zelle und Zelle1 gezeichnet werden, leider es wird nur Zelle1 gezeichnet. (Zeile 42 des Codes ).
🙁
 
Code:
public class Editor2D extends JFrame 
{ 
   private Zelle zelle; 
   private Zelle zelle1; 
   public Editor2D() 
   { 
      setLayout(null); // Default-LayoutManager (BorderLayout) entfernen
      this.setSize(600, 600); 
      this.setLocation(100,100); 
      this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
      zelle= new Zelle(10,10); 
      zelle.setSize(100, 100); // Größe der Komponente setzen
      this.add(zelle); 
      zelle1= new Zelle(120,10);
      zelle1.setSize(100, 100); // Größe der Komponente setzen
      this.add(zelle1); 
      this.setVisible(true); 
   } 
   
   class Zelle extends JComponent 
   { 
      public Zelle(int x, int y) 
      { 
         setLocation(x, y);
      } 
      protected void paintComponent( Graphics g ) 
      { 
         g.setColor(Color.WHITE); 
         g.fillRect(0, 0, 99, 99); 
         g.setColor(Color.red); 
         g.drawRect(0, 0, 99, 99); 
      } 
   }
   
   public static void main(String args[]) 
   { 
      new Editor2D(); 
   } 

}
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben