Frames schließen

Status
Nicht offen für weitere Antworten.

s0n'4

Mitglied
Hallo Leute! Mein erster Thread!!

Also hab hier nen Code und überlege warum bei meinem Frame der Schliessbutton nicht funktioniert, kann mir da jemand vielleicht weiterhelfen???

thx schonmal


Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Label;
import java.awt.TextField;




class GridBagFrame extends Frame {

        Auto VW = new Auto();
    Panel pNorth = new Panel();
    Panel pSouth = new Panel();
    Panel pWest = new Panel();
    Panel pEast = new Panel();
    Panel pCenter = new Panel();
    List oben = new List (5, true);




  public GridBagFrame() {
    super("GridBagLayoutDemo");
    VW.init(33, 8, 200);
    GridBagConstraints constraints = new GridBagConstraints();
    setLayout(new GridBagLayout());
    setBackground(Color.lightGray);
    Component c;

    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.gridheight = 2;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.insets = new Insets(10, 5, 10, 5);
    Label header = new Label("VW");
    header.setBackground(Color.gray);
    header.setFont(new Font("Sans Serif", Font.BOLD, 20));
    add(header, constraints);

    constraints.anchor = GridBagConstraints.CENTER;
    constraints.gridwidth = GridBagConstraints.RELATIVE;
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.insets = new Insets(0, 5, 0, 2);
   
    constraints.insets = new Insets(0, 5, 5, 5);
    c = new TextField();
    defineFont(c);
    
    
    
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE; 
    constraints.gridwidth = GridBagConstraints.RELATIVE;
    constraints.insets = new Insets(0, 5, 0, 0);
    constraints.gridheight = 1;
   // c = new Label("km fahren");
  //  defineFont(c);
  //  add(c, constraints);
    
    constraints.gridheight = 1;
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.BOTH; 
//    constraints.insets = new Insets(0, 0, 0, 5);
    constraints.insets = new Insets(0, 5, 5, 5);
    defineFont(c);
    
    
    constraints.gridwidth = GridBagConstraints.RELATIVE;
    constraints.gridheight = 2;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    add(VW.FuellstandsAnzeige, constraints);
    VW.FuellstandsAnzeige.setBackground(Color.red);
    VW.FuellstandsAnzeige.setFont (new Font("Arial", Font.BOLD, 16));
    

    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.insets = new Insets(0, 5, 5, 5);
    constraints.gridheight = 1;
    constraints.fill = GridBagConstraints.BOTH; 
    defineFont(c);
    add(VW.Tanken, constraints);
    VW.Tanken.setFont (new Font("Arial", Font.BOLD, 14));
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.gridheight = 1;
    constraints.fill = GridBagConstraints.BOTH; 
    defineFont(c);
    add(VW.Nochmal, constraints);
    VW.Nochmal.setFont (new Font("Arial", Font.BOLD, 14));
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.gridheight = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    add(VW.Money, constraints);
    VW.Money.setBackground(Color.blue);
    VW.Money.setFont (new Font("Arial", Font.BOLD, 16));
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.gridheight = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    header.setFont(new Font("Sans Serif", Font.BOLD, 16));
    add(VW.Pleite, constraints);
    VW.Pleite.setBackground(Color.yellow);
    VW.Pleite.setFont (new Font("Arial", Font.BOLD, 16));

    //constraints.insets = new Insets(0, 5, 0, 5);    
    //c = new Button("Löschen");
    //defineFont(c);
    //add(c, constraints);
 
    
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.gridheight = 2;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    header.setFont(new Font("Sans Serif", Font.BOLD, 16));
    add(VW.Status, constraints);
    VW.Status.setBackground(Color.green);
    VW.Status.setFont (new Font("Arial", Font.BOLD, 16));
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 10, 5);
    defineFont(c);
    add(VW.Idiotb, constraints);

