在Java中使用监听器将按钮链接到图片 [英] Link a button to a picture using a listener in Java

查看:0
本文介绍了在Java中使用监听器将按钮链接到图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java创建一个记忆游戏。类似于此,但更简单->http://www.zefrank.com/memory/

以下是我的代码:

import javax.swing.*;

public class Memoriin {

    public static void main(String[] args) {
        JFrame frame = new MemoriinFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

和:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MemoriinFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    public static final int DEFAULT_HEIGHT = 600;
    public static final int DEFAULT_WIDTH = 800;
    public JButton button[] = new JButton[8];
    ArrayList<ImageIcon> icons = new ArrayList<ImageIcon>();
    ImageIcon tail = new ImageIcon("foto.jpg");

    ImageIcon photo1 = new ImageIcon("foto1.jpg");
    ImageIcon photo2 = new ImageIcon("foto2.jpg");
    ImageIcon photo3 = new ImageIcon("foto3.jpg");
    ImageIcon photo4 = new ImageIcon("foto4.jpg");
    ImageIcon photo1copy = photo1;
    ImageIcon photo2copy = photo2;
    ImageIcon photo3copy = photo3;
    ImageIcon photo4copy = photo4;



    public MemoriinFrame() {
          setTitle("Memory Game");
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
          setLayout(new GridLayout(2, 4));

          addIcons();
          for(int i = 0; i <= 7; i++) {
              button[i] = new JButton();
              button[i].setIcon(tail);
              button[i].addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      performActionEventHandler();
                  }
              });
              add(button[i]);
          }

    }

    public void performActionEventHandler() {
        // how can I link each button with a specific picture?
    }

    public void addIcons() {
        icons.add(photo1);
        icons.add(photo2);
        icons.add(photo3);
        icons.add(photo4);
        icons.add(photo1copy);
        icons.add(photo2copy);
        icons.add(photo3copy);
        icons.add(photo4copy);
        Collections.shuffle(icons);
    }

    public void tailToImage(JButton button) {
        button.setIcon(icons.get(0));
        icons.remove(0);
    }
}

因此,我正在尝试将按钮与特定图片链接。我试图这样做,但得到了一个不必要的结果:如果我单击一个按钮,则图片会变为随机图片。但我有8个按钮和8个图片,我想链接他们,这样每个按钮去与相同的图片整个游戏。

附注:英语不是我的母语。

推荐答案

为了将按钮和图片相关联,最好在它们之间建立映射。您可以使用类似的内容。

Map<JButton, ImageIcon>

以上是按钮和图标之间的一种非常粗略的关系。你可能不得不在这件事上即兴发挥。像这样的事情..

图像来源:对于foto1到foto4,我从Stackoverflow中获取了前4名用户的头像。

ImageIcon photo1 = new ImageIcon("foto1.jpg");
ImageIcon photo2 = new ImageIcon("foto2.jpg");
ImageIcon photo3 = new ImageIcon("foto3.jpg");
ImageIcon photo4 = new ImageIcon("foto4.jpg");
ImageIcon photo1copy = new ImageIcon("foto1.jpg");
ImageIcon photo2copy = new ImageIcon("foto2.jpg");
ImageIcon photo3copy = new ImageIcon("foto3.jpg");
ImageIcon photo4copy = new ImageIcon("foto4.jpg");

Map<JButton, ImageIcon> buttonImage = new HashMap<JButton, ImageIcon>();

public MemoriinFrame() {
      setTitle("Memory Game");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
      setLayout(new GridLayout(2, 4));

      for(int i = 0; i <= 7; i++) {

          button[i] = new JButton();
          button[i].setIcon(tail);
          button[i].addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  performActionEventHandler((JButton)e.getSource());
              }
          });
          add(button[i]);
      }

      addIcons();

}

public void performActionEventHandler(JButton clickedButton) {
    clickedButton.setIcon(buttonImage.get(clickedButton));
}

public void addIcons() {
    icons.add(photo1);
    icons.add(photo2);
    icons.add(photo3);
    icons.add(photo4);
    icons.add(photo1copy);
    icons.add(photo2copy);
    icons.add(photo3copy);
    icons.add(photo4copy);
    Collections.shuffle(icons);

    for(int i=0;i<icons.size();i++){
        buttonImage.put(button[i], icons.get(i));
    }
}

注意:这不是一个完全没有错误的答案,因为我只是在玩弄它。而且它还有很大的重构余地。但这应该足以让你振作起来。

这篇关于在Java中使用监听器将按钮链接到图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