我可以将 JLabels 添加到数组中吗?

大卫·包蒂斯塔

我想清理我的代码,我想知道是否可以将它们JLabels放入ArrayList. 这将用于do-while检查我的播放器碰撞的循环。

下面是GamePane班级。此代码包含移动玩家并包含碰撞代码。这是我创建的地方ArrayList

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

/**
 * This class Holds the game pane that has the moving player. It also contains
 * the GamePane
 * 
 * @author 602052004
 *
 */

public class GamePane extends JPanel implements ActionListener, KeyListener {// *change GamePane to GamePane
    // This is were the game screen is made and the player is created.

    private static final long serialVersionUID = 1L;
    JLabel player = new JLabel();
    JLabel finish = new JLabel();

    // This is were the JLabels for the walls are created
    //JLabel wall1 = new JLabel();
    //ArrayList<JLabel> array = new ArrayList<>();

 ArrayList<JLabel> array = new ArrayList<>();
 array.add(wall1);
    JLabel wall1 = new JLabel();
    JLabel wall2 = new JLabel();
    JLabel wall3 = new JLabel();
    JLabel wall4 = new JLabel();
    JLabel wall5 = new JLabel();
    JLabel wall6 = new JLabel();
    JLabel wall7 = new JLabel();
    JLabel wall8 = new JLabel();
    JLabel wall9 = new JLabel();
    JLabel wall10 = new JLabel();
    JLabel wall11 = new JLabel();
    JLabel wall12 = new JLabel();
    JLabel wall13 = new JLabel();
    JLabel wall14 = new JLabel();
    JLabel wall15 = new JLabel();
    JLabel wall16 = new JLabel();
    JLabel wall17 = new JLabel();
    JLabel wall18 = new JLabel();
    JLabel wall19 = new JLabel();
    JLabel wall20 = new JLabel();
    JLabel wall21 = new JLabel();
    JLabel wall22 = new JLabel();
    JLabel wall23 = new JLabel();
    JLabel wall24 = new JLabel();
    int playerSpeed = 5;
    int FPS = 40;
    private final Set<Integer> keys = new HashSet<>();

    // The keys set holds the keys being pressed

    public static void main(String[] args) {
        // Open the GUI window
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // Create a new object and
                // run its go() method
                new GamePane().go();
            }
        });
    }

    GamePane() {
        // Run the parent class constructor
        super();
        // Allow the panel to get focus
        setFocusable(true);
        // Don't let keys change the focus
    }

    /**
     * The frame that shows my game. It contains the game frame which holds my
     * JPanel GameStage and ButtonPane.
     */
    protected void go() {
        setLayout(new CardLayout());
        // Setup the window
        JFrame GameFrame = new JFrame();
        // Add this panel to the window
        GameFrame.setLayout(new CardLayout());
        GameFrame.add(this, "main");
        GameFrame.setContentPane(this);

        // Set's the window properties
        GameFrame.setTitle("main");
        GameFrame.setSize(800, 600);
        GameFrame.setLocationRelativeTo(null);
        GameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GameFrame.setVisible(true);
        GameFrame.add(new ButtonPane(GameFrame), "buttons");
        // Creates the new JPanel that will hold the game.
        JPanel gamestage = new JPanel();
        gamestage.setBackground(Color.darkGray);
        GameFrame.add(gamestage, "game");
        gamestage.setLayout(null);
        // *Move the setup of the player and the timer under the walls
        // Get a sample of collisions going so that i can do it over the weekend
        // Setup the movable box
        player.setBounds(25, 25, 20, 20);
        player.setVisible(true);
        player.setBackground(Color.cyan);
        // Opaque makes the background visible
        player.setOpaque(true);

        // Setup the key listener
        addKeyListener(this);
        // Null layout allows moving objects!!!
        gamestage.add(player);
        // Set the timer
        Timer tm = new Timer(1000 / FPS, this);
        tm.start();

        wall1.setBounds(10, 15, 10, 480);
        wall1.setVisible(true);
        wall1.setBackground(Color.white);
        wall1.setOpaque(true);
        gamestage.add(wall1);

        wall2.setBounds(10, 10, 755, 10);
        wall2.setVisible(true);
        wall2.setBackground(Color.white);
        wall2.setOpaque(true);
        gamestage.add(wall2);
        // wall3.setBounds(x, y, width, height);
        wall3.setBounds(10, 100, 100, 10);
        wall3.setVisible(true);
        wall3.setBackground(Color.white);
        wall3.setOpaque(true);
        gamestage.add(wall3);

        wall4.setBounds(100, 60, 10, 40);
        wall4.setVisible(true);
        wall4.setBackground(Color.white);
        wall4.setOpaque(true);
        gamestage.add(wall4);

        wall5.setBounds(70, 60, 35, 10);
        wall5.setVisible(true);
        wall5.setBackground(Color.white);
        wall5.setOpaque(true);
        gamestage.add(wall5);

        wall6.setBounds(60, 100, 10, 90);
        wall6.setVisible(true);
        wall6.setBackground(Color.white);
        wall6.setOpaque(true);
        gamestage.add(wall6);

        wall7.setBounds(10, 230, 60, 10);
        wall7.setVisible(true);
        wall7.setBackground(Color.white);
        wall7.setOpaque(true);
        gamestage.add(wall7);

        wall8.setBounds(60, 230, 10, 65);
        wall8.setVisible(true);
        wall8.setBackground(Color.white);
        wall8.setOpaque(true);
        gamestage.add(wall8);

        wall9.setBounds(60, 290, 125, 10);
        wall9.setVisible(true);
        wall9.setBackground(Color.white);
        wall9.setOpaque(true);
        gamestage.add(wall9);

        wall10.setBounds(175, 300, 10, 45);
        wall10.setVisible(true);
        wall10.setBackground(Color.white);
        wall10.setOpaque(true);
        gamestage.add(wall10);

        wall11.setBounds(130, 335, 45, 10);
        wall11.setVisible(true);
        wall11.setBackground(Color.white);
        wall11.setOpaque(true);
        gamestage.add(wall11);

        wall12.setBounds(10, 335, 70, 10);
        wall12.setVisible(true);
        wall12.setBackground(Color.white);
        wall12.setOpaque(true);
        gamestage.add(wall12);

        wall13.setBounds(10, 435, 60, 10);
        wall13.setVisible(true);
        wall13.setBackground(Color.white);
        wall13.setOpaque(true);
        gamestage.add(wall13);

        wall14.setBounds(10, 385, 230, 10);
        wall14.setVisible(true);
        wall14.setBackground(Color.white);
        wall14.setOpaque(true);
        gamestage.add(wall14);

        wall15.setBounds(60, 390, 10, 50);
        wall15.setVisible(true);
        wall15.setBackground(Color.white);
        wall15.setOpaque(true);
        gamestage.add(wall15);

        wall16.setBounds(230, 290, 10, 155);
        wall16.setVisible(true);
        wall16.setBackground(Color.white);
        wall16.setOpaque(true);
        gamestage.add(wall16);

        wall17.setBounds(10, 485, 750, 10);
        wall17.setVisible(true);
        wall17.setBackground(Color.white);
        wall17.setOpaque(true);
        gamestage.add(wall17);

        wall18.setBounds(70, 180, 60, 10);
        wall18.setVisible(true);
        wall18.setBackground(Color.white);
        wall18.setOpaque(true);
        gamestage.add(wall18);

        wall19.setBounds(120, 180, 10, 55);
        wall19.setVisible(true);
        wall19.setBackground(Color.white);
        wall19.setOpaque(true);
        gamestage.add(wall19);
    }

    public boolean areColliding(JLabel a, JLabel b) {
        return a.getBounds().intersects(b.getBounds());
    }

    /**
     * this method makes the player move. It takes the players speed and subtracts
     * or adds the player speed to the current position of the player. It also
     * figures out were the player is at currently aswell.
     * 
     * @param arg0
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        // Move up if W is pressed
        if (keys.contains(KeyEvent.VK_W)) {
            player.setLocation(player.getX(), player.getY() - playerSpeed);
        }
        // Move right if D is pressed
        if (keys.contains(KeyEvent.VK_D)) {
            player.setLocation(player.getX() + playerSpeed, player.getY());
        }
        // Move down if S is pressed
        if (keys.contains(KeyEvent.VK_S)) {
            player.setLocation(player.getX(), player.getY() + playerSpeed);
        }
        // Move left if A is pressed
        if (keys.contains(KeyEvent.VK_A)) {
            player.setLocation(player.getX() - playerSpeed, player.getY());
        }

            // Check for collisions
        if (areColliding(wall1, player)) {
            // Reposition the target
            int newX = (int) (25);
            int newY = (int) (25);
            player.setLocation(newX, newY);
        }else 

        // Check for collisions
                if (areColliding(wall2, player)) {
                    // Reposition the target
                    int newX = (int) (25);
                    int newY = (int) (25);
                    player.setLocation(newX, newY);
                }else
        // Check for collisions
        if (areColliding(wall3, player)) {
            // Reposition the target
            int newX = (int) (25);
            int newY = (int) (25);
            player.setLocation(newX, newY);
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // Add the key to the list
        // of pressed keys
        if (!keys.contains(e.getKeyCode())) {
            keys.add(e.getKeyCode());
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // Remove the key from the
        // list of pressed keys
        keys.remove((Integer) e.getKeyCode());
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
}

下面是ButtonPane班级。它控制屏幕切换。切换的两个屏幕是GamePaneButtonPaneGamePane包含了墙壁和所有的代码来运行游戏。ButtonPane等级如下表所示:

/**
 * This pane contains the button and sets up the button pane
 */
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonPane extends JPanel {

    private JButton startBTN;// Calls the JButton
    JFrame game;

    public ButtonPane(JFrame g) {
        game = g;
        setLayout(new GridBagLayout());
        setBackground(Color.gray);// Sets the menu stages color blue
        startBTN = new JButton("Start");// Creates a new button
        add(startBTN);// Adds the button on the startStage

        startBTN.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (game.getContentPane().getLayout() instanceof CardLayout) {
                    CardLayout layout = (CardLayout) getParent().getLayout();
                    layout.show(game.getContentPane(), "game");

                }
            }
        });
    }
}
0x光标

