Java 单击“更改JButton图像”图标
我正在尝试用java创建一个JFrame应用程序,它与“扫雷者”类似,但规则/目标略有不同 我已经创建了一个JButton网格,12x12,并且有一个JButton 2D数组 我正在尝试更改按钮单击时的图像(使其成为X或金块图像)。如果每个按钮都有一个单独的名称,我知道该怎么做,但创建144个单独的按钮并为每个按钮命名似乎不符合逻辑 因此,我需要做的是在该按钮的单击事件上,更改/设置该按钮的图像,但在我的动作侦听器中,我只能在知道该按钮的特定数组坐标时才能计算出来 我的问题是如何更改特定按钮的图像?或者如何获取按钮[?][?]的值,以便更改该按钮的图像 谢谢Java 单击“更改JButton图像”图标,java,arrays,eclipse,jbutton,imageicon,Java,Arrays,Eclipse,Jbutton,Imageicon,我正在尝试用java创建一个JFrame应用程序,它与“扫雷者”类似,但规则/目标略有不同 我已经创建了一个JButton网格,12x12,并且有一个JButton 2D数组 我正在尝试更改按钮单击时的图像(使其成为X或金块图像)。如果每个按钮都有一个单独的名称,我知道该怎么做,但创建144个单独的按钮并为每个按钮命名似乎不符合逻辑 因此,我需要做的是在该按钮的单击事件上,更改/设置该按钮的图像,但在我的动作侦听器中,我只能在知道该按钮的特定数组坐标时才能计算出来 我的问题是如何更改特定按钮的图
public class GoldPanel extends JPanel{
ImageIcon xImage = new ImageIcon("x.png");
ImageIcon goldImage = new ImageIcon("");
losingButtonListener loseButton = new losingButtonListener();
winningButtonListener winButton = new winningButtonListener();
JButton[][] button = new JButton[12][12];
//creates the layout
GridLayout layout = new GridLayout(12,12);
Random myRand = new Random();
public GoldPanel(){
//creates panel for name/title/score/etc
JPanel titlePanel = new JPanel();
add(titlePanel);
JLabel title = new JLabel("Welcome to the Goldmine Game!");
titlePanel.add(title);
//creates panel for the game board
JPanel gamePanel = new JPanel();
add(gamePanel);
gamePanel.setLayout(layout);
for(int i=0;i<12;i++)
{
for(int j=0;j<12;j++)
{
button[i][j] = new JButton(" ");
gamePanel.add(button[i][j]);
button[i][j].addActionListener(loseButton);
}
}
button[0][0].addActionListener(winButton);
}//end constuctor
private class losingButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}//actionPerformed
}//buttonListener
private class winningButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("you win");
}//actionPerformed
}//winningButtonListener
}//end GoldPanel class
公共类GoldPanel扩展了JPanel{
ImageIcon xImage=新的ImageIcon(“x.png”);
ImageIcon goldImage=新的ImageIcon(“”);
losingButtonListener loseButton=新建losingButtonListener();
winningButtonListener winButton=新建winningButtonListener();
JButton[][]按钮=新JButton[12][12];
//创建布局
GridLayout=新的GridLayout(12,12);
Random myRand=新的Random();
公共黄金小组(){
//为姓名/头衔/分数等创建面板
JPanel titlePanel=新的JPanel();
添加(标题板);
JLabel title=新JLabel(“欢迎来到金矿游戏!”);
标题板添加(标题);
//为游戏板创建面板
JPanel gamePanel=新的JPanel();
添加(游戏面板);
gamePanel.setLayout(布局);
对于(int i=0;i如果您查看,您会看到每个操作事件都是由对象源
构造的。这意味着,如果系统注册单击按钮,则此按钮将传递给ActionEvent
的构造函数作为源
因此,通过将此对象强制转换到正确的类,您实际上获得了正确的按钮
[…]
已执行的公共无效行动(行动事件ae){
JButton theRightButton=(JButton)ae.getSource();
//用按钮做东西。。。
}
[...]
使用切换按钮
所以swing在按钮模型中为您跟踪状态
扩展按钮以传递构造函数中的x,y坐标,并将它们存储为字段
将同一事件侦听器附加到所有按钮,将源代码转换为button类,并检索已单击的按钮的x/y
在活动中,动态地将按下的图标更改为mine或number
如果要复制此规则,请单击所有相邻的空字段,但请确保检查已按下的状态,以免循环返回已处理的相邻字段。您还可以使用ActionEvent
的actionCommand
属性,该属性默认为按钮的文本或可在按钮本身上设置,作为盲施法或必须使用instanceof
…的替代方法,您还可以使用操作
,允许OP传递受该操作影响的对象的引用…这正是我所需要的。感谢您的帮助!