Hangman CardLayout/repaint?

  • Themenstarter Themenstarter IchLerneJava
  • Beginndatum Beginndatum
I

IchLerneJava

Gast
Hi, bin dabei Hangman zu programmieren. Aktuell habe ich mehrere Klassen für eine grafische Realisierung erstellt (Hangman als Konsolenversion habe ich bereits erfolgreich zum laufen bekommen).

Die Hangman.java startet das Programm, in diesem Fenster soll dann der Spieler Optionen auswählen und das Spiel starten können. Nun kommt mein Anliegen. Die Hangman.java besitzt eine Menge Panels (Ich bin froh das es so aussieht). Wenn der Spieler das Spiel startet soll aber kein neues Fenster geöffnet werden, sondern der Fensterinhalt neu geschrieben werden (Zur Veranschaulichung habe ich HangmanPlay.java geschrieben, so soll das Fenster danach aussehen). Ich hab Informationen darüber gefunden es entweder mit der repaint-Methode oder den CardLayouts zu machen. Was ist bei so vielen Panels zum empfehlen? Ich habe mich schon an das Probleme gewagt, hatte aber nur mäßigen Erfolg. Jetzt werden die vielen Panels zu einem echten Problem...
Ich wäre euch für Anregungen, Hilfe und anderen Kommentaren sehr dankbar. Einen schönen Abend noch

Gruß
IchLerneJava

Folgend die Klassen:

Hangman.java
Java:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;

@SuppressWarnings("serial")
public class Hangman extends Frame
{
  private final HangmanWindowListener hangmanWindowListener = new HangmanWindowListener();
  private final HangmanActionListener hangmanActionListener = new HangmanActionListener(this);

  private final Panel                 pnlNorth              = new Panel(new GridLayout(1, 1));
  private final Panel                 pnlNorthRow1          = new Panel(new FlowLayout(
                                                                FlowLayout.LEFT));
  private final Panel                 pnlNorthRow2          = new Panel(new FlowLayout(
                                                                FlowLayout.LEFT));

  private final Panel                 pnlCenter             = new Panel(new GridLayout(1, 1));
  private final Panel                 pnlCenterCol1         = new Panel(new FlowLayout(
                                                                FlowLayout.LEFT));
  private final Panel                 pnlCenterCol2         = new Panel(new BorderLayout());
  private final Panel                 pnlCenterColx2        = new Panel(new BorderLayout());
  private final Panel                 pnlCenterCol2a        = new Panel();
  private final Panel                 pnlCenterCol2b        = new Panel();
  private final Panel                 pnlCenterCol2c        = new Panel();
  private final Panel                 pnlSouth              = new Panel(new GridLayout(1, 1));

  public Button                       btnPlay               = new Button("Start Game");
  public Button                       btnExit               = new Button("Exit");
  public Button                       btnAbout              = new Button("About");
  public Button                       btnaddCategory        = new Button(">>");
  public Button                       btnremoveCategory     = new Button("<<");

  private final Label                 lblname               = new Label("1. Name:");
  private final Label                 lblLevel              = new Label("2. Difficulty level:");
  private final Label                 lblCategory           = new Label(
                                                                "  3. Select Category/ies for your Game:");

  public TextField                    tfname                = new TextField(34);
  public TextArea                     txtAreaInfo           = new TextArea("Hello", 17, 45);
  public TextArea                     selCategory           = new TextArea(14, 20);

  public List                         listCategory          = new List(15, true);

  public CheckboxGroup                cbg                   = new CheckboxGroup();
  public Checkbox                     easy                  = new Checkbox("Easy", this.cbg, false);
  public Checkbox                     normal                = new Checkbox("Normal", this.cbg,
                                                                false);
  public Checkbox                     difficult             = new Checkbox("Difficult", this.cbg,
                                                                false);

  MenuBar                             mainbar               = new MenuBar();

  Menu                                game                  = new Menu("Game");
  Menu                                options               = new Menu("Options");
  Menu                                help                  = new Menu("Help");
  Menu                                importwc              = new Menu("Import");

  MenuItem                            newGame               = new MenuItem("New Game");
  MenuItem                            rank                  = new MenuItem("Ranking");
  MenuItem                            exit                  = new MenuItem("Exit");
  MenuItem                            level                 = new MenuItem("Level");
  MenuItem                            about                 = new MenuItem("About");
  MenuItem                            importWord            = new MenuItem("Words");
  MenuItem                            importCategory        = new MenuItem("Category");
  MenuItem                            changeColor           = new MenuItem("Change color (Hangman)");
  MenuItem                            instr                 = new MenuItem("Instructions");

  public Hangman()
  {
    this.setTitle("Hangman");
    this.setSize(700, 400);
    this.setLocationRelativeTo(null);

    this.listCategory.add("Animals");
    this.listCategory.add("Beer brands");
    this.listCategory.add("Celebrity");
    this.listCategory.add("Cities");
    this.listCategory.add("Computer science");
    this.listCategory.add("Countries");
    this.listCategory.add("Disney");
    this.listCategory.add("First name");
    this.listCategory.add("Food");
    this.listCategory.add("Games");
    this.listCategory.add("General");
    this.listCategory.add("Music bands");
    this.listCategory.add("Mountains");
    this.listCategory.add("Movies");
    this.listCategory.add("Rivers");
    this.listCategory.add("Sights");

    this.addWindowListener(this.hangmanWindowListener);
    this.btnExit.addActionListener(this.hangmanActionListener);
    this.btnAbout.addActionListener(this.hangmanActionListener);
    this.newGame.addActionListener(this.hangmanActionListener);
    this.exit.addActionListener(this.hangmanActionListener);
    this.about.addActionListener(this.hangmanActionListener);
    this.instr.addActionListener(this.hangmanActionListener);
    this.btnaddCategory.addActionListener(this.hangmanActionListener);
    this.btnremoveCategory.addActionListener(this.hangmanActionListener);

    this.btnPlay.setFont(new Font("", Font.BOLD, 12));
    this.lblCategory.setFont(new Font("", Font.BOLD, 12));
    this.lblLevel.setFont(new Font("", Font.BOLD, 12));
    this.lblname.setFont(new Font("", Font.BOLD, 12));

    this.mainbar.add(this.game);
    this.mainbar.add(this.options);
    this.mainbar.add(this.help);
    setMenuBar(this.mainbar);

    this.game.add(this.newGame);
    this.game.add(this.rank);
    this.game.addSeparator();
    this.game.add(this.exit);
    this.options.add(this.importwc);
    this.importwc.add(this.importWord);
    this.importwc.add(this.importCategory);
    this.options.add(this.changeColor);
    this.help.add(this.instr);
    this.help.add(this.about);

    this.pnlNorthRow1.add(this.lblname);
    this.pnlNorthRow1.add(this.tfname);
    this.pnlNorthRow2.add(this.lblLevel);
    this.pnlNorthRow2.add(this.easy);
    this.pnlNorthRow2.add(this.normal);
    this.pnlNorthRow2.add(this.difficult);

    this.pnlCenterCol1.add(this.txtAreaInfo);
    this.pnlCenterCol2a.add(this.listCategory);
    this.pnlCenterCol2b.add(this.btnaddCategory);
    this.pnlCenterCol2b.add(this.btnremoveCategory);
    this.pnlCenterCol2c.add(this.selCategory);
    this.pnlCenterColx2.add(this.lblCategory);

    this.pnlSouth.add(this.btnExit);
    this.pnlSouth.add(this.btnAbout);
    this.pnlSouth.add(this.btnPlay);

    this.add(this.pnlNorth, BorderLayout.NORTH);
    this.add(this.pnlCenter, BorderLayout.CENTER);
    this.add(this.pnlSouth, BorderLayout.SOUTH);

    this.pnlNorth.add(this.pnlNorthRow1);
    this.pnlNorth.add(this.pnlNorthRow2);
    this.pnlCenter.add(this.pnlCenterCol1);
    this.pnlCenter.add(this.pnlCenterCol2);
    this.pnlCenterCol2.add(this.pnlCenterColx2, BorderLayout.NORTH);
    this.pnlCenterCol2.add(this.pnlCenterCol2a, BorderLayout.WEST);
    this.pnlCenterCol2.add(this.pnlCenterCol2b, BorderLayout.CENTER);
    this.pnlCenterCol2.add(this.pnlCenterCol2c, BorderLayout.EAST);

    this.txtAreaInfo.setEditable(false);
    this.setVisible(true);
  }
  public static void main(String[] args)
  {
    @SuppressWarnings("unused")
    Hangman w = new Hangman();
  }
}

HangmanInfo.java
Java:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

@SuppressWarnings("serial")
public class HangmanInfo extends Frame implements WindowListener
{

  public TextArea     txtArea     = new TextArea();
  public Button       btnBack     = new Button("Back to Game");
  private final Panel panelCenter = new Panel(new BorderLayout());
  private final Panel panelSouth  = new Panel(new BorderLayout());

  public HangmanInfo()
  {
    addWindowListener(new WindowAdapter()
    {
      @Override
      public void windowClosing(WindowEvent e)
      {
        setVisible(false);
      }
    });
    this.setSize(400, 300);
    this.panelSouth.add(this.btnBack);
    this.panelCenter.add(this.txtArea);
    this.txtArea.setEditable(false);
    this.add(this.panelCenter, BorderLayout.CENTER);
    this.add(this.panelSouth, BorderLayout.SOUTH);

    this.btnBack.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent arg0)
      {
        setVisible(false);
      }
    });
  }

  @Override
  public void windowOpened(WindowEvent e)
  {
  }

  @Override
  public void windowClosing(WindowEvent e)
  {
  }

  @Override
  public void windowClosed(WindowEvent e)
  {
  }

  @Override
  public void windowIconified(WindowEvent e)
  {
  }

  @Override
  public void windowDeiconified(WindowEvent e)
  {
  }

  @Override
  public void windowActivated(WindowEvent e)
  {
  }

  @Override
  public void windowDeactivated(WindowEvent e)
  {
  }
}

HangmanActionListener.java
Java:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HangmanActionListener implements ActionListener
{
  public Hangman h;

  public HangmanActionListener(Hangman h)
  {
    this.h = h;
  }

  @Override
  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == this.h.newGame)
    {
      System.out.println("new Game");;
    }

    else if (e.getSource() == this.h.btnExit || e.getSource() == this.h.exit)
    {
      System.exit(0);
    }

    else if (e.getSource() == this.h.about || e.getSource() == this.h.btnAbout)
    {
      HangmanInfo a = new HangmanInfo();
      a.setTitle("About Hangman");
      a.txtArea.setText("About....");
      a.setLocationRelativeTo(null);
      a.setVisible(true);
    }

    else if (e.getSource() == this.h.instr)
    {
      HangmanInfo b = new HangmanInfo();
      b.setTitle("Hangman Instructions");
      b.txtArea.setText("Instructions....");
      b.setLocationRelativeTo(null);
      b.setVisible(true);
    }
    else if (e.getSource() == this.h.btnaddCategory)
    {
      String array[] = this.h.listCategory.getSelectedItems();
      for (int i = 0; i < array.length; i++)
      {
        this.h.selCategory.append(array[i] + "\n");
      }
      this.h.setVisible(true);
    }
  }
}

HangmanWindowListener.java
Java:
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class HangmanWindowListener implements WindowListener
{

  @Override
  public void windowOpened(WindowEvent e)
  {
    // TODO Auto-generated method stub

  }

  @Override
  public void windowClosing(WindowEvent e)
  {
    // TODO Auto-generated method stub
    System.exit(0);
  }

  @Override
  public void windowClosed(WindowEvent e)
  {
    // TODO Auto-generated method stub

  }

  @Override
  public void windowIconified(WindowEvent e)
  {
    // TODO Auto-generated method stub

  }

  @Override
  public void windowDeiconified(WindowEvent e)
  {
    // TODO Auto-generated method stub

  }

  @Override
  public void windowActivated(WindowEvent e)
  {
    // TODO Auto-generated method stub

  }

  @Override
  public void windowDeactivated(WindowEvent e)
  {
    // TODO Auto-generated method stub

  }

}

HangmanPlay.java (Nur zur VERANSCHAULICHUNG)
Java:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;

@SuppressWarnings("serial")
public class HangmanPlay extends Frame
{
  private final HangmanWindowListener hangmanWindowListener = new HangmanWindowListener();
  private final Panel                 pnlNorth              = new Panel();
  private final Panel                 pnlSouth              = new Panel(new GridLayout(2, 1));
  private final Panel                 pnlSouthA             = new Panel(new FlowLayout(
                                                                FlowLayout.CENTER));
  private final Panel                 pnlSouthB             = new Panel(new FlowLayout(
                                                                FlowLayout.CENTER));

  public Label                        lblText               = new Label(
                                                                "Hier soll später das Programm laufen");

  public Button                       btnA                  = new Button("A");
  public Button                       btnB                  = new Button("B");
  public Button                       btnC                  = new Button("C");
  public Button                       btnD                  = new Button("D");
  public Button                       btnE                  = new Button("E");
  public Button                       btnF                  = new Button("F");
  public Button                       btnG                  = new Button("G");
  public Button                       btnH                  = new Button("H");
  public Button                       btnI                  = new Button("I");
  public Button                       btnJ                  = new Button("J");
  public Button                       btnK                  = new Button("K");
  public Button                       btnL                  = new Button("L");
  public Button                       btnM                  = new Button("M");
  public Button                       btnN                  = new Button("N");
  public Button                       btnO                  = new Button("O");
  public Button                       btnP                  = new Button("P");
  public Button                       btnQ                  = new Button("Q");
  public Button                       btnR                  = new Button("R");
  public Button                       btnS                  = new Button("S");
  public Button                       btnT                  = new Button("T");
  public Button                       btnU                  = new Button("U");
  public Button                       btnV                  = new Button("V");
  public Button                       btnW                  = new Button("W");
  public Button                       btnX                  = new Button("X");
  public Button                       btnY                  = new Button("Y");
  public Button                       btnZ                  = new Button("Z");

  public HangmanPlay()
  {
    this.setTitle("Hangman");
    this.setSize(700, 400);
    this.setLocationRelativeTo(null);
    this.addWindowListener(this.hangmanWindowListener);
    this.setVisible(true);

    this.pnlNorth.add(this.lblText);
    this.pnlSouthA.add(this.btnA);
    this.pnlSouthA.add(this.btnB);
    this.pnlSouthA.add(this.btnC);
    this.pnlSouthA.add(this.btnD);
    this.pnlSouthA.add(this.btnE);
    this.pnlSouthA.add(this.btnF);
    this.pnlSouthA.add(this.btnG);
    this.pnlSouthA.add(this.btnH);
    this.pnlSouthA.add(this.btnI);
    this.pnlSouthA.add(this.btnJ);
    this.pnlSouthA.add(this.btnK);
    this.pnlSouthA.add(this.btnL);
    this.pnlSouthA.add(this.btnM);
    this.pnlSouthB.add(this.btnN);
    this.pnlSouthB.add(this.btnO);
    this.pnlSouthB.add(this.btnP);
    this.pnlSouthB.add(this.btnQ);
    this.pnlSouthB.add(this.btnR);
    this.pnlSouthB.add(this.btnS);
    this.pnlSouthB.add(this.btnT);
    this.pnlSouthB.add(this.btnU);
    this.pnlSouthB.add(this.btnV);
    this.pnlSouthB.add(this.btnW);
    this.pnlSouthB.add(this.btnX);
    this.pnlSouthB.add(this.btnY);
    this.pnlSouthB.add(this.btnZ);

    this.add(this.pnlNorth);
    this.pnlSouth.add(this.pnlSouthA);
    this.pnlSouth.add(this.pnlSouthB);
    this.add(this.pnlSouth, BorderLayout.SOUTH);
  }

  public static void main(String args[])
  {
    @SuppressWarnings("unused")
    HangmanPlay f = new HangmanPlay();
  }
}
 
Kann niemand helfen?, es geht nur darum ob es üblich ist, so viele Panels mit CardLayouts zu verwalten? oder andere Möglichkeiten eleganter sind.

IchLerneJava
 
Sicherlich gibt es einfachere Wege. Beispielsweise verwalte die sichbaren / nicht sichtbaren Teile in einer Liste und stelle diese je nach Zustand im OnPaint eines Panels dar
 

Zurück
Oben