2017-03-10 3 views
-3

更新:各ボタンを手動でコーディングしました。とにかく、ありがとう。FIXED:Java Memory Game - Java他の画像を表示しない

私はJavaで書かれたメモリゲームを作ろうとしています。何らかの理由で、Javaはすべてのイメージを同じものとしてレンダリングします。クリックすると、最新の画像がすべてのボタンに表示されます。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 
import java.util.ArrayList; 

import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 

/** 
* @author Steven 
* 
*/ 
public class Memory extends JFrame { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    private static final int GRIDSIZE = 4; 
    private PicButton[][] liteBut = new PicButton[GRIDSIZE][GRIDSIZE]; 
    private Random rand = new Random(); 
    private ClassLoader cl = this.getClass().getClassLoader(); 
    private String[] imagelist = {"images/image01.jpg", "images/image02.jpg", "images/image03.jpg", "images/image04.jpg"}; 
    private ArrayList<String> images = new ArrayList<String>(); 
    private volatile String icon = ""; 

    public Memory() { 
     initGUI(); 
     setTitle("Memory"); 
     setResizable(false); 
     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    private void initGUI() { 
     assignimages(); 
     TitleLabel framedTitle = new TitleLabel("Memory"); 
     add(framedTitle, BorderLayout.PAGE_START); 

     JPanel centerPanel = new JPanel(); 
     centerPanel.setLayout(new GridLayout(GRIDSIZE, GRIDSIZE)); 
     add(centerPanel, BorderLayout.CENTER); 

     for (int row = 0; row < GRIDSIZE; row++) { 
      for (int col = 0; col < GRIDSIZE; col++) { 
       liteBut[row][col] = new PicButton(row, col); 

       if (row == 0) { 
        if (col == 0) { 
         icon = images.get(0); 
         System.out.println(icon); 
        } else if (col == 1) { 
         icon = images.get(1); 
         System.out.println(icon); 
        } else if (col == 2) { 
         icon = images.get(2); 
         System.out.println(icon); 
        } else if (col == 3) { 
         icon = images.get(3); 
         System.out.println(icon); 
        } 

       } else if (row == 1) { 
        if (col == 0) { 
         icon = images.get(0); 
         System.out.println(icon); 
        } else if (col == 1) { 
         icon = images.get(1); 
         System.out.println(icon); 
        } else if (col == 2) { 
         icon = images.get(2); 
         System.out.println(icon); 
        } else if (col == 3) { 
         icon = images.get(3); 
         System.out.println(icon); 
        } 

       } 

       liteBut[row][col].addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         PicButton button = (PicButton) e.getSource(); 
         int row = button.getRow(); 
         int col = button.getCol(); 
         String id = button.getID(); 
         System.out.println("Hi from " + id); 
         liteBut[row][col].setIcon(new ImageIcon(cl.getResource(icon))); 
        } 
       }); 
       centerPanel.add(liteBut[row][col]); 
      } 
     } 

    } 

    private void assignimages() { 
     for (int x = 0; x < 4; x++) { 
      int i = rand.nextInt(GRIDSIZE); 
      images.add(imagelist[i]); 
     } 
     for (int x = 0; x < images.size(); x++) { 
      System.out.println(images.get(x)); 
     } 
    } 

    public static void main(String[] args) { 
     try { 
      String className = UIManager.getCrossPlatformLookAndFeelClassName(); 
      UIManager.setLookAndFeel(className); 
     } catch (Exception e) { 
     } 

     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new Memory(); 
      } 
     }); 
    } 

    public class PicButton extends JButton { 

     private static final long serialVersionUID = 1L; 
     private static final int MAXSIZE = 150; 

     private int row = 0; 
     private int col = 0; 
     private String id = ""; 
     //private Boolean hasPic; 

     public PicButton(int row, int col) { 
      this.row = row; 
      this.col = col; 
      id = Integer.toString(row) + Integer.toString(col); 
      System.out.println(id); 
      setBackground(Color.BLACK); 
      Dimension size = new Dimension(MAXSIZE, MAXSIZE); 
      setPreferredSize(size); 

     } 

     public int getRow() { 
      return row; 
     } 

     public int getCol() { 
      return col; 
     } 

     public String getID() { 
      return id; 
     } 

     public void setImage() { 
      setBackground(Color.RED); 
      //hasPic = true; 
     } 

     public void clearImage() { 
      setBackground(Color.BLACK); 
      //hasPic = false; 
     } 
    } 

    public class TitleLabel extends JLabel { 

     private static final long serialVersionUID = 1L; 

     public TitleLabel(String title) { 
      Font titleFont = new Font(Font.SERIF, Font.BOLD, 32); 
      setFont(titleFont); 
      setHorizontalAlignment(JLabel.CENTER); 
      setText(title); 
      setBackground(Color.BLACK); 
      setForeground(Color.WHITE); 
      setOpaque(true); 
     } 
    } 

} 
+0

コードをダウンロードしていません。関連するビットをここに貼り付けてください。 – shmosel

+1

また、最小限で完全で検証可能な例にしてください。 http://stackoverflow.com/help/mcve –

答えて

1
liteBut[row][col].setIcon(new ImageIcon(cl.getResource(icon))); 

ボタンに画像としてiconの最後の既知の値を割り当てる(それは覚えて、これは、インスタンスのフィールドである)、このボタンに割り当てられた「値」ではなく、さ最後にiconに割り当てられたボタンはfor-loopで作成されていますので、基本的にすべてのボタンが最後にiconを取得し、アイコン値PictureButtonを提供して、ボタンをクリックすると「更新」できます。

この機能はボタン自体に組み込まれているため、さらに管理しやすくなります

+0

これを実装する方法や、for-loopsを捨てて手動でコード化する方法はありますか? –

+0

私が言ったように、アイコンの値を 'PictureButton'に渡して、それを処理して自分自身を更新してください。 – MadProgrammer

+0

@ S.Conawayあなたが試していると思われるものに基づいて、[この例](http://stackoverflow.com/questions/16292498/swingworker-thread-sleep-or-javax-swing-timer-i-need-to-insert-a-pause/16293498#16293498)は役に立ちます – MadProgrammer

関連する問題