2014-03-01 225 views
0

我三歲就做了一個像寶石迷陣或糖果粉碎的遊戲。我創建了64個按鈕單位8x8。我想改變點擊按鈕的圖像。但我不能。如果你能幫忙,我會很高興。或者我該怎麼辦?點擊這個按鈕改變圖片

編輯:我很抱歉。我無法清楚地解釋我的問題。我的問題,我想改變點擊兩個按鈕的圖片。例如,我點擊按鈕[5],然後點擊按鈕[11]。然後按鈕[5]圖標應該是按鈕[11]圖標,[11]圖標應該是按鈕[5]圖標。

public class butondeneme extends JFrame{ 

private JPanel grid; 
private JFrame jr; 

public butondeneme(){ 
    jr=new JFrame(); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 640, 640); 
    grid=new JPanel(); 
    grid.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
    grid.setLayout(new GridLayout(8,8,5,5)); 

    JButton[] buttons = new JButton[64]; 


//Creating random image for buttons 

     for (int i = 0; i<buttons.length; i++) { 

       Random r = new Random(); 
       int a = r.nextInt(9)+1; 
       switch(a){ 
       case 1 : buttons[i]=new JButton(new ImageIcon("img//Cakal.png")); 
         break; 
       case 2 : buttons[i]=new JButton(new ImageIcon("img//BugsBunny.png")); 
         break; 
       case 3 : buttons[i]=new JButton(new ImageIcon("img//Pig.png")); 
         break; 
       case 4 : buttons[i]=new JButton(new ImageIcon("img//Taz.png")); 
         break; 
       case 5 : buttons[i]=new JButton(new ImageIcon("img//Sam.png")); 
         break; 
       case 6 : buttons[i]=new JButton(new ImageIcon("img//DuffyDuck.png")); 
         break; 
       case 7 : buttons[i]=new JButton(new ImageIcon("img//Tweety.png")); 
         break; 
       case 8 : buttons[i]=new JButton(new ImageIcon("img//Slyvester.png")); 
         break; 
       case 9 : buttons[i]=new JButton(new ImageIcon("img//RoadRunner.png")); 
         break; 
       } 

       //Adding number to find easily 
       String comand=Integer.toString(i); 

          //Get ImageIcon name 
       final String imgName=((ImageIcon)buttons[i].getIcon()).toString(); 



       buttons[i].addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 




         } 
        } 
       }); 
       buttons[i].setActionCommand(comand); 

       grid.add(buttons[i]); 

      } 



    add(grid); 



} 



public static void main(String[] args){ 
    butondeneme erdem=new butondeneme(); 
    erdem.setVisible(true); 
} 



    } 
+1

只需使用'Button.setIcon(newImage)',會做:-) –

回答

3

我認爲你可以使用:

Button.setIcon(newImage); 
0

我假設你正在尋找一個解決方案如何找到哪個按鈕被觸發ActionEvent?如果是的話,它可以通過以下方式進行:

buttons[i].addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 

     JButton button = (JButton) e.getSource(); 
     button.setIcon(<your icon>); 

    } 
}; 
0

這應該工作

import java.awt.GridLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.util.Random; 

    import javax.swing.BorderFactory; 
    import javax.swing.ImageIcon; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 

    public class butondeneme extends JFrame{ 

    private JPanel grid; 
    private JFrame jr; 
    JButton[] buttons; 

    public butondeneme(){ 


     jr=new JFrame(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 640, 640); 
     grid=new JPanel(); 
     grid.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     grid.setLayout(new GridLayout(8,8,5,5)); 

     buttons = new JButton[64]; 


    //Creating random image for buttons 

      for (int i = 0; i<buttons.length; i++) { 

        Random r = new Random(); 
        int a = r.nextInt(9)+1; 
        switch(a){ 
        case 1 : buttons[i]=new JButton(new ImageIcon("img//Cakal.png")); 
          break; 
        case 2 : buttons[i]=new JButton(new ImageIcon("img//BugsBunny.png")); 
          break; 
        case 3 : buttons[i]=new JButton(new ImageIcon("img//Pig.png")); 
          break; 
        case 4 : buttons[i]=new JButton(new ImageIcon("img//Taz.png")); 
          break; 
        case 5 : buttons[i]=new JButton(new ImageIcon("img//Sam.png")); 
          break; 
        case 6 : buttons[i]=new JButton(new ImageIcon("img//DuffyDuck.png")); 
          break; 
        case 7 : buttons[i]=new JButton(new ImageIcon("img//Tweety.png")); 
          break; 
        case 8 : buttons[i]=new JButton(new ImageIcon("img//Slyvester.png")); 
          break; 
        case 9 : buttons[i]=new JButton(new ImageIcon("img//RoadRunner.png")); 
          break; 
        } 

        //Adding number to find easily 
        String comand=Integer.toString(i); 

           //Get ImageIcon name 
        final String imgName=((ImageIcon)buttons[i].getIcon()).toString(); 

        buttons[i].addActionListener(new ButtonListener(i)); 
        add(grid); 

       } 

    } 



    public static void main(String[] args){ 
     butondeneme erdem=new butondeneme(); 
     erdem.setVisible(true); 
    } 

    class ButtonListener implements ActionListener{ 

     int index; 

     public ButtonListener(int index) { 
      this.index=index; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      buttons[index].setIcon(new ImageIcon("new_icon.png")); 
     } 

    } 

    } 
+0

太謝謝你了。不過,我想更改點擊兩個按鈕的圖片。例如,我點擊按鈕[5],然後點擊按鈕[11]。然後按鈕[5]圖標應該是按鈕[11]圖標,[11]圖標應該是按鈕[5]圖標。 – rdmzcn

4

後期編輯:

你只需要使用JButton.setIcon()方法即可獲得所需的效果。儘管可能需要對其進行修改以滿足您的需求,但要讓它適用於您的用例。請在這個連接的代碼示例有alook:

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

public class ButtonIconExample { 

    private JButton[] buttons = new JButton[64]; 
    //private Icon informationIcon = UIManager.getIcon("OptionPane.informationIcon"); 
    //private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon"); 
    private Icon[] icons = {UIManager.getIcon("OptionPane.informationIcon"), 
          UIManager.getIcon("OptionPane.errorIcon"), 
          UIManager.getIcon("OptionPane.warningIcon")}; 
    private int previousIndex; 
    private Random random; 

    private ActionListener buttonActions = new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      JButton button = (JButton) ae.getSource(); 
      if (previousIndex == -1) 
       previousIndex = Integer.parseInt(button.getActionCommand()); 
      else { 
       Icon previousIcon = buttons[previousIndex].getIcon(); 
       Icon currentIcon = button.getIcon(); 
       buttons[previousIndex].setIcon(currentIcon); 
       button.setIcon(previousIcon); 
       previousIndex = -1; 
      } 
     } 
    }; 

    public ButtonIconExample() { 
     previousIndex = -1; 
     random = new Random(); 
    } 

    private void displayGUI() { 
     JFrame frame = new JFrame("Button Icon Example"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridLayout(8, 8, 5, 5)); 
     for (int i = 0; i < buttons.length; i++) { 
      buttons[i] = new JButton(); 
      buttons[i].setIcon(icons[random.nextInt(3)]); 
      buttons[i].setActionCommand("" + i); 
      buttons[i].addActionListener(buttonActions); 
      contentPane.add(buttons[i]); 
     } 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) { 
     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       new ButtonIconExample().displayGUI(); 
      } 
     }; 
     EventQueue.invokeLater(runnable); 
    } 
}