2014-02-22 19 views
0

這是我第一次嘗試在Java中使用GUI佈局。我試圖創建一個簡單的記憶卡遊戲,用戶翻轉兩張牌,並試圖獲得一場比賽,如果沒有比賽,他們會翻轉過來,如果有一場比賽他們會一直翻轉,直到您獲得所有比賽。雖然我將整個遊戲變成動態的構造函數變量,這些變量定義了列數和行數,但我可能會讓自己變得很難。我認爲這比硬編碼某個值更好,但現在我無法將圖像放入我的img文件夾中。用獨特的ImageIcons動態初始化JButtons

我知道變量的變量在Java &中是不允許的,這對我來說很難適應作爲ColdFusion開發人員。你能幫我想辦法在Java中正確實現這一點嗎?

這是我的代碼的簡化版本。

import javax.swing.JFrame; 

public class MemoryApp 
{ 
    public static void main(String args[]) 
    { 
     //Creates a new game with 3 columns and 4 rows 
     final CardGame myGame = new CardGame(3, 4); 

     javax.swing.SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowGUI(myGame.getCols(), myGame.getRows()); 
      } 
     }); 
    } 

    private static void createAndShowGUI(int c, int r) { 
     //Create and set up the window. 
     Window frame = new Window("GridLayoutDemo", c, r); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //Set up the content pane. 
     frame.addComponentsToPane(frame.getContentPane()); 
     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

紙牌遊戲類:

public class CardGame 
{ 
    private int cols, rows; 

    public CardGame(int c, int r) 
    { 
     cols = c; 
     rows = r; 
    } 

    public int getCols(){ 
     return cols; 
    } 

    public int getRows(){ 
     return rows; 
    } 
} 

與網格佈局的窗口:

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.GridLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JSeparator; 

public class Window extends JFrame 
{ 
    private int cols, rows; 
    GridLayout windowGrid = new GridLayout(1,1); 

    public Window(String t, int c, int r) 
    { 
     super(t); 
     cols = c; 
     rows = r; 
     windowGrid.setColumns(c); 
     windowGrid.setRows(r); 
    } 

    public void addComponentsToPane(final Container pane) 
    { 
     final JPanel compsToExperiment = new JPanel(); 
     compsToExperiment.setLayout(windowGrid); 
     JPanel controls = new JPanel(); 
     controls.setLayout(new GridLayout(cols,rows)); 

     int countCard = (cols * rows)/2; 

     /** 
     * Add buttons to experiment with Grid Layout. 
     * This is where I'd like to be able to loop through 
     * countCard and create the required number of buttons 
     * as well as put images on the buttons like so: 
     * 
     * ImageIcon image1 = new ImageIcon(getClass().getResource("card1.png")); 
     * through 
     * ImageIcon image1 = new ImageIcon(getClass().getResource("card" & cardCount & ".png")); 
     * 
     * Below is how I would attempt this in ColdFusion- I know 
     * the variable of variables syntax is invalid, it is just 
     * to show you what I mean. 
     */ 

     // for(int i = 0; i < countCard; i++;) 
     // {  
     //  compsToExperiment.add(new JButton("../img/card" & i & ".png")); 
     //  ImageIcon variables["image" & i] = new ImageIcon(getClass().getResource("card" & i & ".png")); 
     //  imageButton.setIcon(variables["image" & i]); 

     //  etc. with ButtonClickEventHandler, haven't gotten that far yet 
     // } 

     pane.add(compsToExperiment, BorderLayout.NORTH); 
     pane.add(new JSeparator(), BorderLayout.CENTER); 
    } 
} 

回答

1

基於代碼註釋掉,您張貼到目前爲止代碼,一種方法可能是像這樣:

public class Window extends JFrame 
{ 
    ... 

    // A java.util.List that stores all the buttons, so 
    // that their icons may be changed later 
    private List<JButton> buttons = new ArrayList<JButton>(); 

    // A java.util.List that stores all the ImageIcons that 
    // may be placed on the buttons 
    private List<ImageIcon> imageIcons = new ArrayList<ImageIcon>(); 

    public void addComponentsToPane(final Container pane) 
    { 
     ... 

     for(int i = 0; i < countCard; i++;) 
     {  
      // String concatenation is done with "+" in Java, not with "&" 
      String fileName = "card" + i + ".png"; 

      // Create the icon and the button containing the icon 
      ImageIcon imageIcon = new ImageIcon(getClass().getResource(fileName)); 
      JButton button = new JButton(imageIcon); 

      // Add the button to the main panel 
      compsToExperiment.add(button); 

      // Store the button and the icon in the lists 
      // for later retrieval 
      imageIcons.add(imageIcon); 
      buttons.add(button); 

      // Attach an ActionListener to the button that will 
      // be informed when the button was clicked. 
      button.addActionListener(createActionListener(i)); 
     } 
    } 


    private ActionListener createActionListener(final int cardIndex) 
    { 
     return new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       clickedCardButton(cardIndex); 
      } 
     }; 
    } 

    private void clickedCardButton(int cardIndex) 
    { 
     System.out.println("Pressed button for card "+cardIndex); 

     // Here you can change the icons of the buttons or so... 
     JButton button = buttons.get(cardIndex); 
     ImageIcon imageIcon = imageIcons.get(cardIndex); 
     .... 
    } 

Y你提到這是你第一次嘗試用Java構建GUI。所以我認爲這只是爲了「練習」。如果你的意圖是建立一個「真正的應用程序」,你應該考慮一些模型 - 視圖 - 控制器(MVC)方法。

+0

非常酷 - 這個編譯精美,我可以很容易地跟隨。正確的,這或多或少是一種實踐。我不明白MVC的概念足以實現它。我想我需要以某種方式使用列表,這真的有助於點擊。 –