    pack();

  }

  void defineFont(Component c) {
    c.setFont(new Font("SansSerif", Font.PLAIN, 16));
  }


}
 

        

 class Auto
    {

        int Tankinhalt, VerbrauchAuf100km, Geld;
        Label FuellstandsAnzeige = new Label ("     ");
        Label Status = new Label ("Standartfüllung");
        Label Idiot = new Label ("                                      ");
        Label Money = new Label ("            ");
        Label Pleite = new Label ("              ");
        //Button Fahren = new Button ("LOS!");
        Button Fahren = new Button ("100km fahren");
        Button Tanken = new Button ("10L tanken kostet 15€");
        Button Idiotb = new Button ("Hilfe!");
        Button Nochmal = new Button ("200€ verdienen!");
        
        public void init (int Anfangsfuellung, int Verbrauch, int AnfangGeld)
        {
            Tankinhalt = Anfangsfuellung;
            VerbrauchAuf100km = Verbrauch;
            Geld = AnfangGeld;
            update ();
            Tanken.addActionListener (new TankenButtonListener ());
            Fahren.addActionListener (new FahrenButtonListener ());
            Idiotb.addActionListener (new IdiotButtonListener ());
            Nochmal.addActionListener (new NochmalButtonListener());
        }
        
        void update()
        {
            FuellstandsAnzeige.setText (Integer.toString (Tankinhalt)+" Liter  ");
            Money.setText (Integer.toString (Geld)+" €  ");
        }
        
        class TankenButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {
                if (Geld>15)
                {
                    Tankinhalt = Tankinhalt +10;
                
                    if (Tankinhalt>60)
                    {
                        Tankinhalt = 60;
                        Status.setText ("Voll");
                    }
                    else
                    {
                        Status.setText ("Wird Voller");
                        Geld = Geld - 15;
                    
                        if (Geld<0)
                        {
                            Geld = 0;
                            Pleite.setText (" Du bist Pleite!");
                        
                        }
                
                    }
                }
                else
                {
                    if (Geld>0)
                    {
                        Pleite.setText ("Du hast zuwenig Geld!");
                        Status.setText ("Ohne Moos nix los!");
                    }
                    else
                    {
                         if (Geld<0)
                        {
                            Geld = 0;
                            Pleite.setText (" Du bist Pleite!");
                        
                        }
                    }
                }
                update();
            }
        }
        
        class FahrenButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {
                Tankinhalt = Tankinhalt -8;
                
                if (Tankinhalt<=0)
                {
                    Tankinhalt = 0;
                    Status.setText ("Leer!!");
                }
                else
                {
                    if (Tankinhalt<15)
                    {
                        Status.setText ("Solltest Tanken!");
                    }
                    else
                    {
                        Status.setText ("Wird leerer");
                    }
                }
                update();
            }
        }
        
        class IdiotButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {


                Idiot.setText ("Hilfe ist Luxus!");
            }
        }
        
        class NochmalButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {
                                Geld = Geld + 200;
                Pleite.setText ("              ");
                update();
            }
        }
    }
        
public class Main extends Applet {

    GridBagFrame gridBagFrame ;

  public void init() {
    gridBagFrame = new GridBagFrame();
  }

  public void start() {
    gridBagFrame.setVisible(true);
  }

  public void stop() {
    gridBagFrame.setVisible(false);
  }

  public void destroy() {
    gridBagFrame.dispose();
    gridBagFrame = null;
  }
  
        
    }
 
Code:
frame.addWindowsListener(
        new WindowAdapter() {
             public void windowClosing( WindowEvent e) {
              System.exit( 0);
             }
            });

Ich würde dir aber zu Swing raten (und ganz subjektiv: vom GridBagLayout AB :wink: )
 
Warum? also ich finde mit dem GridBag kann man am besten Layouten (Was is Swing?)

Ähm, wo muss ich das einsetzen?
Bin wirklich neu in Java, hab da nich so viel ahnung und da wo ich denke gibts immer nen Error -,-


