2017-06-04 8 views
-1

飛行機をボード全体に移動したいです。私は「私は(真)setFocusableまし 前にそれをやったので、 これは非常に単純である必要があり、私はのKeyListenerを追加しました、と私は私が何か分からないKeyListenerが応答しません

)(requestFocusInWindowもしまし間違っ
キーが押され、それも私が「こんにちは」プリントアウトしようとすることでテスト
KeyPressed方法を入力しないであろう。

package game; 

    import java.awt.Graphics; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 

    import javax.swing.JFrame; 

    public class GUI{ 

     /* Author: Carl Zhang 
     * Date: 
     * 
     */ 

     //player's username 
     public static String user; 

     //classes 
     public static GUI gui; 
     public static LoginPanel lp; 
     public static MenuPanel mp; 
     public static GamePanel gp; 
     public static HighscorePanel hp; 

     public static InstructionPanel ip; 

     //dimensions 
     private static final int WIDTH = 500; 
     private static final int HEIGHT = 500; 

     public GUI() { 


     JFrame frame = new JFrame("ZCraft"); 
      frame.setSize(WIDTH, HEIGHT); 
      frame.setResizable(false); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setLocationRelativeTo(null); 

     //adding JPanels 
     lp = new LoginPanel(); 
     frame.add(lp); 

     mp = new MenuPanel(); 
     frame.add(mp); 

     gp = new GamePanel(); 
     frame.add(gp); 

     ip = new InstructionPanel(); 
     frame.add(ip); 

     hp = new HighscorePanel(); 
     frame.add(hp); 

     frame.setVisible(true); 

    }//end constructor GUI() 

    public static int getWidth() { 
     return WIDTH; 

    }//end getWIDTH() 

    public static int getHeight() { 
     return HEIGHT; 

    }//end getHEIGHT 

    public static void main(String[] args) { 
     gui = new GUI(); 

    }//end main 

}//end class GUI 

     package game; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class GamePanel extends JPanel implements ActionListener, KeyListener { 

    public static GamePanel gp; 

    private boolean up = false; 
    private boolean right = false; 
    private boolean down = false; 
    private boolean left = false; 

    private ZCraft player; 

    private JButton pause, back; 

    public GamePanel(){ 
     //timer 
     Timer timer = new Timer(5, this); 

     setSize(GUI.getWidth(), GUI.getHeight()); 
     setBackground(Color.BLACK); 
     setLayout(null); 
     setVisible(false); 

     /* 

     pause = new JButton("Pause"); 
     pause.setHorizontalAlignment(SwingConstants.CENTER); 
     pause.setBounds(); 
     pause.setBackground(Color.GREEN); 
     pause.setForeground(Color.RED); 
     pause.setOpaque(true); 
     pause.setBorderPainted(false); 
     pause.addActionListener(this); 
     add(pause); 

     back = new JButton("Back"); 
     back.setHorizontalAlignment(SwingConstants.CENTER); 
     back.setBounds(); 
     back.setBackground(Color.GREEN); 
     back.setForeground(Color.RED); 
     back.setOpaque(true); 
     back.setBorderPainted(false); 
     back.addActionListener(this); 
     add(back); 

     */ 

     start(); 

     timer.start(); 

     setFocusable(true); 
     addKeyListener(this); 
     requestFocusInWindow(); 
     //setFocusTraversalKeysEnabled(false); 
     //requestFocus(); 

    }//end constructor GamePanel() 

    public void start() { 
     player = new ZCraft(GUI.user); 

    }//end start() 

    public void update() { 
     if(up) { 
      player.moveYAxis(true); 

     }else { 
      player.moveYAxis(false); 

     } 

     if(right) { 
      player.moveXAxis(true); 

     }else { 
      player.moveXAxis(false); 

     } 

    }//end update() 

    public void render(Graphics g) { 
     player.render(g); 

    }//end render(Graphics g) 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     render(g); 

    }//end paintComponent(Graphics g) 

    @Override 
    public void actionPerformed(ActionEvent e) {  
     update(); 
     repaint(); 

    }//end actionPerformed(ActionEvent e) 

    @Override 
    public void keyTyped(KeyEvent e) {}//end keyTyped(KeyEvent e) 

    @Override 
    public void keyPressed(KeyEvent e) { 
     int code = e.getKeyCode(); 

     if(code == KeyEvent.VK_UP) { 
      up = true; 

     } 

     if(code == KeyEvent.VK_RIGHT) { 
      right = true; 

     } 

     if(code == KeyEvent.VK_DOWN) { 
      down = true; 

     } 

     if(code == KeyEvent.VK_LEFT) { 
      left = true; 

     } 

     if(code == KeyEvent.VK_SPACE) { 
      //player.shoot(); 

     } 

     if(code == KeyEvent.VK_1) { 

     } 

    }//end keyPressed(KeyEvent e) 

    @Override 
    public void keyReleased(KeyEvent e) { 
     int code = e.getKeyCode(); 

     if(code == KeyEvent.VK_UP) { 
      up = false; 

     } 

     if(code == KeyEvent.VK_RIGHT) { 
      right = false; 

     } 

     if(code == KeyEvent.VK_DOWN) { 
      down = false; 

     } 

     if(code == KeyEvent.VK_LEFT) { 
      left = false; 

     } 

     if(code == KeyEvent.VK_SPACE) { 
      //player.shoot(); 

     } 

    }//end keyReleased(KeyEvent e) 

}//end class GamePanel 

