0

我像JFrame以前一样上舞台课,一切都运转良好..直到我试图让它JPanel停止,然后它就停止了,什么也没有发生。KeyListener不工作,知道我的来源有什么问题

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
class stage extends JPanel implements Runnable,KeyListener
{   
    private int timer;
    int y_back = -4500;
    int x_back = 0;
    int x_player =295;
    int y_player = 590;
    int x_nenek = 600;  
    private Image img;
    private Image img_player;
    private Image img_nenek;    
    stage()
    {
        super();
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(0,0);
        setSize(700,700);
        //setResizable(false);
        setVisible(true);       
        try{
            img = ImageIO.read(getClass().getResourceAsStream("jalan.png"));
            img_player= ImageIO.read(getClass().getResourceAsStream("player.png"));
            img_nenek= ImageIO.read(getClass().getResourceAsStream("Nenek.png"));
            }
        catch(Exception e){}        
    new Thread(this).start();
    addKeyListener(this);    
    }
    public void update(Graphics g)
    {
        paint(g);
    }
    public void paint(Graphics g)
    {
        g.drawImage(img,x_back,y_back+200,null);//background
        g.drawImage(img_player,x_player,y_player,null);//player
        g.drawImage(img_nenek,x_nenek,y_back+1750,null);// nenek    
    }
    public void keyPressed(KeyEvent e)
    {
        int c = e.getKeyCode();
        if (y_back<-200) 
        {
            if (c == KeyEvent.VK_UP)
            {
                if(x_nenek>=200 &&x_nenek<=500)
                {
                        JOptionPane.showMessageDialog(null,"awas ada yang menyeberang","Ehem~~~~",JOptionPane.WARNING_MESSAGE);
                        y_back+=0       ;
                }
                if (x_player >=310 && y_back>=-3550 && y_back<= -3400)
                {
                    JOptionPane.showMessageDialog(null,"Kamu telah masuk kubangan","peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }

                if (x_player <=265 && y_back >=-3300 && y_back <=-3250)
                {
                    JOptionPane.showMessageDialog(null,"Kamu telah masuk kubangan","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }
                if ((x_player>=182 && x_player<=365) && (y_back>=-2100 && y_back<=-1800))//clear
                {
                    JOptionPane.showMessageDialog(null," itu lubang mas =_+ , balik sana","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }   
                if ((x_player>=130 && x_player <=200)&& y_back>=-900 && y_back<=-800)
                {
                    JOptionPane.showMessageDialog(null,"baksoooooooooooooo","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back -=10;    
                }               
                if ((x_player>=280 && x_player<=600) && y_back>=-750)//clear
                {
                    JOptionPane.showMessageDialog(null,"lewat kiri mas =_+","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back -=10;
                }   

                if((x_player>=130 && x_player <=180) && y_back>=-4200 && y_back<=-4000) // clear
                {
                    JOptionPane.showMessageDialog(null,"Minggir... ntar ketatap mas =_=","Peringatan",JOptionPane.WARNING_MESSAGE); 
                    y_back -=10;

                }

                if((x_player >= 0 && x_player<= 275 && y_back>=-2800 && y_back<=-2600) || x_player >=400&& y_back>=-2800 && y_back<=-2600)//clear       
                    y_back +=0;
                else
                y_back+=10;
            }
            if (c == KeyEvent.VK_LEFT && x_player>135)
            {
                if(x_nenek>=200 &&x_nenek<=500)
                    {   
                        JOptionPane.showMessageDialog(null,"awas ada yang menyeberang","Ehem~~~~",JOptionPane.WARNING_MESSAGE);

                    }
                if (x_player <=265 && y_back >=-3300 && y_back <=-3250)
                {
                    JOptionPane.showMessageDialog(null,"Kamu telah masuk kubangan","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }
                if ((x_player>=130 && x_player <=200)&& y_back>=-900 && y_back<=-800)
                {
                    JOptionPane.showMessageDialog(null,"baksoooooooooooooo","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back -=3;
                    x_player+=3;        
                }               
                if ((x_player>=182 && x_player<=365) && (y_back>=-2100 && y_back<=-1700))//clear
                {
                    JOptionPane.showMessageDialog(null," itu lubang mas =_+ , balik sana","Peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }   
                if((x_player>=130 && x_player <=180) && y_back>=-4200 && y_back<=-4000)// clear
                {
                    JOptionPane.showMessageDialog(null,"Minggir... ntar ketatap mas =_=","Peringatan",JOptionPane.WARNING_MESSAGE); 
                    y_back -=3;
                    x_player-=3;
                }   
                else
                x_player-=3;
                y_back +=3;

            }
            if (c == KeyEvent.VK_RIGHT && x_player<465)
            {
                if(x_nenek>=200 &&x_nenek<=500)//clear
                    {
                    JOptionPane.showMessageDialog(null,"awas ada yang menyeberang","Ehem~~~~",JOptionPane.WARNING_MESSAGE);
                    y_back+=5;
                    x_player+=5;
                    }
                if (x_player >=340 && y_back>=-3550 && y_back<= -3400)
                {
                    JOptionPane.showMessageDialog(null,"Kamu telah masuk pada kubangan","peringatan",JOptionPane.WARNING_MESSAGE);
                    y_back =-4500;
                }
                if ((x_player>=280 && x_player<=600) && y_back>=-750)
                    JOptionPane.showMessageDialog(null,"lewat kiri mas =_+","Peringatan",JOptionPane.WARNING_MESSAGE);
                x_player+=3;
                y_back +=3;
            }
        }
        else
        JOptionPane.showMessageDialog(null,"Selamat, kamu sudah sampai di tempat tujuan","",JOptionPane.WARNING_MESSAGE);              
    }
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){}
    public void run()
    {
        while (true)
        try
        {
            repaint();
            if(y_back>=-2000)
            x_nenek-=10;
            Thread.sleep(200);
        }    
            catch (Exception e)
            {
            }       
    }
}
public class Tes2 extends JFrame
{
    Tes2()
    {
        stage s = new stage();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(50,50);
    setSize(700,700);
    setResizable(false);
    setVisible(true);
        add(s);       
    }
    public static void main(String[] args)
    {
        new Tes2();
    }
}
4

1 回答 1

2

不要覆盖paint() 和update()。这仅适用于 AWT 组件,而不适用于 Swing 组件。

对于 JPanel 上的自定义绘画,您覆盖该paintComponent()方法并确保在开始时调用 super.paintComponent()。阅读 Swing 教程中关于自定义绘画的部分以获取更多信息和工作示例。

关键事件仅分派给具有焦点的组件。您需要使面板具有焦点。

但是,通常您不应该使用 KeyListener。更好的方法是使用Key Bindings. 查看Motion With the Keyboard以获取有关此主题的更多信息以及一些工作示例以及 Swing 教程的链接。

于 2013-06-18T03:50:33.313 回答