以下のコードで実行したら
初期の座標で止まったまま落下しません
原因を教えてください
java
1import javax.swing.*; 2import java.awt.*; 3import java.awt.event.*; 4 5public class action extends JFrame implements ActionListener{ 6 7 JPanel Panel; 8 9 JLabel Player; 10 JLabel Block; 11 12 ImageIcon PlayerIcon; 13 ImageIcon BlockIcon; 14 15 Timer timer; 16 17 int PlayerX = 20;//プレイヤーのX座標 18 int PlayerY = 40;//プレイヤーのY座標 19 20 static boolean Jflag = false;//ジャンプフラグ 21 22 public static void main(String args[]){ 23 24 action Frame = new action(); 25 26 Frame.setSize(900, 500);//ウィンドウのサイズ設定(X, Y) 27 Frame.setTitle("アクションゲーム");//ウィンドウのタイトル 28 Frame.setLocationRelativeTo(null);//ウィンドウを画面の中央に表示する 29 Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//xで閉じるようにする 30 Frame.setVisible(true);//ウィンドウを可視化 31 32 } 33 34 action(){ 35 36 timer = new Timer(10, this); 37 38 Panel = new JPanel(); 39 Panel.setLayout(null);//レイアウトマネージャー無効化 40 41 PlayerIcon = new ImageIcon("./PlayerIcon.png");//プレイヤーの画像指定 42 BlockIcon = new ImageIcon("./BlockIcon.png");//ブロックの画像指定 43 44 Player = new JLabel(PlayerIcon);//プレイヤー画像設定 45 Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標とサイズ設定 46 47 Block = new JLabel(BlockIcon);//ブロック画像設定 48 Block.setBounds(0, 442, 20, 20);//ブロックの座標とサイズ設定 49 50 Panel.add(Player);//プレイヤー画像追加 51 Panel.add(Block);//ブロック画像追加 52 53 Container contentPane = getContentPane(); 54 contentPane.add(Panel, BorderLayout.CENTER); 55 56 getContentPane().add(Panel, BorderLayout.CENTER);; 57 58 timer.start(); 59 60 } 61 62 public void actionPerformed(ActionEvent e){ 63 64 if(PlayerY <= 462){ 65 66 Jflag = false; 67 timer.stop(); 68 69 } 70 71 if(Jflag == true){//もしジャンプフラグがtrueなら 72 73 PlayerY += 5;//プレイヤーのY座標を-5 74 Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定 75 Panel.repaint();//パネルを更新 76 77 }else{ 78 79 timer.stop(); 80 81 } 82 83 } 84 85}
回答2件
あなたの回答
tips
プレビュー