gpt4 book ai didi

java - 将图标从 jbutton move 到 jbutton

转载 作者:行者123 更新时间:2023-12-02 05:54:07 27 4
gpt4 key购买 nike

我设置了一个网格布局,中间有 16 个按钮。我在第一个按钮上放置了一个图标。

我将如何循环,当用户选择网格上的下一个按钮时,它将图标从旧位置 move 到新位置?

private ArrayList<JButton> grid = new ArrayList<JButton>(); 

JPanel gridBtnPanel = new JPanel();
gridBtnPanel.setLayout(new GridLayout(4, 4));
for(int i = 0; i <= 16; i++){
JButton innerButton = new JButton();
gridBtnPanel.add(innerButton);
grid.add(innerButton);
}

ImageIcon player= new ImageIcon("player.JPG");

//starting position
grid.get(0).setIcon(player);

//wanting to move to next button when I select the near by button
for(int i = 0; i < grid.lastIndexOf(theifPerson); i++){
grid.get(i).setIcon(null);
}

任何帮助都会很棒。

谢谢。

最佳答案

您可以向按钮添加 Action 监听器,一旦按下按钮,它就会搜索所有按钮以找到具有非空图标的按钮,并将按下的按钮的图标与非空图标切换

关于java - 将图标从 jbutton move 到 jbutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23259662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com