当我触发我的火球创建时,它会重置我的角色JLabel [英] When I trigger my fireball creating, it resets my Character JLabel

查看:77
本文介绍了当我触发我的火球创建时,它会重置我的角色JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package clickrpg2;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;

public class MainFrame extends javax.swing.JFrame implements ActionListener {

    static MainFrame mainFrame = new MainFrame();
    static Hero hero = new Hero();
    static JLabel fireballButton[] = new JLabel[1000000];
    static int fireballTotal = 0;
    Timer timer = new Timer(10, this);

    public MainFrame() {
        addKeyListener(new KeyThing());
        addMouseListener(new MouseThing());
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/clickrpg2/StickMan1.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(1454, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(597, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                mainFrame.setVisible(true);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int c = 0;
        int x = 0;
        int y = 0;
        int xChar = jLabel1.getX();
        int yChar = jLabel1.getY();
        while (true) {
            x = fireballButton[c].getX();
            y = fireballButton[c].getY();
            fireballButton[c].setBounds(x + 5, y, 64, 32);
            c++;
            if (c == fireballTotal) {
                break;
            }
        }
        jLabel1.setBounds(xChar,yChar,64,64);
    }

    class KeyThing extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            int x = jLabel1.getX();
            int y = jLabel1.getY();
            int speed = hero.getSpeed();
            jLabel1.setBounds(x, y, 64, 64);
            int keycode = e.getKeyCode();

            switch (keycode) {
                case KeyEvent.VK_LEFT:
                    jLabel1.setBounds(x - speed, y, 64, 64);
                    break;
                case KeyEvent.VK_RIGHT:
                    jLabel1.setBounds(x + speed, y, 64, 64);
                    break;
                case KeyEvent.VK_DOWN:
                    jLabel1.setBounds(x, y + speed, 64, 64);
                    break;
                case KeyEvent.VK_UP:
                    jLabel1.setBounds(x, y - speed, 64, 64);
                    break;
                case KeyEvent.VK_SPACE:
                    x = jLabel1.getX();
                    y = jLabel1.getY();
                    fireballButton[fireballTotal] = new JLabel();
                    fireballButton[fireballTotal].setPreferredSize(new Dimension(x, y));
                    mainFrame.getContentPane().add(fireballButton[fireballTotal], BorderLayout.CENTER);
                    ImageIcon label = new ImageIcon("C:\\Users\\Nick\\Documents\\NetBeansProjects\\ClickRPG2\\src\\clickrpg2\\Fireball.png");
                    fireballButton[fireballTotal].setIcon(label);
                    mainFrame.setVisible(true);
                    fireballButton[fireballTotal].setBounds(x, y, 64, 64);
                    mainFrame.add(fireballButton[fireballTotal]);
                    fireballTotal++;
                    timer.start();
                    if (fireballTotal > 900000) {
                        fireballTotal = 0;
                    }
                    break;
            }
        }
    }

    class MouseThing extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            int x = mainFrame.getX();
            int y = mainFrame.getY();
            fireballButton[fireballTotal] = new JLabel();
            ImageIcon label = new ImageIcon("C:\\Users\\Nick\\Documents\\NetBeansProjects\\ClickRPG2\\src\\clickrpg2\\Fireball.png");
            fireballButton[fireballTotal].setIcon(label);
            fireballButton[fireballTotal].setBounds(x, y, 64, 64);
            fireballTotal++;
            timer.start();
            if (fireballTotal > 900000) {
                fireballTotal = 0;
            }
        }
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
}

基本上,我只是需要它我制作火球时不重置jLabel1 jLabel.\

Basically, I just need it to not reset jLabel1 when I make a fireball jLabel.\

有什么建议吗?在此先感谢,我是新来的,所以我希望代码阻止。因为我不知道错误发生在哪里,所以我需要留下大部分代码。抱歉。

Any advice? Thanks in advance, I'm new here, so I hope the code blocks. Because I have no idea where the error is occuring, I needed to leave most of the code in. Sorry.

推荐答案

嗯,这是你的问题...

Well, there's your problem...

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);

