java吧 关注:1,255,239贴子:12,744,103
  • 3回复贴,共1

各位大神,求帮忙

只看楼主收藏回复

小弟做了一个游戏菜单,准备通过上下左右四个按键来设置控制焦点在那个菜单选项上,无奈,keyEvent就是不能正常监听,但代码正常运行


IP属地:江西1楼2014-12-15 10:09回复
    Layout.java
    import java.awt.Rectangle;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    public class Layout extends JFrame implements FocusListener{
    JButton startButton, aboutButton, helpButton, settingButton, exitButton;
    KeyAction keyAction = new KeyAction();
    ImageIcon background = new ImageIcon(".//source/background.png");
    JLabel label = new JLabel(background);
    public Layout(){
    super("Teris version 1.000");
    this.setBounds( 60, 100, 800, 600);
    this.setTitle("Teris Version 1.00");
    this.setLayout(null);
    startButton = new JButton(new ImageIcon(Path(0)));//set startButton background
    startButton.setBounds(getRectangle(0));//set startButton size & position
    this.add(startButton);//add startButton in this panel
    startButton.addFocusListener(this);//add focus listener for startButton
    aboutButton = new JButton(new ImageIcon(Path(2)));//set aboutButton background
    aboutButton.setBounds(getRectangle(2));//set aboutButton size & position
    this.add(aboutButton);//add aboutButton in this panel
    aboutButton.addFocusListener(this);//add focus listener for startButton
    settingButton = new JButton(new ImageIcon(Path(4)));//set settingButton background
    settingButton.setBounds(getRectangle(4));//set settingButton size & position
    this.add(settingButton);//add settingButton in this panel
    settingButton.addFocusListener(this);//add focus listener for settingButton
    helpButton = new JButton(new ImageIcon(Path(6)));//set helpButton background
    helpButton.setBounds(getRectangle(6));//set helpButton size & position
    this.add(helpButton);//add helpButton in this panel
    helpButton.addFocusListener(this);//add focus listener for helpButton
    exitButton = new JButton(new ImageIcon(Path(8)));//set exitButton background
    exitButton.setBounds(getRectangle(8));//set exitButton size & position
    add(exitButton);//add exitButton in this panel
    exitButton.addFocusListener(this);//add focus listener for exitButton
    label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
    this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
    this.add(label);
    this.addKeyListener(keyAction);
    this.setVisible(true);
    this.selectMenu();
    }
    public String Path(int i){
    String path[] = {
    ".//src/startUnfocus.png",
    ".//src/startFocus.png",
    ".//src/aboutUnfocus.png",
    ".//src/aboutFocus.png",
    ".//src/settingUnfocus.png",
    ".//src/settingFocus.png",
    ".//src/helpUnfocus.png",
    ".//src/helpFocus.png",
    ".//src/exitUnfocus.png",
    ".//src/exitFocus.png"
    };
    return path[i];
    }
    //according image width & height to setting menu button position & size
    public Rectangle getRectangle(int i){
    File picture[] = new File [10];
    Rectangle pictureRectangle[] = new Rectangle[10];
    BufferedImage sourceImage[] = new BufferedImage[10];
    try{
    for(int j = 0; j < 10; j++){
    picture[j] = new File(Path(j));
    sourceImage[j] = ImageIO.read(new FileInputStream(picture[j]));
    pictureRectangle[j] = new Rectangle((this.getWidth() - sourceImage[j].getWidth()) / 2,
    (int)(j / 2) * this.getHeight() / 6 + 30, sourceImage[j].getWidth(), sourceImage[j].getHeight());
    }
    return pictureRectangle[i];
    }
    catch(IOException e){
    e.printStackTrace();
    return null;
    }
    }
    public void focusGained(FocusEvent e) {
    if(e.getSource() == startButton){
    startButton.setIcon(new ImageIcon(Path(1)));
    keyAction.menuIndex = 0;
    }
    if(e.getSource() == aboutButton){
    aboutButton.setIcon(new ImageIcon(Path(3)));
    keyAction.menuIndex = 1;
    }
    if(e.getSource() == settingButton){
    settingButton.setIcon(new ImageIcon(Path(5)));
    keyAction.menuIndex = 2;
    }
    if(e.getSource() == helpButton){
    helpButton.setIcon(new ImageIcon(Path(7)));
    keyAction.menuIndex = 3;
    }
    if(e.getSource() == exitButton){
    exitButton.setIcon(new ImageIcon(Path(9)));
    keyAction.menuIndex = 4;
    }
    System.out.println(keyAction.menuIndex);
    }
    public void focusLost(FocusEvent e) {
    if(e.getSource() == startButton){
    startButton.setIcon(new ImageIcon(Path(0)));
    }
    if(e.getSource() == aboutButton){
    aboutButton.setIcon(new ImageIcon(Path(2)));
    }
    if(e.getSource() == settingButton){
    settingButton.setIcon(new ImageIcon(Path(4)));
    }
    if(e.getSource() == helpButton){
    helpButton.setIcon(new ImageIcon(Path(6)));
    }
    if(e.getSource() == exitButton){
    exitButton.setIcon(new ImageIcon(Path(8)));
    }
    }
    public void selectMenu(){
    switch(keyAction.menuIndex){
    case 0:
    startButton.requestFocus(true);
    break;
    case 1:
    aboutButton.requestFocus(true);
    break;
    case 2:
    settingButton.requestFocus(true);
    break;
    case 3:
    helpButton.requestFocus(true);
    break;
    case 4:
    exitButton.requestFocus(true);
    break;
    }
    }
    }


    IP属地:江西2楼2014-12-15 10:10
    回复
      KeyAction.java
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      class KeyAction implements KeyListener{
      byte menuIndex = 0;
      @Override
      public void keyPressed(KeyEvent e) {
      System.out.println("KeyPressed");
      if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_LEFT){
      menuIndex --;
      if(menuIndex == -1){
      menuIndex = 4;
      }
      }
      if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_RIGHT){
      menuIndex ++;
      if(menuIndex == 5){
      menuIndex = 0;
      }
      }
      }
      @Override
      public void keyReleased(KeyEvent e) {
      System.out.println("Key Realeased");
      // TODO Auto-generated method stub
      }
      @Override
      public void keyTyped(KeyEvent e) {
      System.out.println("Key Typed");
      // TODO Auto-generated method stub
      }
      }


      IP属地:江西3楼2014-12-15 10:11
      回复
        Main.java
        public class Main {
        public static void main(String args[]){
        Layout layout = new Layout();
        KeyAction keyAction = new KeyAction();
        }
        }


        IP属地:江西4楼2014-12-15 10:12
        回复