我创建菜单再给窗体加入面板,菜单就看不见了,而J面板就可以看见,不过它们都接受不到键盘事件???我式了n个机子我家的单位的都一样..帮我看看那里错误 package myprojects.edacioussnake;
import java.awt.*; import java.awt.event.*; import javax.swing.*;
class EdaciousSnakeFrom extends JFrame { Container c=getContentPane(); RoomMap DomePanel=new RoomMap(); JMenuBar JWnMnuBar=new JMenuBar(); JMenu MainMunBtn[]=new JMenu[3]; String MainMunBtnStrings[]={"Operation","Seting","Help"}; JMenuItem OperationBtn[]=new JMenuItem[4]; int OperationModeID=2;//0~2 String OperationModeStrings[]={"continue","Step"}; String OperationBtnStrings[]={"Start","#Mode","Stop","-","Exit"}; JMenu SetLevelBtn=new JMenu("LeveL"); JRadioButtonMenuItem LvNumber[]=new JRadioButtonMenuItem[4]; JMenuItem HelpBtn=new JMenuItem("Help"); ButtonGroup grop=new ButtonGroup(); public EdaciousSnakeFrom() { int i; boolean enabled=true; int index=0; for(i=0;i<MainMunBtn.length;i++) MainMunBtn[i]=new JMenu(MainMunBtnStrings[i]); for(i=0;i<OperationBtn.length;i++){ if(OperationBtnStrings[i]=="-"){ MainMunBtn[0].addSeparator(); index++; } else if(OperationBtnStrings[i]=="#Mode"){ if(OperationModeID==0) OperationBtnStrings[i]=OperationModeStrings[OperationModeID]; else if(OperationModeID==1) OperationBtnStrings[i]=OperationModeStrings[OperationModeID]; else if(OperationModeID==2) enabled=false; }
OperationBtn[i]=new JMenuItem(OperationBtnStrings[index]); OperationBtn[i].setEnabled(enabled); enabled=true; MainMunBtn[0].add(OperationBtn[i]); index++; } for(i=0;i<LvNumber.length;i++){ LvNumber[i]=new JRadioButtonMenuItem("Lv." + (i+1)); grop.add(LvNumber[i]); SetLevelBtn.add(LvNumber[i]); } MainMunBtn[1].add(SetLevelBtn); MainMunBtn[2].add(HelpBtn); for(i=0;i<MainMunBtn.length;i++){ JWnMnuBar.add(MainMunBtn[i]); } setJMenuBar(JWnMnuBar); c.add(DomePanel,BorderLayout.CENTER); setSize(300,500); show(); }
public static void main(String args[]) { new EdaciousSnakeFrom(); } }
class RoomMap extends Panel{ Snake OLaSnake=new Snake(); KeyEventer KeyOperation=new KeyEventer(); public RoomMap(){ addKeyListener(KeyOperation); } public void paint(Graphics g){ } class KeyEventer extends KeyAdapter{ public void KeyPressed(KeyEvent e){ System.out.println(e.getKeyCode()); }
} }
class Snake{ public int definition; public int speed; public int state; private int orientation private void getPoint(int num){ } public void round(int orientation){ } public void steer(){ }
}