Swing Componenten auf AWT Frame

pria

Mitglied
Warum sind Swing Componenten auf AWT Frames mit einer Painth-Methode zum zeichnen von Bildern nicht sofort sichtbar?

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends Frame
{
  // Anfang Variablen
  static JProgressBar jpb = new JProgressBar();
  ImageIcon[] icon = new ImageIcon[200];
  int[] X = new int[200];
  int[] Y = new int[200];
  private Image offscreenImage;
  private Graphics offscreenGraphics;
  private List lst = new List();
  private Button jbu = new Button("Hallo");
  private Button jbu2 = new Button("N Hallo");
  private JDesktopPane jdp = new JDesktopPane();
  private JTextField jtf = new JTextField();
  // Ende Variablen

  public test(String title) {
    // Frame-Initialisierung
    super(title);
    
    for(int i = 0;i < 200;i++)
    {
     X[i] = 0;
     Y[i] = 0;
     icon[i] = null;
    }

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) { System.exit(0); }
    });

    int frameWidth = 300;
    int frameHeight = 300;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2 ;
    setLocation(x, y);

    // Anfang Komponenten
    setLayout(null);

    jdp.setBounds(0,200,300,100);
    add(jdp);

    jpb.setBounds(50,50,50,5);
    add(jpb);

    lst.setBounds(200,50,100,100);
    lst.setBackground(new Color(10,60,10));
    lst.setForeground(new Color(0,255,100));
    lst.add("Hallo");
    lst.add("Nicht Hallo");
    lst.add("Reset");
    add(lst);
    lst.addItemListener( new ItemListener() {
          public void itemStateChanged( ItemEvent e )
          {lstActionPrefermed(e);}});

    jbu.setBounds(200, 30, 50, 20);
    jbu.setEnabled(false);
    jbu.setBackground(new Color(255,0,0));
    add(jbu);
    jbu.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        jbuActionPerformed(evt); } });

    jbu2.setBounds(250, 30, 50, 20);
    jbu2.setEnabled(false);
    jbu2.setBackground(new Color(255,0,0));
    add(jbu2);
    jbu2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        jbu2ActionPerformed(evt); } });

    jtf.setBounds(0, 0, 121, 21);
    jdp.add(jtf);
    // Ende Komponenten

    icon[1] = new ImageIcon("Pria.gif");
    setResizable(false);
    setVisible(true);
    repaint();
  }

  // Anfang Ereignisprozeduren
  void lstActionPrefermed(ItemEvent e)
  {
   /*System.out.println(e);
   System.out.println(e.getItem());
   System.out.println(e.getItemSelectable());
   System.out.println(e.getStateChange());
   System.out.println(e.paramString());*/
   if(lst.getSelectedItem().equals("Hallo"))
   {
    jbu.setEnabled(true);
    jbu.setBackground(new Color(0,255,0));
    jbu2.setEnabled(false);
    jbu2.setBackground(new Color(255,0,0));
   }
   else
   if(lst.getSelectedItem().equals("Nicht Hallo"))
   {
    jbu.setEnabled(false);
    jbu.setBackground(new Color(255,0,0));
    jbu2.setEnabled(true);
    jbu2.setBackground(new Color(0,255,0));
   }
   else
   if(lst.getSelectedItem().equals("Reset"))
   {
    jbu.setEnabled(false);
    jbu.setBackground(new Color(255,0,0));
    jbu2.setEnabled(false);
    jbu2.setBackground(new Color(255,0,0));
    setTitle("test");
   }

  }
  public void jbuActionPerformed(ActionEvent evt)
  {
    setTitle("Fenster sagt: Hallo");
  }
  public void jbu2ActionPerformed(ActionEvent evt)
  {
    setTitle("Fenster sagt: Nicht Hallo");
  }
  // Ende Ereignisprozeduren

   public void paint(Graphics g)
  {

  }

  public void update (Graphics g)
  {
  if (offscreenImage == null)
  {
   offscreenImage = createImage (this.getSize().width, this.getSize().height);
   offscreenGraphics = offscreenImage.getGraphics ();
  }

  offscreenGraphics.setColor (getBackground ());
  offscreenGraphics.fillRect (0, 0, this.getSize().width, this.getSize().height);

  for(int i = 0;i < 20;i++)
     if(icon[i] != null)
      offscreenGraphics.drawImage(icon[i].getImage(), X[i], Y[i], this);

  paint (offscreenGraphics);

  g.drawImage (offscreenImage, 0, 0, this);

  }

  public static void main(String[] args) {
    new test("test");

    jpb.setValue(0);

    for(int i = 0;i <= 100;i++)
    {
     jpb.setValue(i);
     try{Thread.sleep(50);}catch(Exception err){}
    }
    for(int i = 0;i < 20;i++)
    {
     jpb.setBounds(i * 10,i * 10,50,5);
     try{Thread.sleep(500);}catch(Exception err){}
    }

  }
}
 
Hallo,

AWT und Swing Komponenten sollen nicht miteinander kombiniert werden, weil es zu Darstellungsproblemen führt.

Wenn dir AWT nicht genug ist, und du möchtest ein natives Look & Feel, dann nimmste halt SWT. Wenn dir das egal ist, dann bietet dir Swing alles, was es in AWT gibt und mehr. Es macht irgendwie keinen Sinn, beide zu kombinieren, gedenke ich.


Vg Erdal
 
Zurück