Java 是否垂直和水平检查特定的JButton?
所以,我正在制作我的幻灯片puzzel游戏,它在网格布局中有16个按钮。其中15个有数字1-15,最后一个是空的。我现在已经为gui和除public void actionPerformed(ActionEvent e)之外的所有东西编写了代码。这就是它的外观:(gui) (很抱歉没有在这里发布图片,我不能这样做,因为我没有足够的声誉) 例如,当我单击“4”按钮时,它应该会像右图中的一样发生变化。要改变这一点,我知道怎么做,但要水平和垂直检查“空按钮”是否在它旁边,我不知道怎么做。我试着用谷歌搜索它,但是我没有找到任何可以帮助我的东西。我该怎么做?我不是要求你们为我写代码,不,我是问我应该如何解决这个问题 谢谢以下步骤:Java 是否垂直和水平检查特定的JButton?,java,swing,button,layout,Java,Swing,Button,Layout,所以,我正在制作我的幻灯片puzzel游戏,它在网格布局中有16个按钮。其中15个有数字1-15,最后一个是空的。我现在已经为gui和除public void actionPerformed(ActionEvent e)之外的所有东西编写了代码。这就是它的外观:(gui) (很抱歉没有在这里发布图片,我不能这样做,因为我没有足够的声誉) 例如,当我单击“4”按钮时,它应该会像右图中的一样发生变化。要改变这一点,我知道怎么做,但要水平和垂直检查“空按钮”是否在它旁边,我不知道怎么做。我试着用谷
- 查找空单元格的索引
- 查找当前单击单元格的索引
- 获取当前单击单元格的左、右、上、下索引
- 放置逻辑以验证逻辑移动
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);
}
}
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类难题{
私有静态列表btns=new ArrayList();
静态类MyButtonActionListener实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
JButton btn=(JButton)e.getSource();
int emptyIndex=-1;
int currentInex=-1;
对于(int i=0;i
显示了一种方法。谢谢!非常感谢。