我的一个面板上有个JComboBox,然后我添加了一个监听键盘的Down键的方法,
可是我按Down的时候,那个JComboBox就变化为下拉筐里面的内容,
怎么让我键盘监听的事件执行啊? 

解决方案 »

  1.   

    用ItemListener监听器,Combo的Item改变会触发itemStateChanged
      

  2.   

    //我是加的itemlistener啊package src;
    import java.awt.*;
    import java.awt.event.*;import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import src.Diamond;
    import javax.swing.*;/**
     * @author 栖息的企鹅
     * 
     *主要面板,方块就在这里下落.
     *主要是在构造函数里面加入画出table的方法,还有一个重要方法就是判断一行满了的时候的删除工作
     *另外,这个面板还负责监听Play的时候键盘的动作.
     *
     *这个类主要是构造单机的时候界面,包括一个mainTable&中间的一些控制的部件.
     */
    public class GameTable extends Panel implements KeyListener,ActionListener,ItemListener
    {
    public int rowNum;
    public int colNum;
    public int[][] diamondTable;
    public int[][] nextDiamondTable;
    public String[] speedList={"1","2","3","4","5","6","7","8","9","10"};
    public JComboBox speedBox;
    public JLabel scoreLabel;
    public JButton startBtn;
    public JButton reStartBtn;
    /*temp变量,为画Diamond的时候需要用到*/
    public Graphics tempG;
    //public int[][] partten;

    /*在这里生成一个Diamond*/
    public Diamond diamond;
    public elsfkGame mainGame;
    public JButton tempBtn;

    public GameTable(elsfkGame mainGame)
    {
    this.mainGame=mainGame;
    rowNum=24;
    colNum=16;
    diamondTable=new int[rowNum][colNum];
    nextDiamondTable=new int[4][4];
    for(int i=0;i<rowNum;i++)
    for(int j=0;j<colNum;j++)
    diamondTable[i][j]=0;
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    nextDiamondTable[i][j]=0;
    /*初始化Diamond*/
    diamond=new Diamond(this);
    tempG=this.getGraphics();



    this.setLayout(null);

    /*画中间的一些部件.*/
    /*先画一个speedBox*/
    speedBox=new JComboBox(speedList);
    speedBox.setBackground(Color.LIGHT_GRAY);
    speedBox.addItemListener(this);
    speedBox.setBounds(303,145,60,20);
    speedBox.setEditable(false);
    this.add(speedBox);

    /*再画一个label显示分数*/
    scoreLabel=new JLabel("0.00");
    scoreLabel.setBackground(Color.LIGHT_GRAY);
    scoreLabel.setBounds(303,200,80,20);
    scoreLabel.setBackground(Color.LIGHT_GRAY);
    this.add(scoreLabel);


    /*弄两Button上去*/
    startBtn=new JButton("start");
    startBtn.addActionListener(this);
    startBtn.setBackground(Color.LIGHT_GRAY);
    startBtn.setBounds(303,250,80,20);
    this.add(startBtn);

    reStartBtn=new JButton("reStart");
    reStartBtn.setToolTipText("reStart.");
    reStartBtn.addActionListener(this);
    reStartBtn.setBackground(Color.LIGHT_GRAY);
    reStartBtn.setBounds(303,280,80,20);
    this.add(reStartBtn);

    this.addKeyListener(this);
    }

    public void paint(Graphics g)
    {
    //画格子啊
    for(int i=0;i<rowNum;i++)
    for(int j=0;j<colNum;j++)
    {
    if(diamondTable[i][j]==0)
    {
    g.setColor(Color.LIGHT_GRAY);
    g.fill3DRect(20+15*j+2,20+15*i+2,15,15,true);
    }
    else if(diamondTable[i][j]==1)
    {
    g.setColor(Color.ORANGE);
    g.fill3DRect(20+15*j+2,20+15*i+2,15,15,true);
    }
    }


    /*
     * 画next Diamond 
     * 初始点的坐标是300,50 
     * x轴上画了table后占用了260
     * 
     * */
    g.setColor(Color.RED);
    g.drawString("下一个Diamond:",300,35);
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    {
    if(nextDiamondTable[i][j]==0)
    {
    g.setColor(Color.LIGHT_GRAY);
    g.fill3DRect(300+15*j+2,50+15*i+2,15,15,true);
    }
    else if(nextDiamondTable[i][j]==1)
    {
    g.setColor(Color.ORANGE);
    g.fill3DRect(300+15*j+2,50+15*i+2,15,15,true);
    }
    }

    g.setColor(Color.RED);
    g.drawString("Speed:",303,140);

    speedBox.repaint();
    scoreLabel.repaint();
    startBtn.repaint();
    reStartBtn.repaint();
    }

    /*呵呵 这个方法当真能消除闪烁!!*/
    public void update(Graphics g)
    {
    paint(g);
    }

    /*绘制出一个GameTable上面的Diamond*/
    public void drawDiamond(int tableGridState)
    {
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    {
    if(tableGridState==1)
    {
    tempG.setColor(Color.ORANGE);
    tempG.fill3DRect(280+15*j+2,20+15*i+2,15,15,true);
    }
    else if(tableGridState==0)
    {
    tempG.setColor(Color.LIGHT_GRAY);
    tempG.fill3DRect(300+15*j+2,50+15*i+2,15,15,true);
    }
    }

    }

    /*绘制出GameTable上面的下一个要运行的Diamond*/
    public void drawNextDiamond(int tableGridState)
    {
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    {
    if(tableGridState==1)
    {
    tempG.setColor(Color.ORANGE);
    tempG.draw3DRect(88+j*15+2,20+15*i+2,15,15,true);
    }
    else if(tableGridState==0)
    {
    tempG.setColor(Color.LIGHT_GRAY);
    tempG.draw3DRect(88+j*15+2,20+15*i+2,15,15,true);
    }
    }


    }

    /*按了开始的按钮的时候,程序就在这里的到执行*/
    public void actionPerformed(ActionEvent e)
    {
    /*测试是否能正常工作*/
    //JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
        if(e.getSource()==startBtn)
        {
         tempBtn=(JButton)e.getSource();
         if(tempBtn.getText()=="start")
         {
         startBtn.setText("pause");
         speedBox.setEditable(false);
         mainGame.refreshThread.resume();
         }
         else if(tempBtn.getText()=="pause")
         {
         startBtn.setText("start");
         speedBox.setEditable(false);
         mainGame.refreshThread.suspend();
         }
        
        }
        else if(e.getSource()==reStartBtn)
        {
        
        }

       
    }
    public void keyPressed(KeyEvent e)
    {
    JOptionPane.showConfirmDialog(null, "choose twwwww", "choose twwwww", JOptionPane.YES_NO_OPTION);
    switch(e.getKeyCode())
    {
    case KeyEvent.VK_LEFT:diamond.leftMove();break;
    case KeyEvent.VK_RIGHT:diamond.rightMove();break;
    case KeyEvent.VK_DOWN:JOptionPane.showConfirmDialog(null, "choose 1", "choose 1", JOptionPane.YES_NO_OPTION);break;
    }

    }
    public void keyReleased(KeyEvent e)
    {
    //null
    }
    public void keyTyped(KeyEvent e)
    {
    //null
    }

    public void itemStateChanged(ItemEvent e)
    {
    JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
    } }
      

  3.   

    呵呵 问题解决,可惜不能给我自己加分
    csdn程序做的还不够人性化.