滑动拼图Java Shuffle问题

时间:2014-04-07 02:14:12

标签: java swing jpanel jbutton actionlistener

当您点击随机播放按钮时,我正在尝试为15件滑动拼图进行随机播放。不幸的是,我不知道如何“抓住”JPanel按钮,因为它们是动态的,不会被“传递”到Action Listener: 这是我的for循环创建&添加按钮:

   pos = new int[][] {
                         {0, 1, 2, 3}, 
                        {4, 5, 6, 7}, 
                        {8, 9, 10, 11}, 
                        {12, 13, 14, 15}
                    };


    centerPanel = new JPanel();
    centerPanel.setLayout(new GridLayout(4, 5, 0, 0));

    add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH);    
    add(centerPanel, BorderLayout.CENTER);

    int counter = 0;
    for ( int i = 0; i < 4; i++) {
        for ( int j = 0; j < 4; j++) {
            if ( j == 3 && i == 3) {
                label = new JLabel("");
                centerPanel.add(label); // EMPTY LABEL
            } else {
                counter++;
                button = new JButton();
                button.setText(String.valueOf(counter));
                button.addActionListener(this);
                centerPanel.add(button);

            }
        }
    }

然后我添加了Shuffle按钮:

    southPanel = new JPanel(new FlowLayout());
    JButton shuffleButton = new JButton("Shuffle");
    southPanel.add(shuffleButton);
    shuffleButton.addActionListener(this);

然后这是Shuffle代码我有4个随机动作我到目前为止(我知道这很糟糕,我不知道更好)。它会移动标签,但按顺序按下所有按钮:(

     public void actionPerformed(ActionEvent e) {

    //GET EMPTY LABEL LOCATION
            int labelX = label.getX();
    int labelY = label.getY();

    int labelPosX = labelX / sizeLabel.width;
    int labelPosY = labelY / sizeLabel.height;

    int labelIndex = pos[labelPosX][labelPosY];


                         // GET button location
            buttonX = label.getX() - size.width;
            buttonY = label.getY();

            // GET button position
            buttonPosX = buttonX / size.width;
            buttonPosY = buttonY / size.height;
            buttonIndex = pos[buttonPosY][buttonPosX];

            // IF LABEL can be moved
            if ((labelY == buttonY && (labelX - buttonX) == size.width )) {

                System.out.println("LABEL MOVES LEFT");
                labelIndex = buttonIndex + 1;

                centerPanel.add(label,buttonIndex);

                // HERE I NEED TO "GRAB BUTTON" AND PUT it 
                // IN LABEL INDEX ???

                labelY = label.getY();
                labelX = label.getX();

            }

任何帮助表示赞赏!!我尝试使用Component getComponent(int),但我不知道如何实现它。我认为这样的事情会很完美......

1 个答案:

答案 0 :(得分:1)

更多的是保持对JLabel的某种引用,然后尝试维护标签所带有的信息。

例如,您只需将JLabel放入List并随机播放列表,删除标签并重新添加。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Shuffle {

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

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

                final PuzzelPane puzzelPane = new PuzzelPane();
                JButton shuffle = new JButton("Shuffel");
                shuffle.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        puzzelPane.shuffle();
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(puzzelPane);
                frame.add(shuffle, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PuzzelPane extends JPanel {

        private List<JLabel> labels;

        public PuzzelPane() {
            setLayout(new GridLayout(3, 3));
            labels = new ArrayList<>(9);
            for (int index = 0; index < 9; index++) {
                JLabel label = new JLabel(String.valueOf(index));
                label.setHorizontalAlignment(JLabel.CENTER);
                labels.add(label);
            }
            shuffle();
        }

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

        public void shuffle() {
            removeAll();
            Collections.shuffle(labels);
            for (JLabel label : labels) {
                add(label);
            }
            revalidate();
        }
    }

}

你也可以使用相同的想法...