Ach ja noch eine Frage: Wie kann ich mir Informationen aus so einem Frame im Hauptfenster anzeigen lassen?
 
Den Code von Marco13 setzt Du in den Konstruktor, irgendwo in der Gegen von Zeile 38 würde ich vorschlagen. Du mußt allerdings das "frame." weglassen, also nur addWindowListener(...)

Zu Swing kannst Du in jedem Handbuch nachlesen, z. B. dem Javabuch von Guido Krüge (Download unter www.javabuch.de).

Zu Deiner letzten Frage: Pauschal gesagt mußt Du in Deinem Hauptfenster geeignete Methode zur Übergabe bereitstellen und Dein Frame muß das Hauptfenster kennen, dann rufst Du aus dem aktuellen Frame die Methode des Hauptfesters auf und sendest die Information rüber.

<edit>
Habe eben erst gesehen, daß Du das aus einem Applet erezugst. Ich persönlich findes das immer etwas umständlich, aus einem Applet einen Frame zu erzeugen. Gib mal in der Forumssuche "Webstart" ein....
</edit>
 
Bei mir kommt dann immer die Meldung :
cannot find Symbol - method addWindowsListener(<anonymous java.awt.event.WindowAdapter>)
 
Oh danke!!!!
Also es gibt jetzt zwar keinen Fehler mehr aber wenn ich bei extends, also in Zeile 19 statt frame JFrame schreibe geht es auch, wobei ich gelesen hab,dass das Fenster dann nur versteckt wird.
 
Sobald du auch nur einzige Swing-GUI-Komponente nutzt, solltest du auf AWT-GUI-Komponenten verzichten. Also niemals Swing und AWT mischen.

Die Zeile zum Schließen des Fensters in Swing sieht viel einfacher aus.
Code:
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

Wenn du gleich die Anwendung beenden willst:
Code:
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 
ja is ja toll, fanke für den rat, aber ich kann leider nicht auf alle awt componenten verzichten, ich hab nämlich keinen blassen Schimmer wie ich nur swingbefele nutze...

trotzdem thx
werd mich mal weiter versuchen

ich hab da immer so einen Fehler, denke aber das der daher kommt das ich awt und swing mische. (Also der Fehler kommt wenn ich das Applet starte: "Ausnahme: java.security.AccessControlException:access denied (java.lang.RuntimePermission exitVM) ").

naja is auch nur nen Schulprojekt, also is nicht so schlimm wenn was nicht funktioniert, dann versuch ich eben erstmal was anderes. ne lösung wär aber trotzdem gut!)
Hier nochmal mein aktualisierter Code:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Label;
import java.awt.TextField;




// Das ist der Hauptteil
        
public class Main extends JApplet {

    VWAUTO vwauto ;
    BMWAUTO bmwauto;
    Panel pNorth = new Panel();
    Panel pSouth = new Panel();
    Panel pWest = new Panel();
    Panel pEast = new Panel();
    Panel pCenter = new Panel();

    
  public void init() {
    

    

      
              
    vwauto = new VWAUTO();
    bmwauto = new BMWAUTO();

    setLayout(new BorderLayout());
    setBackground(Color.lightGray);
    
    
        add(BorderLayout.WEST, pWest);
        add(BorderLayout.EAST, pEast);
        add(BorderLayout.SOUTH, pSouth);
        add(BorderLayout.CENTER, pCenter);

    
        pWest.setLayout (new GridLayout(4, 1));
        pWest.setBackground(Color.black);
        
        pWest.add (vwauto.VW.Fahren);
        vwauto.VW.Fahren.setBackground(Color.lightGray);    
        
        Button BMW = new Button ("BMW");
        BMW.setBackground(Color.gray);
        BMW.setFont(new Font("Sans Serif", Font.BOLD, 20));
        pWest.add (BMW);
        vwauto.VW.Fahren.setBackground(Color.lightGray);    
        
        


    Button VW = new Button ("VW");
    VW.setBackground(Color.gray);
    VW.setFont(new Font("Sans Serif", Font.BOLD, 20));
    pWest.add(VW);
    

    Label Heador = new Label ("Tank VW");
    Heador.setBackground(Color.gray);
    Heador.setFont(new Font("Sans Serif", Font.BOLD, 20));
    add(Heador);
    
    
    add(vwauto.VW.FuellstandsAnzeige2);


    BMW.addActionListener (new BMWButtonListener());
    VW.addActionListener (new VWButtonListener());
    

}
  void defineFont(Component c) {
    c.setFont(new Font("SansSerif", Font.PLAIN, 16));
}