您可以像这样在 for 循环ArrayList中用JLabels初始化一个

ArrayList<JLabel> array = new ArrayList<>();

for (int i = 0, i < numberOfLabels, i++)
{
    array.add(new JLabel());
}

要从 中检索元素ArrayList,只需使用:

array.get(elementIndex);

如果你想把已经创建的JLabels放入一个ArrayList,你可以这样做:

array.add(labelObject);

每个JLabel对象要在ArrayList

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我可以将paintComponents()添加到数组中吗?

来自分类Dev

我可以将javascript原型添加到我的数组实例中吗?

来自分类Dev

我可以将课程添加到链接中吗

来自分类Dev

将JLabels的2D数组打印到GridLayout

来自分类Dev

我可以将对象键添加到数组吗?

来自分类Dev

可以将numpy数组添加到python集吗?

来自分类Dev

我可以在打字稿中添加到只读类成员数组吗?

来自分类Dev

我可以限制添加到数组中的项目数量吗?

来自分类Dev

我可以将面孔从数组添加到THREE.BufferGeometry吗?

来自分类Dev

我可以将样式标签添加到innerHTML吗?

来自分类Dev

我可以将pattern属性添加到textarea吗?

来自分类Dev

我可以将样式标签添加到innerHTML吗?

来自分类Dev

我可以将pattern属性添加到textarea吗?

来自分类Dev

将数组添加到数组中

来自分类Dev

我可以将SVG文件中的动画添加到Android启动屏幕中吗?

来自分类Dev

用JLabels填充数组

来自分类Dev

我可以/应该将Cloudfront添加到在弹性beantalk上运行的Web服务中吗

来自分类Dev

我可以将超链接添加到Apple应用商店描述中吗

来自分类Dev

我们可以将CV限定词添加到main的参数中吗?

来自分类Dev

我可以将Click Listener添加到自定义Toast中吗

来自分类Dev

NLTK:我可以将终端添加到已经生成的语法中吗

来自分类Dev

我可以将通配符依赖项添加到deb包中吗?

来自分类Dev

我可以将颜色选择器添加到Django表单中吗

来自分类Dev

我可以使用Kotlin将List中的MVoice字段添加到mutableSetOf()吗?

来自分类Dev

我可以将通配符依赖项添加到deb包中吗?

来自分类Dev

vb.net-我可以将txt的内容添加到程序中吗?

来自分类Dev

我可以将数字添加到int值中而不是对其进行更改吗?

来自分类Dev

我可以将运算符添加到现有的类中吗?

来自分类Dev

我可以使用 tk.Checkbutton 将变量添加到元组中吗?

Related 相关文章

  1. 1

    我可以将paintComponents()添加到数组中吗?

  2. 2

    我可以将javascript原型添加到我的数组实例中吗?

  3. 3

    我可以将课程添加到链接中吗

  4. 4

    将JLabels的2D数组打印到GridLayout

  5. 5

    我可以将对象键添加到数组吗?

  6. 6

    可以将numpy数组添加到python集吗?

  7. 7

    我可以在打字稿中添加到只读类成员数组吗?

  8. 8

    我可以限制添加到数组中的项目数量吗?

  9. 9

    我可以将面孔从数组添加到THREE.BufferGeometry吗?

  10. 10

    我可以将样式标签添加到innerHTML吗?

  11. 11

    我可以将pattern属性添加到textarea吗?

  12. 12

    我可以将样式标签添加到innerHTML吗?

  13. 13

    我可以将pattern属性添加到textarea吗?

  14. 14

    将数组添加到数组中

  15. 15

    我可以将SVG文件中的动画添加到Android启动屏幕中吗?

  16. 16

    用JLabels填充数组

  17. 17

    我可以/应该将Cloudfront添加到在弹性beantalk上运行的Web服务中吗

  18. 18

    我可以将超链接添加到Apple应用商店描述中吗

  19. 19

    我们可以将CV限定词添加到main的参数中吗?

  20. 20

    我可以将Click Listener添加到自定义Toast中吗

  21. 21

    NLTK:我可以将终端添加到已经生成的语法中吗

  22. 22

    我可以将通配符依赖项添加到deb包中吗?

  23. 23

    我可以将颜色选择器添加到Django表单中吗

  24. 24

    我可以使用Kotlin将List中的MVoice字段添加到mutableSetOf()吗?

  25. 25

    我可以将通配符依赖项添加到deb包中吗?

  26. 26

    vb.net-我可以将txt的内容添加到程序中吗?

  27. 27

    我可以将数字添加到int值中而不是对其进行更改吗?

  28. 28

    我可以将运算符添加到现有的类中吗?

  29. 29

    我可以使用 tk.Checkbutton 将变量添加到元组中吗?

热门标签

归档