当前位置: 代码迷 >> Java相关 >> [求助]高手帮帮忙
  详细解决方案

[求助]高手帮帮忙

热度:154   发布时间:2005-08-08 11:00:00.0
[求助]高手帮帮忙

我创建菜单再给窗体加入面板,菜单就看不见了,而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(){ }

}


----------------解决方案--------------------------------------------------------
我这只菜鸟看了你们高手的这些东东  哎!!!自卑啊~~~~~~~
----------------解决方案--------------------------------------------------------
所以说努力把~~java停简单的就是有点怪,我开始学的时候许多地方不解..习惯就好了
----------------解决方案--------------------------------------------------------
// try it
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Frame1 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 Frame1()
  {
    int i;
    boolean enabled=true;
    int index=0;
    for(i=0;i&lt;MainMunBtn.length;i++)
      MainMunBtn[i]=new JMenu(MainMunBtnStrings[i]);
    for(i=0;i&lt;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&lt;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&lt;MainMunBtn.length;i++)
    {
      JWnMnuBar.add(MainMunBtn[i]);
    }
    setJMenuBar(JWnMnuBar);
    c.add(DomePanel, BorderLayout.CENTER);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Test");
  }
  
  public static void main(String args[])
  {
    Frame1 frame1 = new Frame1();
    frame1.setSize(300, 500);
    frame1.setVisible(true);
  }
}

class RoomMap extends JPanel
{
  Snake OLaSnake=new Snake();
  KeyEventer KeyOperation=new KeyEventer();
  public RoomMap()
  {
    super.setBackground(Color.white);
    addKeyListener(KeyOperation);
  }
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    super.invalidate();
    super.requestFocus();
  }
  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()
  {
  }
}
// viel Spass
----------------解决方案--------------------------------------------------------
关于编程风格问题说几句:一些Java程序员将函数的起始大括号直接放在函数后面,也就是放在同一行,就像你的代码的那种风格,我认为这种风格不好,它降低了程序的可读性,特别是当程序长了以后,更无法看清块的结构,通常C/C++程序员都将大括号另起一行,并且'{''}'上下对齐,块的代码缩进两格,这样程序的结构便一目了然了。

你的那个JFrame 的Constructor 写的不好,写得太复杂了,何必用什么 for-loop, if, else 呢,
建立一个JMenu 然后建立其JMenuItem,并加入,这样不是更好吗?
通常你还要为JMenuItem 写ActionListener,这样按部就班的写,代码会长一些,但是可读性强,代码也容易写。
----------------解决方案--------------------------------------------------------
  相关解决方案