【问题标题】:Change JButton ImageIcon on Click单击时更改 JButton ImageIcon
【发布时间】:2014-11-30 04:00:06
【问题描述】:

我正在尝试在 java 中创建一个类似于 Minesweeper 但规则/目标略有不同的 JFrame 应用程序。

我创建了一个 12x12 的 JButton 网格,并有一个 JButton 二维数组。

我正在尝试更改单击按钮时的图像(使其成为 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

【问题讨论】:

    标签: java arrays eclipse jbutton imageicon


    【解决方案1】:

    如果您查看ActionEvent Documentation Page,您会发现每个动作事件都是用Object source 构造的。这意味着如果系统注册了对按钮的单击,则该按钮将作为 source 传递给 ActionEvent 的构造函数。

    因此,您实际上是通过将此对象转换为正确的类来获得正确的按钮。

    [...]
    public void actionPerformed(ActionEvent ae) {
        JButton theRightButton = (JButton) ae.getSource();
        // do stuff with the button...
    }
    [...]
    

    【讨论】:

    • 您也可以使用ActionEvent 的actionCommand 属性,该属性默认为按钮的文本,或者可以在按钮本身上设置,作为盲投或必须使用的替代方法instanceof...您还可以使用Action,允许 OP 传递受操作影响的对象的引用...
    • 这正是我所需要的。感谢您的帮助!
    【解决方案2】:

    使用切换按钮

    http://docs.oracle.com/javase/7/docs/api/javax/swing/JToggleButton.html

    所以 swing 会在按钮模型中为您跟踪状态。

    扩展按钮以在构造函数中传递 x,y 坐标并将它们存储为字段

    将相同的事件侦听器附加到所有按钮,将源转换为按钮类并检索已单击按钮的 x/y

    在活动中将按下的图标动态更改为我的或数字

    如果您想复制此规则,请对所有相邻的空白字段调用 click,但请确保检查按下状态,以免循环回已处理的邻居。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多