    //add(VW.FuellstandsAnzeige);


  public void start() {
    vwauto.setVisible(false);
    bmwauto.setVisible(false);
  }

  public void stop() {
    vwauto.setVisible(false);
    bmwauto.setVisible(false);
  }

  public void destroy() {
    vwauto.dispose();
    bmwauto.dispose();
    vwauto = null;
    bmwauto = null;
  }
  class BMWButtonListener implements ActionListener {                 //Buttonaktion BMW zeigen
              public void actionPerformed (ActionEvent a) {
               bmwauto.setVisible(true); 
             }
  
            }
            

            
  class VWButtonListener implements ActionListener {                 //Buttonaktion VW zeigen
              public void actionPerformed (ActionEvent a) {
               vwauto.setVisible(true); 
             }
  
            }
            
  
        
}
    
    
class Auto
    {

        int Tankinhalt, VerbrauchAuf100km, Geld;
        Label FuellstandsAnzeige = new Label ("     ");
        Label FuellstandsAnzeige2 = new Label ("     ");
        Label Status = new Label ("Standartfüllung");
        Label Idiot = new Label ("                                      ");
        Label Money = new Label ("            ");
        Label Pleite = new Label ("              ");
        //Button Fahren = new Button ("LOS!");
        Button Fahren = new Button ("100km fahren");
        Button Tanken = new Button ("10L tanken kostet 15€");
        Button Idiotb = new Button ("Hilfe!");
        Button Nochmal = new Button ("200€ verdienen!");
        
        public void init (int Anfangsfuellung, int Verbrauch, int AnfangGeld)
        {
            Tankinhalt = Anfangsfuellung;
            VerbrauchAuf100km = Verbrauch;
            Geld = AnfangGeld;
            update ();
            Tanken.addActionListener (new TankenButtonListener ());
            Fahren.addActionListener (new FahrenButtonListener ());
            Idiotb.addActionListener (new IdiotButtonListener ());
            Nochmal.addActionListener (new NochmalButtonListener());
            
        }
        
        void update()
        {
            FuellstandsAnzeige.setText (Integer.toString (Tankinhalt)+" Liter  ");
            FuellstandsAnzeige2.setText (Integer.toString (Tankinhalt)+" Liter  ");
            Money.setText (Integer.toString (Geld)+" €  ");
        }
        
        class TankenButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {
                if (Geld>15)
                {
                    Tankinhalt = Tankinhalt +10;
                
                    if (Tankinhalt>60)
                    {
                        Tankinhalt = 60;
                        Status.setText ("Voll");
                    }
                    else
                    {
                        Status.setText ("Wird Voller");
                        Geld = Geld - 15;
                    
                        if (Geld<0)
                        {
                            Geld = 0;
                            Pleite.setText (" Du bist Pleite!");
                        
                        }
                
                    }
                }
                else
                {
                    if (Geld>0)
                    {
                        Pleite.setText ("Du hast zuwenig Geld!");
                        Status.setText ("Ohne Moos nix los!");
                    }
                    else
                    {
                         if (Geld<0)
                        {
                            Geld = 0;
                            Pleite.setText (" Du bist Pleite!");
                        
                        }
                    }
                }
                update();
            }
        }
        
        class FahrenButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {
                Tankinhalt = Tankinhalt -8;
                
                if (Tankinhalt<=0)
                {
                    Tankinhalt = 0;
                    Status.setText ("Leer!!");
                }
                else
                {
                    if (Tankinhalt<15)
                    {
                        Status.setText ("Solltest Tanken!");
                    }
                    else
                    {
                        Status.setText ("Wird leerer");
                    }
                }
                update();
            }
        }
        
        class IdiotButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {


                Idiot.setText ("Hilfe ist Luxus!");
            }
        }
        
        class NochmalButtonListener implements ActionListener
        {
            public void actionPerformed (ActionEvent a)
            {
                                Geld = Geld + 200;
                Pleite.setText ("              ");
                update();
            }
        }
}    
    
    
    
    
    
    
    
    
    
    //VW Frame
    
    class VWAUTO extends JFrame {

        Auto VW = new Auto();    



  public VWAUTO() {
         super("VW");
      

    VW.init(33, 8, 200);
    GridBagConstraints constraints = new GridBagConstraints();
    setLayout(new GridBagLayout());
    setBackground(Color.lightGray);
    Component c;
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.gridheight = 2;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.insets = new Insets(10, 5, 10, 5);
    Label header = new Label("VW");
    header.setBackground(Color.gray);
    header.setFont(new Font("Sans Serif", Font.BOLD, 20));
    add(header, constraints);

    constraints.anchor = GridBagConstraints.CENTER;
    constraints.gridwidth = GridBagConstraints.RELATIVE;
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.insets = new Insets(0, 5, 0, 2);
   
    constraints.insets = new Insets(0, 5, 5, 5);
    c = new TextField();
    defineFont(c);
    
    
    
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE; 
    constraints.gridwidth = GridBagConstraints.RELATIVE;
    constraints.insets = new Insets(0, 5, 0, 0);
    constraints.gridheight = 1;
    //c = new Label("km fahren");
    //defineFont(c);
    //add(c, constraints);
    
    constraints.gridheight = 1;
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.BOTH; 
    //constraints.insets = new Insets(0, 0, 0, 5);
    constraints.insets = new Insets(0, 5, 5, 5);
    //add(VW.Fahren, constraints);
    defineFont(c);
    
    
    constraints.gridwidth = GridBagConstraints.RELATIVE;
    constraints.gridheight = 2;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    add(VW.FuellstandsAnzeige, constraints);
    VW.FuellstandsAnzeige.setBackground(Color.red);
    VW.FuellstandsAnzeige.setFont (new Font("Arial", Font.BOLD, 16));
    

    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.insets = new Insets(0, 5, 5, 5);
    constraints.gridheight = 1;
    constraints.fill = GridBagConstraints.BOTH; 
    defineFont(c);
    add(VW.Tanken, constraints);
    VW.Tanken.setFont (new Font("Arial", Font.BOLD, 14));
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.gridheight = 1;
    constraints.fill = GridBagConstraints.BOTH; 
    defineFont(c);
    add(VW.Nochmal, constraints);
    VW.Nochmal.setFont (new Font("Arial", Font.BOLD, 14));
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.gridheight = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    add(VW.Money, constraints);
    VW.Money.setBackground(Color.blue);
    VW.Money.setFont (new Font("Arial", Font.BOLD, 16));
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.gridheight = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    header.setFont(new Font("Sans Serif", Font.BOLD, 16));
    add(VW.Pleite, constraints);
    VW.Pleite.setBackground(Color.yellow);
    VW.Pleite.setFont (new Font("Arial", Font.BOLD, 16));

    //constraints.insets = new Insets(0, 5, 0, 5);    
    //c = new Button("Löschen");
    //defineFont(c);
    //add(c, constraints);
 
    
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.gridheight = 2;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 5, 5);
    header.setFont(new Font("Sans Serif", Font.BOLD, 16));
    add(VW.Status, constraints);
    VW.Status.setBackground(Color.green);
    VW.Status.setFont (new Font("Arial", Font.BOLD, 16));
    
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 5, 10, 5);
    defineFont(c);
    add(VW.Idiotb, constraints);
    
    

    pack();

  }

  void defineFont(Component c) {
    c.setFont(new Font("SansSerif", Font.PLAIN, 16));
  }


}



    //BMW Frame
    class BMWAUTO extends JFrame {

        Auto BMW = new Auto();    


        public BMWAUTO() {
            super("BMW");
            BMW.init(33, 8, 200);
            GridBagConstraints constraints = new GridBagConstraints();
            setLayout(new GridBagLayout());
            setBackground(Color.lightGray);
            Component c;

            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.gridheight = 2;
            constraints.anchor = GridBagConstraints.CENTER;
            constraints.fill = GridBagConstraints.BOTH; 
            constraints.insets = new Insets(10, 5, 10, 5);
            Label header = new Label("BMW");
            header.setBackground(Color.gray);
            header.setFont(new Font("Sans Serif", Font.BOLD, 20));
            add(header, constraints);
            
            constraints.anchor = GridBagConstraints.CENTER;
            constraints.gridwidth = GridBagConstraints.RELATIVE;
            constraints.fill = GridBagConstraints.BOTH; 
            constraints.gridwidth = 2;
            constraints.gridheight = 1;
            constraints.insets = new Insets(0, 5, 0, 2);
            
            constraints.insets = new Insets(0, 5, 5, 5);
            c = new TextField();
            defineFont(c);
            
            
            
            constraints.anchor = GridBagConstraints.WEST;
            constraints.fill = GridBagConstraints.NONE; 
            constraints.gridwidth = GridBagConstraints.RELATIVE;
            constraints.insets = new Insets(0, 5, 0, 0);
            constraints.gridheight = 1;
            // c = new Label("km fahren");
            //  defineFont(c);
            //  add(c, constraints);
            
            constraints.gridheight = 1;
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.anchor = GridBagConstraints.WEST;
            constraints.fill = GridBagConstraints.BOTH; 
            //    constraints.insets = new Insets(0, 0, 0, 5);
            constraints.insets = new Insets(0, 5, 5, 5);
            //add(BMW.Fahren, constraints);
            defineFont(c);
            
            
            constraints.gridwidth = GridBagConstraints.RELATIVE;
            constraints.gridheight = 2;
            constraints.fill = GridBagConstraints.BOTH;
            constraints.insets = new Insets(0, 5, 5, 5);
            add(BMW.FuellstandsAnzeige, constraints);
            BMW.FuellstandsAnzeige.setBackground(Color.red);
            BMW.FuellstandsAnzeige.setFont (new Font("Arial", Font.BOLD, 16));
            
            
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.anchor = GridBagConstraints.CENTER;
            constraints.insets = new Insets(0, 5, 5, 5);
            constraints.gridheight = 1;
            constraints.fill = GridBagConstraints.BOTH; 
            defineFont(c);
            add(BMW.Tanken, constraints);
            BMW.Tanken.setFont (new Font("Arial", Font.BOLD, 14));
            
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.anchor = GridBagConstraints.CENTER;
            constraints.gridheight = 1;
            constraints.fill = GridBagConstraints.BOTH; 
            defineFont(c);
            add(BMW.Nochmal, constraints);
            BMW.Nochmal.setFont (new Font("Arial", Font.BOLD, 14));
            
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.gridheight = 1;
            constraints.fill = GridBagConstraints.BOTH;
            constraints.insets = new Insets(0, 5, 5, 5);
            add(BMW.Money, constraints);
            BMW.Money.setBackground(Color.blue);
            BMW.Money.setFont (new Font("Arial", Font.BOLD, 16));
            
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.gridheight = 1;
            constraints.fill = GridBagConstraints.BOTH;
            constraints.insets = new Insets(0, 5, 5, 5);
            header.setFont(new Font("Sans Serif", Font.BOLD, 16));
            add(BMW.Pleite, constraints);
            BMW.Pleite.setBackground(Color.yellow);
            BMW.Pleite.setFont (new Font("Arial", Font.BOLD, 16));
            
            //constraints.insets = new Insets(0, 5, 0, 5);    
            //c = new Button("Löschen");
            //defineFont(c);
            //add(c, constraints);
            
            
    
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.gridheight = 2;
            constraints.fill = GridBagConstraints.BOTH;
            constraints.insets = new Insets(0, 5, 5, 5);
            header.setFont(new Font("Sans Serif", Font.BOLD, 16));
            add(BMW.Status, constraints);
            BMW.Status.setBackground(Color.green);
            BMW.Status.setFont (new Font("Arial", Font.BOLD, 16));
            
            constraints.gridwidth = GridBagConstraints.REMAINDER;
            constraints.fill = GridBagConstraints.BOTH;
            constraints.insets = new Insets(0, 5, 10, 5);
            defineFont(c);
            add(BMW.Idiotb, constraints);
            
            
            
            pack();
            
        }

        void defineFont(Component c) {
            c.setFont(new Font("SansSerif", Font.PLAIN, 16));
        }
        
     

}
 
