尝试使用GridLayout

时间:2016-01-04 05:46:08

标签: java swing loops jlabel

我试图让一些JLabel在按下按键的情况下使用GridLayout在JPanel上互相切换位置。

到目前为止:
我只能让他们对鼠标点击做出反应 2.他们只能切换位于其上方和左侧的位置。

注意:
1.如果我设置movePlayer(1)它可以工作。并将JLabel从网格(8,7)切换为播放器的图像,其中一个JLabel上面有一个楼层的图像。不过我的问题是,当我设置movePlayer(2)时,它给了我和bouds异常索引 2.这只是我认为与该问题相关的代码的一部分:

  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);
}

上面的代码中没有显示:

1. MapElement是许多子类的父类(继承)   2. loadMap()从一个字符数组中读出字符(例如a,b,c ...),用一个MapElement子类的新实例(例如new Player(),new Floor(),new Tree())填充数组myMap。

3. drawMap()根据类别JLabelsJPanel添加GridLayout(8,7)     myMap [i] [j]

4.(两种方法均采用双For循环i <8,j <7(以填充)   (JLabel)

谢谢你,对于糟糕的编码和noobishness感到抱歉,希望这很清楚。随意建议一个更好的方法来移动JPanelGridLayout的玩家(dir == 1)。请记住,主要问题是:为什么(dir == 2)有效且natural_sum=len(len+1)/2; 没有?!

1 个答案:

答案 0 :(得分:2)

我不确定我是否完全理解您的问题,但是,您可以使用一些技巧来更改组件的呈现顺序,例如,您可以更改componentZOrder,这将是更改组件的布局和呈现顺序。

如果您想要包含键盘互动,我强烈建议您使用Key Bindings API,例如......

Move

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();
            }

        }

    }

}

现在,我使用的Action处理垂直和水平更改,您可能需要考虑使用两个或更多,这可以更好地约束移动,因此如果用户尝试移动超出开始或结束行,它会限制它们,因为它现在正好,它允许组件向上或向下流动到下一行

您可能还想查看How to Use Actions