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
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;
}
}