我需要一些帮助,将menuBar添加到我的JFrame中。 我有我的Gui班:
public class Gui extends JFrame implements KeyListener {
private PanelMiniMap map;
private CardLayout cardLayout = new CardLayout();
private JPanel cards;
public Gui() {
this.setSize(1000, 800);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);// center
this.setResizable(false);
// bauen();
this.cards = new JPanel(cardLayout);
this.addListener();
this.add(cards);
this.setVisible(true);
}
public void addListener() {
this.addKeyListener(this);
}
public void addPanelZuCards(PanelSpielfeld spielfeld, PanelCharakterErschaffung charerschaffung, PanelMiniMap map) {
this.map = map;
this.cards.add(charerschaffung, "charerschaffung");
this.cards.add(spielfeld, "spielfeld");
}
public Dimension getDimension() {
return new Dimension(this.getWidth(), this.getHeight());
}
/**
* Neue Charaktererschaffung ausgeben
*
* @param charerschaffung
* Das Panel, auf dem die Oberflaeche zur Charaktererschaffung
* erstellt wird
*/
public void zeigeCharckterPanel() {
this.cardLayout.show(cards, "charerschaffung");
}
public void zeigeSpielPanel() {
this.cardLayout.show(cards, "spielfeld");
this.requestFocus();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
map.move(-1, 0);
map.repaint();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
map.move(1, 0);
map.repaint();
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
map.move(0, -1);
map.repaint();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
map.move(0, 1);
map.repaint();
}
}
}
我将我的2个JPanels(PanelSpielfeld,PanelCharakterErschaffung)添加到我的Gui中,它锁定如下:
和另一张卡,原则相同,按下" starten后显示另一张卡!"按钮:
所以,如果我将一个menuBar添加到Jframe:
public Gui() {
this.setSize(1000, 800);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);// center
this.setResizable(false);
// bauen();
this.cards = new JPanel(cardLayout);
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
menuBar = new JMenuBar();
menu = new JMenu("A Menu");
menuItem = new JMenuItem("testitem");
menu.add(menuItem);
menuItem = new JMenuItem("3214dx");
menu.add(menuItem);
menuItem = new JMenuItem("dwq213m");
menu.add(menuItem);
menuBar.add(menu);
this.add(menuBar);
this.addListener();
this.add(cards);
this.setVisible(true);
this.addWindowFocusListener(this);
}
什么都没发生,我不明白为什么。 我做错了什么或我该怎么办,在两张卡片上都有相同的MenuBar?#/ p>
修改 我改变了
this.add(menuBar)
为:
this.setJMenuBar(menuBar);
我仍然遗漏了一些东西。我是否必须为JFrame设置特定的LayoutManager?