java
1package Action; 2 3import java.awt.EventQueue; 4import java.awt.event.KeyEvent; 5 6import javax.swing.ImageIcon; 7import javax.swing.JFrame; 8import javax.swing.JLabel; 9 10public class Main { 11 12 private JFrame frame; 13 14 /** 15 * Launch the application. 16 */ 17 public boolean key_up; 18 public boolean key_down; 19 public boolean key_right; 20 public boolean key_left; 21 public static void main(String[] args) { 22 EventQueue.invokeLater(new Runnable() { 23 public void run() { 24 try { 25 Main window = new Main(); 26 window.frame.setVisible(true); 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 } 31 }); 32 } 33 34 /** 35 * Create the application. 36 */ 37 public Main() { 38 initialize(); 39 } 40 41 /** 42 * Initialize the contents of the frame. 43 */ 44 private void initialize() { 45 frame = new JFrame(); 46 frame.setBounds(100, 100, 1027, 506); 47 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 48 frame.getContentPane().setLayout(null); 49 50 51 JLabel Bo = new JLabel(); 52 Bo.setIcon(new ImageIcon("C:\Users\〇〇〇\Pictures\Bo\right.png")); 53 Bo.setBounds(30, 406, 18, 30); 54 frame.getContentPane().add(Bo); 55 if(key_right) { 56 System.out.println("押されたよ"); 57 } 58 59 60 } 61 62 public void keyPressed(KeyEvent e) { 63 if(e.getKeyCode() == KeyEvent.VK_UP) key_up = true; 64 if(e.getKeyCode() == KeyEvent.VK_DOWN) key_down = true; 65 if(e.getKeyCode() == KeyEvent.VK_RIGHT) key_right = true; 66 if(e.getKeyCode() == KeyEvent.VK_LEFT) key_left = true; 67 } 68 public void keyReleased(KeyEvent e) { 69 if(e.getKeyCode() == KeyEvent.VK_UP) key_up = false; 70 if(e.getKeyCode() == KeyEvent.VK_DOWN) key_down = false; 71 if(e.getKeyCode() == KeyEvent.VK_RIGHT) key_right = false; 72 if(e.getKeyCode() == KeyEvent.VK_LEFT) key_left = false; 73} 74 75 } 76 77
Swingでアクションゲームを作ろうと思っています。
そして、JLabelのBoという名前のキャラクターを動かしたいのです。
いきなり座標をいじくるのは、ダメかなと思ったので、
System.out.println("押されたよ");
という風にコンソールに出力するようにしました。
しかし、押しても反応がありません。
どうしたらよいのでしょうか?
ちなみに、キー操作は、
https://www.youtube.com/watch?v=Snk7ZrInI5E
を参考にしました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/14 23:57