尽管有关键听众,但位置仍未移动

时间:2016-12-27 06:04:13

标签: java swing keylistener

我使卡片布局工作完全正常但现在发生的事情是我在对象角色上添加了一个keylistener,这是一个JLabel,所以每当有人按下角色时,它应该向上移动,但它绝对没有!

我也尝试用一个按钮取代它,当点击它时它会移动它并且它完全正常工作。此外,我尝试更改事件意味着我改变了,如果他们按下然后城镇地图的图像会改变但仍然没有效果所以我的KeyListener似乎有问题

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.*; import javax.swing.*;

@SuppressWarnings({ "unused", "serial" }) public class FinalBlowzXC extends JFrame implements KeyListener{

public static JPanel game=new JPanel();
public static JPanel mainmenu=new JPanel(null);
public static JPanel loginpanel=new JPanel(null);
public static JPanel tutorial=new JPanel(null);
public static JPanel registration=new JPanel(null);
public static JPanel town_map=new JPanel(null);
public JTextField username= new JTextField("Username");
public JTextField password=new JTextField("Password");
public  JLabel bglogin=new JLabel();
public JLabel character=new JLabel();
public  JButton log_in=new JButton();
public  JButton register=new JButton();
CardLayout page= new CardLayout();
public String level="";
public int charx=350;
public int chary=435;

public static void main(String []args)
{
    new FinalBlowzXC().setVisible(true);
}

public FinalBlowzXC()
{
    super("Final Blowz Xchanged");
    setSize(640,510);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    game.setLayout(page);
    game.add(mainmenu, "1");
    game.add(loginpanel, "2");
    game.add(tutorial, "3");
    game.add(registration, "4");
    game.add(town_map, "5");
    add(game);
    opening();
}