を私はちょうど方法を入力したい、私は後で移動の事を変更します

+0

助けてください!これは私の合計です –

+0

いいえ[mcve]、なぜですか?コードを実行してテストするにはどうすればよいですか? Key Bindingsの使用を推奨する類似の質問とはどのように違いますか? –

答えて

1

あなたはig JFrameで使用されているレイアウトマネージャがありません。 JFrameのcontentPaneはBorderLayoutを使用しています。これに複数のコンポーネントを追加すると、以前に追加されたコンポーネントがカバーされるため、GamePanelのフォーカスがあまりなくなっていません。

ベストソリューション:

  • を学び、よく
  • 使用Key Bindings、ないKeyListenersをレイアウトマネージャを使用しています。
  • ご質問の際には有効なMCVEをご記入ください。問題を示すために必要なコードのみを含む小型で完全なプログラムで、コピー、貼り付け、コンパイル、および実行が可能です。あなたが間違っていることを完全に理解する。唯一のハイスコアのJPanelを示し

    import java.awt.Color; 
    import java.awt.Graphics; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.KeyEvent; 
    import java.awt.event.KeyListener; 
    
    import javax.swing.BorderFactory; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
    import javax.swing.Timer; 
    
    public class GUI { 
        public static String user; 
    
        public static GUI gui; 
        public static LoginPanel lp; 
        public static MenuPanel mp; 
        public static GamePanel gp; 
        public static HighscorePanel hp; 
    
        public static InstructionPanel ip; 
        private static final int WIDTH = 500; 
        private static final int HEIGHT = 500; 
    
        public GUI() { 
    
         JFrame frame = new JFrame("ZCraft"); 
         frame.setSize(WIDTH, HEIGHT); 
         frame.setResizable(false); 
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         frame.setLocationRelativeTo(null); 
         lp = new LoginPanel(); 
         frame.add(lp); 
    
         mp = new MenuPanel(); 
         frame.add(mp); 
    
         gp = new GamePanel(); 
         frame.add(gp); 
    
         ip = new InstructionPanel(); 
         frame.add(ip); 
    
         hp = new HighscorePanel(); 
         frame.add(hp); 
    
         frame.setVisible(true); 
    
        } 
    
        public static int getWidth() { 
         return WIDTH; 
    
        } 
    
        public static int getHeight() { 
         return HEIGHT; 
    
        } 
    
        public static void main(String[] args) { 
         gui = new GUI(); 
    
        } 
    
    } 
    
    class GamePanel extends JPanel implements ActionListener, KeyListener { 
    
        public static GamePanel gp; 
    
        private boolean up = false; 
        private boolean right = false; 
        private boolean down = false; 
        private boolean left = false; 
    
        private ZCraft player; 
    
        private JButton pause, back; 
    
        public GamePanel() { 
         setBorder(BorderFactory.createTitledBorder("GamePanel")); // !! 
         Timer timer = new Timer(5, this); 
    
         setSize(GUI.getWidth(), GUI.getHeight()); 
         setBackground(Color.BLACK); 
         setLayout(null); 
         setVisible(false); 
    
         start(); 
    
         timer.start(); 
    
         setFocusable(true); 
         addKeyListener(this); 
         requestFocusInWindow(); 
    
        } 
    
        public void start() { 
         player = new ZCraft(GUI.user); 
    
        } 
    
        public void update() { 
         if (up) { 
          player.moveYAxis(true); 
    
         } else { 
          player.moveYAxis(false); 
    
         } 
    
         if (right) { 
          player.moveXAxis(true); 
    
         } else { 
          player.moveXAxis(false); 
    
         } 
    
        } 
    
        public void render(Graphics g) { 
         player.render(g); 
    
        } 
    
        protected void paintComponent(Graphics g) { 
         super.paintComponent(g); 
         render(g); 
    
        } 
    
        @Override 
        public void actionPerformed(ActionEvent e) { 
         update(); 
         repaint(); 
    
        } 
    
        @Override 
        public void keyTyped(KeyEvent e) { 
        } 
    
        @Override 
        public void keyPressed(KeyEvent e) { 
         System.out.println("keypressed: " + e); 
         int code = e.getKeyCode(); 
    
         if (code == KeyEvent.VK_UP) { 
          up = true; 
    
         } 
    
         if (code == KeyEvent.VK_RIGHT) { 
          right = true; 
    
         } 
    
         if (code == KeyEvent.VK_DOWN) { 
          down = true; 
    
         } 
    
         if (code == KeyEvent.VK_LEFT) { 
          left = true; 
    
         } 
    
         if (code == KeyEvent.VK_SPACE) { 
    
         } 
    
         if (code == KeyEvent.VK_1) { 
    
         } 
    
        } 
    
        @Override 
        public void keyReleased(KeyEvent e) { 
         int code = e.getKeyCode(); 
    
         if (code == KeyEvent.VK_UP) { 
          up = false; 
    
         } 
    
         if (code == KeyEvent.VK_RIGHT) { 
          right = false; 
    
         } 
    
         if (code == KeyEvent.VK_DOWN) { 
          down = false; 
    
         } 
    
         if (code == KeyEvent.VK_LEFT) { 
          left = false; 
    
         } 
    
         if (code == KeyEvent.VK_SPACE) { 
    
         } 
    
        } 
    
    } 
    
    class LoginPanel extends MyJPanel { 
    
    } 
    
    class MenuPanel extends MyJPanel { 
    
    } 
    
    class HighscorePanel extends MyJPanel { 
    
    } 
    
    class InstructionPanel extends MyJPanel { 
    
    } 
    
    class MyJPanel extends JPanel { 
        public MyJPanel() { 
         setBorder(BorderFactory.createTitledBorder(getClass().getSimpleName())); 
        } 
    } 
    
    class ZCraft { 
    
        public ZCraft(String user) { 
        } 
    
        public void render(Graphics g) { 
    
        } 
    
        public void moveXAxis(boolean b) { 
    
        } 
    
        public void moveYAxis(boolean b) { 
    
        } 
    
    } 
    

    注:

マイMCVEは、あなたのコードをテストします。


その他の無関係な問題:

  • あなたは過剰使用がより困難あなたのコードをテストし、コードを強化すること、static修飾子をひどくしています。
  • あなたはnullのレイアウトを使用しています。これは非常に危険なことです.1つは、プログラムのアップグレード/更新が困難です。

例えば:

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class GUI2 extends JPanel { 
    private static final int PREF_W = 500; 
    private static final int PREF_H = PREF_W;  
    private LoginPanel lp = new LoginPanel(); 
    private MenuPanel mp = new MenuPanel(); 
    private MyGamePanel gp = new MyGamePanel(); 
    private HighscorePanel hp= new HighscorePanel(); 
    private InstructionPanel ip = new InstructionPanel(); 
    private JPanel[] panels = {lp, mp, gp, hp, ip}; 
    private DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>(); 
    private JComboBox<String> panelsCombo = new JComboBox<>(comboModel); 
    private CardLayout cardLayout = new CardLayout(); 
    private JPanel cardHolderPanel = new JPanel(cardLayout); 

    public GUI2() { 
     for (JPanel panel : panels) { 
      String key = panel.getClass().getSimpleName(); 
      cardHolderPanel.add(panel, key); 
      comboModel.addElement(key); 
     } 

     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(panelsCombo);   

     panelsCombo.setFocusable(false); 
     panelsCombo.addActionListener(e -> { 
      String selection = (String) panelsCombo.getSelectedItem(); 
      cardLayout.show(cardHolderPanel, selection); 
     }); 

     setLayout(new BorderLayout()); 
     add(cardHolderPanel, BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.PAGE_END); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
      return super.getPreferredSize(); 
     } 
     return new Dimension(PREF_W, PREF_H); 
    } 


    private static void createAndShowGui() { 
     GUI2 mainPanel = new GUI2(); 

     JFrame frame = new JFrame("GUI2"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

@SuppressWarnings("serial") 
class MyGamePanel extends MyJPanel { 
    public MyGamePanel() { 
     setKeyBindings(); 
    } 

    private void setKeyBindings() { 
     int condition = WHEN_IN_FOCUSED_WINDOW; 
     InputMap inputMap = getInputMap(condition); 
     ActionMap actionMap = getActionMap(); 

     KeyStroke enterStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); 
     String enterStrokeKey = enterStroke.toString(); 

     inputMap.put(enterStroke, enterStrokeKey); 
     actionMap.put(enterStrokeKey, new AbstractAction() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("Enter pressed"); 
      } 
     }); 
     intoMap(inputMap, actionMap, Direction.UP, KeyEvent.VK_UP); 
     intoMap(inputMap, actionMap, Direction.DOWN, KeyEvent.VK_DOWN); 
     intoMap(inputMap, actionMap, Direction.LEFT, KeyEvent.VK_LEFT); 
     intoMap(inputMap, actionMap, Direction.RIGHT, KeyEvent.VK_RIGHT); 
    } 

    private void intoMap(InputMap inputMap, ActionMap actionMap, Direction dir, int keyCode) { 
     KeyStroke release = KeyStroke.getKeyStroke(keyCode, 0, true); 
     KeyStroke pressed = KeyStroke.getKeyStroke(keyCode, 0, false); 
     inputMap.put(pressed, pressed.toString()); 
     inputMap.put(release, release.toString()); 
     actionMap.put(pressed.toString(), new DirectionAction(dir, false)); 
     actionMap.put(release.toString(), new DirectionAction(dir, true)); 
    } 

    private class DirectionAction extends AbstractAction { 
     private Direction dir; 
     private boolean onRelease; 

     public DirectionAction(Direction dir, boolean onRelease) { 
      this.dir = dir; 
      this.onRelease = onRelease; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.printf("Direction: %s; On Released: %b%n", dir.toString(), onRelease); 
     } 
    } 
} 

enum Direction { 
    UP, DOWN, LEFT, RIGHT 
} 

class LoginPanel extends MyJPanel { 

} 

class MenuPanel extends MyJPanel { 

} 

class HighscorePanel extends MyJPanel { 

} 

class InstructionPanel extends MyJPanel { 

} 

@SuppressWarnings("serial") 
class MyJPanel extends JPanel { 
    public MyJPanel() { 
     setBorder(BorderFactory.createTitledBorder(getClass().getSimpleName())); 
    } 
} 

class ZCraft { 

    public ZCraft(String user) { 
    } 

    public void render(Graphics g) { 

    } 

    public void moveXAxis(boolean b) { 

    } 

    public void moveYAxis(boolean b) { 

    } 

} 
関連する問題