zmazat prosim temu, diky
Příspěvky odeslané z IP adresy 178.40.197.–
Pokusam sa spravit ku hre Sokoban menu. Menu mam hotove, aj ide .. cize ked kliknem na New tak sa mi prida panel s hrou a zmizne panel s Menu .. cize akoby sa to prekreslilo v jednom okne. Lenze tato hra ktora sa spusti z menu mi nereaguje na klavesy a neviem preco. Ked vsak menu dam prec, zmazem ho, cize nevytvaram vobec ani objekt Menu tak mi hra na klavesy reaguje a vsetko funguje. Ako by sa to dalo vyriesit aby to reagovalo aj ked tam mam to menu predtym ? Prikladam casti kodu, ktore s tym suvisia:
Trieda sokoban:
package sokoban;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public final class Sokoban extends JFrame {
private Menu menu;
private Board board;
private static JFrame myFrame;
public Sokoban() {
InitUI();
}
public void InitUI() {
myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(800,600);
myFrame.setLocationRelativeTo(null);
myFrame.setTitle("Labyrint");
myFrame.setResizable(false);
board = new Board();
menu = new Menu();
myFrame.add(menu);
//-----------
JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenuItem newItem = new JMenuItem("New");
newItem.setMnemonic('N');
file.add(newItem);
JMenuItem openItem = new JMenuItem("Open");
openItem.setMnemonic('O');
file.add(openItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');
file.add(exitItem);
//adding action listener to menu items
newItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
menu.setVisible(false);
menu = null;
myFrame.add(board);
}
}
);
openItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Open is pressed");
}
}
);
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Exit is pressed");
}
}
);
JMenuBar bar = new JMenuBar();
myFrame.setJMenuBar(bar);
bar.add(file);
myFrame.getContentPane();
myFrame.setVisible(true);
///------------------------------------
}
public static void main(String[] args) {
Sokoban sokoban = new Sokoban();
}
}
A toto je cast triedy kde je vykreslovana hra, Board class ... funguju klavesy len ak nie je menu:
public class Board extends JPanel implements KeyListener {
public Board() {
addKeyListener(this);
setFocusable(true);
initWorld();
}
/* not importnant code for this problem here*/
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
if ((checkWallCollision(soko,LEFT_COLLISION)) == true) {
return;
}
if (checkBagCollision(LEFT_COLLISION)) {
return;
}
soko.move(-SPACE, 0);
score++;
celkove_score++;
} else if (key == KeyEvent.VK_RIGHT) {
etc.
.
.
.
}
Pomozte prosim ak viete ako by som to mohol poriesit aby som mal aj menu aj hru, ktora bude reagovat na klavesy. Dakujem