| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 633 人关注过本帖
标题:为什么我的程序连事件处理方法都进不了?
取消只看楼主 加入收藏
hsjjgm
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:189
注 册:2013-4-27
结帖率:88.89%
收藏
已结贴  问题点数:30 回复次数:1 
为什么我的程序连事件处理方法都进不了?
代码很长,不过基本都没啥问题,就是为啥keypress(KeyEvent e)执行不了呢?,经检测程序根本就不进去,这是为什么呢?
监听器注册,事件源,监听对象和监听方法都有了啊,请高手指教下
这程序我都写了好几遍了,每次都有各种问题

//主函数
package practise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyTank extends JFrame
{
    private Mypanel mp;
    private JMenuBar jmb;
    private JMenu jm_1;
    //开始
    private JMenuItem jmi_1;
    //暂停
    private JMenuItem jmi_2;
    //保存
    private JMenuItem jmi_3;
    //退出
    private JMenuItem jmi_4;
    //工具条
    private JToolBar jt;
    //按钮
    JButton jb1;
    JButton jb2;
   
    public static void main(String[] args)
    {
        MyTank mt = new MyTank();
    }
   
    private MyTank()
    {
        final int width = 400;
        final int height = 300;
        
//        初始化面板
        mp = new Mypanel();
        this.setContentPane(mp);
        this.addKeyListener(mp);
        mp.setLayout(new BorderLayout());
        mp.setBackground(Color.black);
        //初始化菜单栏
        jmb = new JMenuBar();
        this.setJMenuBar(jmb);
        
        //一级菜单
        jm_1 = new JMenu("菜单");
        jmb.add(jm_1);
        
        //二级菜单
        jmi_1 = new JMenuItem("开始");
        jmi_2 = new JMenuItem("暂停");
        jm_1.addSeparator();
        jmi_3 = new JMenuItem("保存");
        jmi_4 = new JMenuItem("退出");
        
        jm_1.add(jmi_1);
        jm_1.add(jmi_2);
        jm_1.add(jmi_3);
        jm_1.add(jmi_4);
        
        //工具栏
        jt = new JToolBar();
        mp.add(jt,BorderLayout.NORTH);
        jb1 = new JButton("开始");
        jb2 = new JButton("暂停");
        jt.add(jb1);
        jt.add(jb2);
        
        //定义框架属性
        this.setVisible(true);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screensize = kit.getScreenSize();
        int screenwidth = screensize.width;
        int screenheight = screensize.height;
        int x = (screenwidth-width)/2;
        int y = (screenheight-height)/2;
        this.setBounds(x,y,width,height);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

我的面板
package practise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Mypanel extends JPanel implements KeyListener
{
    //定义英雄坦克
    Hero hero;
   
    public Mypanel()
    {
        hero = new Hero(5,88);
    }
   
    //重写绘画函数
    public void paint(Graphics g)
    {
        super.paint(g);
        paintTank(hero.x,hero.y,g,hero.direct,hero.type);
        
    }
   
    private void paintTank(int x, int y, Graphics g, int direct,int type)
    {
        if(type == 1)
        {
            g.setColor(Color.yellow);
            switch(direct)
            {
            //up
            case 0:
                g.fill3DRect(x, y, 5, 30, false);
                g.fill3DRect(x+5, y+5, 10, 20, false);
                g.fill3DRect(x+15, y, 5, 30, false);
                g.fillOval(x+4,y+10,10,10);
                g.drawLine(x+9,y+15,x+9,y-4);
                break;
                //right
            case 1:
                g.fill3DRect(x, y,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x+34,y+10);
                break;
                //down
            case 2:
                g.fill3DRect(x, y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fillOval(x+4,y+10,10,10);
                g.drawLine(x+9,y+15,x+9,y+34);
                break;
                //left
            case 3:
                g.fill3DRect(x, y,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x-4,y+10);
                break;   
            }
        }
        else if(type == 2)
        {
            g.setColor(Color.red);
            switch(direct)
            {
            //up
            case 0:
                g.fill3DRect(x, y, 5, 30, false);
                g.fill3DRect(x+5, y+5, 10, 20, false);
                g.fill3DRect(x+15, y, 5, 30, false);
                g.fillOval(x+4,y+10,10,10);
                g.drawLine(x+9,y+15,x+9,y-4);
                break;
                //right
            case 1:
                g.fill3DRect(x, y,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x+34,y+10);
                break;
                //down
            case 2:
                g.fill3DRect(x, y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fillOval(x+4,y+10,10,10);
                g.drawLine(x+9,y+15,x+9,y+34);
                break;
                //left
            case 3:
                g.fill3DRect(x, y,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x-4,y+10);
                break;   
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent e)
    {
        switch(e.getKeyCode())
        {
        case KeyEvent.VK_W:
            hero.setDirect(0);
            hero.moveUp();
            break;
        case KeyEvent.VK_D:
            hero.setDirect(1);
            hero.moveRight();
            break;
        case KeyEvent.VK_S:
            hero.setDirect(2);
            hero.moveDown();
            break;
        case KeyEvent.VK_A:
            hero.setDirect(3);
            hero.moveLeft();
            break;
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void keyTyped(KeyEvent e)
    {
        // TODO Auto-generated method stub
        
    }
}

成员
package practise;

//定义坦克父类
class Tank
{
    int x, y,speed = 1,direct = 0,type = 1;
    boolean isalive = true;
    public Tank(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
   
    public int getX()
    {
        return x;
    }
    public void setX(int x)
    {
        this.x = x;
    }
    public int getY()
    {
        return y;
    }
    public void setY(int y)
    {
        this.y = y;
    }
    public int getSpeed()
    {
        return speed;
    }
    public void setSpeed(int speed)
    {
        this.speed = speed;
    }
    public int getDirect()
    {
        return direct;
    }
    public void setDirect(int direct)
    {
        this.direct = direct;
    }
    public boolean isIsalive()
    {
        return isalive;
    }
    public void setIsalive(boolean isalive)
    {
        this.isalive = isalive;
    }   
   
    //坦克移动
    //up
    public void moveUp()
    {
        y-=this.speed;
    }
   
    public void moveRight()
    {
        x+=this.speed;
    }
   
    public void moveDown()
    {
        y+=this.speed;
    }
   
    public void moveLeft()
    {
        y-=this.speed;
    }
}

class Hero extends Tank
{
    public Hero(int x, int y)
    {
        super(x,y);
    }
}

class Enermy extends Tank
{
    public Enermy(int x, int y)
    {
        super(x, y);
    }
}


搜索更多相关主题的帖子: private package 函数 public 
2013-06-03 17:18
hsjjgm
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:189
注 册:2013-4-27
收藏
得分:0 
没人指点下嘛
2013-06-03 22:58
快速回复:为什么我的程序连事件处理方法都进不了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037509 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved