垂直和水平检查特定的JButton?

时间:2014-03-30 17:12:26

标签: java swing button layout

所以,我正在开发我的幻灯片游戏,它在网格布局中有16个JButton。其中15个号码为1-15,最后一个号码为空。我现在已经为gui和除了public void actionPerformed(ActionEvent e)之外的所有内容编写了代码。这就是它的样子:( gui)

http://i.stack.imgur.com/rbTt6.jpg

enter image description here

(很抱歉没有在这里发布图片,因为我没有足够的声誉,所以我不能这样做)

例如,当我点击“4”按钮时,它应该像在右边的图片中那样改变。要改变这一点,我知道如何,但水平和垂直检查如果旁边的“空按钮”,我不知道如何。香港专业教育学院试图谷歌,但我发现有什么可以帮助我。我该怎么做?我不是要求你们为我编写代码,不,我在问我该如何解决这个问题呢?

感谢你

1 个答案:

答案 0 :(得分:0)

要遵循的步骤:

  • 查找空单元格的索引
  • 查找当前点击的单元格的索引
  • 获取当前点击的单元格的左,右,上下索引
  • 设置逻辑以验证逻辑移动

     if (emptyIndex == left || emptyIndex == right || emptyIndex == top
                    || emptyIndex == bottom) {
                JButton emptyBtn = btns.get(emptyIndex);
                emptyBtn.setText(btn.getText());
                btn.setText("");
     }
    
  • 如果有效的移动
  • ,则交换空的和当前单击的单元格的文本
  • 那个'全部

这是给你的代码。

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.JPanel;

public class Puzzle {
    private static List<JButton> btns = new ArrayList<JButton>();

    static class MyButtonActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton btn = (JButton) e.getSource();

            int emptyIndex = -1;
            int currentInex = -1;
            for (int i = 0; i < btns.size(); i++) {
                if (btns.get(i).getText().equals("")) {
                    emptyIndex = i;
                } else if (btns.get(i).getText().equals(btn.getText())) {
                    currentInex = i;
                }
            }

            int left = currentInex - 1;
            int right = currentInex + 1;
            int top = currentInex - 4;
            int bottom = currentInex + 4;

            if (emptyIndex == left || emptyIndex == right || emptyIndex == top
                    || emptyIndex == bottom) {
                JButton emptyBtn = btns.get(emptyIndex);
                emptyBtn.setText(btn.getText());
                btn.setText("");
            }
        }
    }

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 4));

        for (int i = 0; i < 15; i++) {
            JButton btn = new JButton(String.valueOf(i));
            btn.addActionListener(new MyButtonActionListener());
            btns.add(btn);
        }
        JButton empty = new JButton("");
        empty.addActionListener(new MyButtonActionListener());
        btns.add(empty);

        Collections.shuffle(btns);
        for (JButton btn : btns) {
            panel.add(btn);
        }

        frame.setTitle("The 15 game");
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}