    public void opening()
    {
        page.show(game, "1");
        JLabel bgmainmenu;
        JButton start;
        JButton exit;
        bgmainmenu = new JLabel();
        start = new JButton();
        exit = new JButton();

        bgmainmenu.setIcon(new ImageIcon(getClass().getResource("/FF-XV.jpg")));
        bgmainmenu.setBounds(0,0,640,480);
        mainmenu.add(bgmainmenu);
        mainmenu.add(start);
        start.setBounds(280, 360, 70, 20);
        start.setBorder(null);
        start.setBorderPainted(false);
        start.setContentAreaFilled(false);
        start.setOpaque(false);
        start.addActionListener(new Start());
        exit.setBounds(280, 385, 70, 20);
        mainmenu.add(exit);
        exit.setBorder(null);
        exit.setBorderPainted(false);
        exit.setContentAreaFilled(false);
        exit.setOpaque(false);
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });
    }


    class Start implements ActionListener{

    public void actionPerformed(ActionEvent e) {    
            login();

    }

}

    public void login()
    {
        page.show(game, "2");

        bglogin.setIcon(new ImageIcon(getClass().getResource("/FF-XV Login.jpg")));
        bglogin.setBounds(0, 0, 640, 480);


        username.setBounds(300, 285, 85, 15);
        username.setBorder(null);
        username.setForeground(Color.WHITE);
        username.setOpaque(false);

        password.setBounds(300, 310, 85, 20);
        password.setBorder(null);
        password.setForeground(Color.WHITE);
        password.setOpaque(false);

        log_in.setBounds(280, 335, 50, 45);
        log_in.setBorder(null);
        log_in.setBorderPainted(false);
        log_in.setContentAreaFilled(false);
        log_in.setOpaque(false);
        log_in.addActionListener(new Log_in());

        register.setBounds(335, 335, 55, 45);
        register.setBorder(null);
        register.setBorderPainted(false);
        register.setContentAreaFilled(false);
        register.setOpaque(false);
        register.addActionListener(new Register());

        loginpanel.add(username);
        loginpanel.add(password);
        loginpanel.add(bglogin);
        loginpanel.add(log_in);
        loginpanel.add(register);
    }

    class Log_in implements ActionListener{

        public void actionPerformed(ActionEvent e) 
        {
            Tutorial();

        }

    }

    class Register implements ActionListener{

        public void actionPerformed(ActionEvent e)
        {
            page.show(game, "4");
        }
    }

    public void Tutorial()
    {
        page.show(game, "3");
        JLabel bgtutorial=new JLabel();
        JLabel animeforward=new JLabel();
        JLabel animeright=new JLabel();
        JLabel animeleft=new JLabel();
        JButton next=new JButton();
        JLabel animebackward=new JLabel();
        bgtutorial.setIcon(new ImageIcon(getClass().getResource("/FF-XV Tutorial.jpg")));
        bgtutorial.setBounds(0, 0, 640, 480);
        animeforward.setIcon(new ImageIcon(getClass().getResource("walkanimofficialfront.gif")));
        animeforward.setBounds(115, 230, 30, 30);
        animeright.setIcon(new ImageIcon(getClass().getResource("walkanimofficialright.gif")));
        animeright.setBounds(190, 315, 30, 30);
        animeleft.setIcon(new ImageIcon(getClass().getResource("walkanimofficialleft.gif")));
        animeleft.setBounds(45, 315, 30, 30);
        animebackward.setIcon(new ImageIcon(getClass().getResource("walkanimofficialback.gif")));
        animebackward.setBounds(115, 405, 30, 30);
        next.setBounds(530, 430, 100, 30);
        next.setBorder(null);
        next.setBorderPainted(false);
        next.setContentAreaFilled(false);
        next.setOpaque(false);
        next.addActionListener(new Next());
        tutorial.add(next);
        tutorial.add(animeright);
        tutorial.add(animeleft);
        tutorial.add(animebackward);
        tutorial.add(animeforward);
        tutorial.add(bgtutorial);

    }

    class Next implements ActionListener{

        public void actionPerformed(ActionEvent e)
        {
            town();
        }
    }

    public void town()
    {
        page.show(game, "5");
        JLabel map=new JLabel();
        map.setIcon(new ImageIcon(getClass().getResource("/FF-XV Town.jpg")));
        map.setBounds(0, 0, 640, 480);
        character.setIcon(new  ImageIcon(getClass().getResource("standfront.png")));
        character.setBounds(charx, chary, 30, 35);
        town_map.add(character);
        town_map.add(map);

    }   


    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_UP)
        {
            character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialfront.gif")));
            character.setLocation(character.getX(), character.getY()+5);
        }

        if(e.getKeyCode()==KeyEvent.VK_RIGHT)
        {
            character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialright.gif")));
            character.setLocation(character.getX()+5, character.getY());
        }

        if(e.getKeyCode()==KeyEvent.VK_LEFT)
        {
            character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialleft.gif")));
            character.setLocation(character.getX()-5, character.getY()+5);
        }

        if(e.getKeyCode()==KeyEvent.VK_DOWN)
        {
            character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialback.gif")));
            character.setLocation(character.getX(), character.getY()-5);
        }

    }


    public void keyReleased(KeyEvent e) {


    }

    public void keyTyped(KeyEvent e) {


    }

}

2 个答案:

答案 0 :(得分:1)

在程序中添加以下代码。

public static JPanel mainmenu = new JPanel();
public static JPanel loginpanel = new JPanel();

或初始化mainmenu&在将loginpanel面板添加到game面板之前 | epoch | val_acc | val_loss | |-------|---------------------|---------------------| | 0 | 0.87280000000000002 | 0.4549577592611313 | | 1 | 0.9042 | 0.33785744296908377 | | 2 | 0.90439999999999998 | 0.32197892428636549 | | 3 | 0.9204 | 0.28751977597177031 | | 4 | 0.92400000000000004 | 0.27027705881893632 | | 5 | 0.92090000000000005 | 0.27390556887090206 | | 6 | 0.92430000000000001 | 0.27321851629316807 | | 7 | 0.92559999999999998 | 0.26133619344234466 | | 8 | 0.92800000000000005 | 0.25024868482649326 | | 9 | 0.9284 | 0.24917590338289738 | | 10 | 0.92930000000000001 | 0.25116596995443108 | | 11 | 0.92730000000000001 | 0.25142454357892274 | | 12 | 0.93100000000000005 | 0.24925820100381971 | | 13 | 0.90459999999999996 | 0.32235068403929473 | | 14 | 0.49480000000000002 | 7.9964268966674803 | | 15 | 0.48849999999999999 | 8.1361763145446773 | | 16 | 0.37880000000000003 | 9.9022711822509759 |

答案 1 :(得分:0)

当您致电game.add(loginPanel, "2") loginPanel时,

null