Trying to move JLabels on a JPanel with GridLayout

随声附和 提交于 2019-12-12 04:04:46

问题


I am trying to make some JLabels switch places with eachother on a JPanel with GridLayout on key pressed.

So far:
1. I can only make them react to a mouse click.
2. They can only switch places with the one above it and to its left.

Note:
1. If I set movePlayer(1) it works. And switches a JLabel from the grid (8,7) with an image of the player with one of the JLabels with an image of a floor above it. However my problem is, when i set movePlayer(2) it gives me and index out of bouds exception.
2. This is only part of the code which I believe to be relevant to the issue:

  JPanel myCentrePanel = new JPanel();

MapElement[][] myMap = new MapElement[8][7];



 public TileSwitching(){
      super("TileSwitching");
      setSize(400,400);
      setResizable(false);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      myCentrePanel.setLayout(new GridLayout(8,7));
      myCentrePanel.setPreferredSize(new Dimension(5,5));
      add(myCentrePanel);
      loadMap(level1);
      drawMap();
      myCentrePanel.setFocusable(true);
      myCentrePanel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {

                    movePlayer(1);
                    myCentrePanel.removeAll();
                    drawMap();
                    myCentrePanel.revalidate();

                }
            });

      myCentrePanel.setBackground(Color.CYAN);
  }
public void movePlayer(int dir){

if(dir == 1){

            for(int i = 0; i<8; i++){
                for(int j= 0; j<7; j++){
            if (myMap[i][j].getClass() == wk.getClass()){
                myMap[i-1][j] = new Player();
                myMap[i][j] = new Floor();
            }
            else{
                myMap[i][j] = myMap[i][j];
            }
                }}

if(dir == 2){

            for(int i = 0; i<8; i++){
                for(int j= 0; j<7; j++){
            if (myMap[i][j].getClass() == wk.getClass()){
                myMap[i+1][j] = new Player();
                myMap[i][j] = new Floor();
            }
            else{
                myMap[i][j] = myMap[i][j];
            }
                }}
        }

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

What isn't shown in the code above:

1.MapElement is a parent class(inherits) to many subclasses.
2.loadMap() reads out chars (E.g. a,b,c...)from an array of chars to fill the array myMap with a new instance of a subclass of MapElement(E.g. new Player(),new Floor(),new Tree()).

3.drawMap() adds JLabels to the JPanel according to the class of of myMap[i][j]

4.(Both methods were done with double For loops i<8, j<7(to fill the
GridLayout(8,7) .

Thank you, sorry for the bad coding and noobishness, hope this is clear. Feel free to suggest a better way to move a player (JLabel) on a JPanel with a GridLayout. And remember, the main question is: Why does (dir == 1) work and (dir == 2) doesn't?!


回答1:


I'm not sure I understand your problem entirely, but, there are some tricks you can use to change the order in which components are rendered, for example, you can change the componentZOrder, which will change the order in which the components are laid out and rendered.

If you want to include key board interaction, I highly recommend using the Key Bindings API, for example...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Test {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel player;
        private List<JLabel> tiles;

        public TestPane() {
            setLayout(new GridLayout(3, 3));
            player = makeLabel("P");
            add(player);
            tiles = new ArrayList<>(8);
            for (int index = 0; index < 8; index++) {
                JLabel tile = makeLabel(" ");
                tiles.add(tile);
                add(tile);
            }

            addKeyBinding("left", KeyEvent.VK_LEFT, new MoveAction(-1, 0));
            addKeyBinding("right", KeyEvent.VK_RIGHT, new MoveAction(1, 0));
            addKeyBinding("up", KeyEvent.VK_UP, new MoveAction(0, -1));
            addKeyBinding("down", KeyEvent.VK_DOWN, new MoveAction(0, 1));
        }

        protected void addKeyBinding(String name, int keyCode, Action action) {
            InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap actionMap = getActionMap();

            inputMap.put(KeyStroke.getKeyStroke(keyCode, 0), name);
            actionMap.put(name, action);
        }

        protected JLabel makeLabel(String text) {
            JLabel label = new JLabel(text);
            label.setBorder(new CompoundBorder(
                    new LineBorder(Color.GRAY),
                    new EmptyBorder(4, 4, 4, 4)));
            return label;
        }

        public class MoveAction extends AbstractAction {
            private final int xDelta, yDelta;

            public MoveAction(int xDelta, int yDelta) {
                this.xDelta = xDelta;
                this.yDelta = yDelta;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = getComponentZOrder(player);
                index += xDelta;
                index += (yDelta * 3);
                if (index < 0) {
                    index = 0;
                } else if (index >= getComponentCount()) {
                    index = getComponentCount() - 1;
                }
                setComponentZOrder(player, index);
                revalidate();
                repaint();
            }

        }

    }

}

Now, the Action I use handles both vertical and horizontal changes, you might want to consider using two or more, which could better constrain the movement, so if the user tried moving beyond the start or end of the row, it would restrict them, as it stands right now, it allows the component to flow up or down to the next row

You might also like to have a look at How to Use Actions



来源:https://stackoverflow.com/questions/34585343/trying-to-move-jlabels-on-a-jpanel-with-gridlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!