s0n'4 hat gesagt.:
ja is ja toll, fanke für den rat, aber ich kann leider nicht auf alle awt componenten verzichten, ich hab nämlich keinen blassen Schimmer wie ich nur swingbefele nutze...
Na klar, ohne Weiteres! Es gibt keine Swing-Befehle, aber Swing-Klassen. Swing ist quasi eine Sammlung moderner und sehr leistungsfähiger Klassen, mit denen sich GUIs bauen lassen.
Meistens beginnen diese Komponenten mit einem "J" und sind im Paket javax.swing zu finden.
Wenn du also vor Label oder Panel noch ein J schreibst, benutzt du schon die Swing-Klasse und nicht mehr die AWT-Komponente.
Es geht also nicht um AWT-Klassen, wie bspw. Color, Graphics, Font oder Toolkit. Das Nicht-mischen-dürfen bezieht sich rein auf GUI-Komponenten, also Klassen, die von java.awt.Component erben.

s0n'4 hat gesagt.:
ich hab da immer so einen Fehler, denke aber das der daher kommt das ich awt und swing mische. (Also der Fehler kommt wenn ich das Applet starte: "Ausnahme: java.security.AccessControlException:access denied (java.lang.RuntimePermission exitVM) ").
Dieser Fehler tritt nie im Zusammenhang mit dem Mischen von Swing und AWT auf. Es geht hier vielmehr um die eingeschränkten Rechte eines Applets. Ein unsigniertes Applet darf die Java-VM nicht beenden.
 
dann hab ich aber nochmal eine Frage: wie is der Befehl für swing Labels um den Hintergrund festzulegen?
meiner funktioniert nicht mehr.

Code:
JLabel Headar = new JLabel ("Tank BMW");
    Headar.setBackground(Color.gray);
    Headar.setFont(new Font("Sans Serif", Font.BOLD, 20));
    pEast.add(Headar);


wobei die normalen Buttons ja viel besser aussehen
Buttons.JPG


jedenfalls die im kleinen extrafenster, keine ahnung warum die im Hauptfenster anders aussehen, wo die ja die gleichen Befehle haben.....
 
So ich hab ne neue Frage!
wie kann ich ein fenster programieren, in dem ich einen "weiter" button habe, durch den ich ein neues fenster lade, das das alte ersetzt?

und mit was für einem code lege ich ein hintergrund bild fest?


thx schon mal
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben