Hi,
ich bin relativ neu in Java und die Suche gab bisher nichts was mir weitergeholfen hat.
Ich möchte in meiner Anwendung ein JPanel neu erstellen, wenn ich in meinem JMenu ein bestimmtes JMenuItem anklicke.
Der normale Aufruf der Methode im Konstruktor funktioniert.
Der Aufruf über das ActionEvent nicht. (Aufruf Z. 17 und 49)
Hilfe!
ich bin relativ neu in Java und die Suche gab bisher nichts was mir weitergeholfen hat.
Ich möchte in meiner Anwendung ein JPanel neu erstellen, wenn ich in meinem JMenu ein bestimmtes JMenuItem anklicke.
Der normale Aufruf der Methode im Konstruktor funktioniert.
Der Aufruf über das ActionEvent nicht. (Aufruf Z. 17 und 49)
Hilfe!
Java:
public class MemoryGUI extends JFrame implements GUIListener{
//attributes
private MemoryGame memoryGame;
private java.util.List<ViewComponent> componentList = new ArrayList<ViewComponent>();
private JPanel cardPanel = new JPanel();
private GridLayout layout = new GridLayout(1,1);
private ImageSet imageSet = new ImageSetBunny();
private java.util.List<File> pictureList = new ArrayList<File>();
private JMenuBar menubar;
private boolean gameOver = false;
//constructors
public MemoryGUI()
{
initComponents();
newMemoryGame();
}
//methods
private void initComponents()
{
//init variables
JMenu menu;
JMenuItem menuItem;
//Frame
this.setSize(300,300);
this.setResizable(false);
this.setLocation(GUItools.getCenteredPoint(this.getSize().height, this.getSize().width));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
//Menubar
menubar = new JMenuBar();
//Build the first menu.
menu = new JMenu("Spiel");
menu.setMnemonic(KeyEvent.VK_A);
menubar.add(menu);
//Neues Spiel
menuItem = new JMenuItem("Neues Memory",
KeyEvent.VK_T);
menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
newMemoryGame();
}
});
menu.add(menuItem);
//Beenden
menuItem = new JMenuItem("Beenden",
KeyEvent.VK_T);
menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
menu.add(menuItem);
//Adding
this.add(BorderLayout.CENTER, cardPanel);
this.add(BorderLayout.NORTH, menubar);
}
private void newMemoryGame()
{
componentList.clear();
pictureList.clear();
cardPanel.removeAll();
memoryGame = new MemoryGame(8);
createCardComponents();
gameOver = false;
}
private void createCardComponents()
{
int countCardsSet = 0;
int countSpaceLeft = 1;
int rows = 1;
int columns = 1;
File picture = new File( imageSet.ImagePathStr() + imageSet.cardBackImageStr() + imageSet.pictureFormat());
pictureList.add(picture);
layout = new GridLayout(columns, rows);
cardPanel.setLayout(layout);
for(int i = 0; i < memoryGame.getCardList().size(); i++)
{
countSpaceLeft = (rows * columns - countCardsSet);
if(countSpaceLeft == 0)
{
if(rows == columns) rows++;
else columns++;
layout = new GridLayout(columns, rows);
cardPanel.setLayout(layout);
}
ViewComponent viewComponent = new ViewComponent();
viewComponent.setID(i);
viewComponent.setImage( pictureList.get(0) );
viewComponent.setGUIListener(this);
componentList.add(viewComponent);
cardPanel.add( viewComponent );
countCardsSet++;
picture = new File( imageSet.ImagePathStr() + imageSet.cardFrontImageStr() + i + imageSet.pictureFormat());
pictureList.add(picture);
}
//cardPanel.setSize(rows * imageSet.pictureWidth(), columns * imageSet.pictureHeight() + 29);
this.setSize(rows * imageSet.pictureWidth(), columns * imageSet.pictureHeight() + 54);
this.setLocation(GUItools.getCenteredPoint(this.getSize().height, this.getSize().width));
this.repaint();
}
//listener methods
@Override
public void MouseClicked(ViewComponent sender) {
if(gameOver == false)
{
Card card = memoryGame.getCardList().get(sender.getID());
memoryGame.cardChosen(this, card);
}
}
@Override
public void CardFlipped(Card card) {
if(card.isFaceUp())
componentList.get(card.getID()).setImage(pictureList.get(card.getPictureID() + 1));
else
componentList.get(card.getID()).setImage(pictureList.get(0));
}
@Override
public void GameOver() {
gameOver = true;
}
}