您的容器受布局管理器控制。任何改变任何组件的位置或大小的尝试(通过 setBounds setLocation 和/或 setSize )将失败,因为布局管理器将放弃这些更改而转而支持自己的...

Your container is under the control of a layout manager. Any attempt to change the position or size of any component (via setBounds, setLocation and/or setSize) will fail because the layout manager will discard those changes in favor of its own...

您添加或删除任何更改的时刻组件,布局管理器会将所有组件更新到它认为应该的位置。

The moment you add or remove any component, the layout manager will update all the components to where it thinks they should be.

这不起作用,这是一个坏主意。

This won't work and as a bad idea.

while (true) {
    x = fireballButton[c].getX();
    y = fireballButton[c].getY();
    fireballButton[c].setBounds(x + 5, y, 64, 32);
    c++;
    if (c == fireballTotal) {
        break;
    }
}

任何耗时或阻塞动作(如循环)将阻止EDT更新图形,基本上意味着你实际上不会看到火球...而且我不相信你用计时器这个(这个将是正确的方向)仍然使用循环...

Any time consuming or blocking actions (like a loop) will stop the EDT from updating the graphics, basically meaning that you won't actually see the fire ball...And I can't believe you used Timer with this (which would be in the right direction) and still used a loop...

我的建议......

My advice...

使用 JLayerPane 而不是。这允许你使用绝对定位(虽然如果你真的想要你可以应用布局管理器......但它只是 JPanel

此外,请阅读 Swing中的并发性

带示例的更新

public class TestFireBall {

    public static void main(String[] args) {
        new TestFireBall();
    }

    public TestFireBall() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FireBallPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class FireBallPane extends JLayeredPane {

        private JLabel character;
        private List<JLabel> fireBalls;
        private ImageIcon icoFireBall;
        private Timer timer;
        private Timer coolOffTimer;
        private int vX = 12;
        private boolean coolOff = false;

        public FireBallPane() {
            fireBalls = new ArrayList<JLabel>(25);

            ImageIcon icoCharacter = null;
            ImageIcon fireBall = null;

            try {
                icoCharacter = new ImageIcon(ImageIO.read(getClass().getResource("/BlackMage.png")));
                icoFireBall = new ImageIcon(ImageIO.read(getClass().getResource("/FireBall.png")));
            } catch (Exception e) {
                e.printStackTrace();
            }

            character = new JLabel(icoCharacter);

            add(character);

            setFocusable(true);
            requestFocusInWindow();

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "fire");
            ActionMap am = getActionMap();
            am.put("fire", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!coolOff) {
                        coolOff = true;
                        JLabel fireBall = createFireBall();
                        fireBalls.add(fireBall);
                        if (!timer.isRunning()) {
                            timer.start();
                        }
                        coolOffTimer.restart();
                    }
                }
            });

            timer = new Timer(125, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (fireBalls.size() > 0) {
                        JLabel[] balls = fireBalls.toArray(new JLabel[fireBalls.size()]);
                        for (JLabel fireBall : balls) {
                            if (fireBall.getParent() == null) {
                                add(fireBall);
                            }
                            Point p = fireBall.getLocation();
                            p.x += vX;
                            if (p.x + fireBall.getWidth() >= getWidth()) {
                                remove(fireBall);
                                fireBalls.remove(fireBall);
                            } else {
                                fireBall.setLocation(p);
                            }
                            repaint();
                        }
                    } else {
                        timer.stop();
                    }
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);

            coolOffTimer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    coolOff = false;
                }
            });
            coolOffTimer.setRepeats(false);
            coolOffTimer.setCoalesce(true);
        }

        protected JLabel createFireBall() {
            JLabel fireBall = new JLabel(icoFireBall);

            fireBall.setSize(fireBall.getPreferredSize());
            int x = character.getX() + character.getWidth();
            int y = character.getY() + ((character.getHeight() - fireBall.getHeight()) / 2);
            fireBall.setLocation(x, y);

            return fireBall;
        }

        @Override
        public void invalidate() {
            super.invalidate();

            character.setSize(character.getPreferredSize());
            int height = getHeight();
            int y = (height - character.getHeight()) / 2;
            character.setLocation(0, y);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }
}

这篇关于当我触发我的火球创建时,它会重置我的角色JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