2013-01-19 32 views
2

本質上,我編碼的是一款益智遊戲。新創建的JButton不會覆蓋以前創建的JButtons

它包含一個圖像,圖像被進一步分成9個部分,放置在包含3x3 JButton GridLayout的JPanel上。最初,9個按鈕是空的。當用戶點擊「開始遊戲」時,9個按鈕將顯示按鈕上的圖像。

我用setPreferredSize()來設置包含9個空的JButton的JPanel的大小。之後,我使用Inset(0,0,0,0)使按鈕的內容填充整個按鈕。

但現在,當我想添加圖像按鈕來替換空的按鈕,當用戶點擊「開始遊戲」,它不起作用。

我認爲這是因爲我之前設置的setPreferredSize()正在阻止Insets的值工作。

我插入了一些system.out.println值來檢查方法是否正在運行,它會運行,但當用戶單擊「開始遊戲」時圖像仍然拒絕出現在按鈕上。

public class GameFrame extends JFrame implements ActionListener { 
     private JButton button1; 
     private JButton[] button = new JButton[9]; 
     private Insets buttonMargin; 
     private boolean testImageMethod; 
     private JPanel puzpiece; 

     public GameFrame(){ 
      //.. coding .. 

      // create new buttons - button1 
      button1 = new JButton("Start Game"); 
      // add action event to "Start" button 
      button1.addActionListener(this); 

      // creates a new panel for the splitted puzzle pieces 
      puzpiece = new JPanel(); 
      puzpiece.setLayout(new GridLayout(3,3)); 

      // check if testImageMethod boolean (in setupImage()) is true, 
      //if it isn't, adds 9 buttons w/o images. 
      for(int a=0; a<9; a++){ 
       if(testImageMethod){ 
       } 
       else{ 
        // adds 9 buttons without images 
        button[a] = new JButton(); 
        puzpiece.add(button[a]); 
        puzpiece.setPreferredSize(new Dimension(500,200)); 
       } 


      } 
      // adds puzpiece panel into the frame 
       this.add(puzpiece,BorderLayout.WEST);  
      //.. coding .. 

     } 

     public void actionPerformed(ActionEvent e){ 
      if (e.getSource() == button1){ 
       // puzpiece.button.setVisible(false); 
       //puzpiece.remove(button); 

       // call setImage() method 
       setImage(); 

       for(int a=0; a<9; a++){ 
       // adds the new 9 buttons with images into panel 
       puzpiece.add(button[a]); 
       // test if method is running 
       System.out.println("qq"); 
       } 
      } 
      else{ 
       System.out.println("bbb"); 
      } 
     } 

     // method setImage() divides the image into subimages 
     public void setImage(){ 
      //.. coding .. 
      // test if method is running 
      System.out.println("a"); 

      setupImage(count++, sc); 
     } 

     // method setupImage() adds the subimages to the buttons 
     private void setupImage(int a, Image wi) 
     { 
      // test if method is running 
      System.out.println("d"); 

      buttonMargin = new Insets(0, 0, 0, 0); 
      button[a] = new JButton(new ImageIcon(wi)); 
      button[a].setMargin(buttonMargin); 

      // test if method is running 
      System.out.println("e"); 

     } // end method setupImage() 
    } 
+2

爲什麼要更換JButtons?爲什麼不簡單地交換JButtons顯示的ImageIcons? –

+0

請檢查編輯到@ GagandeepBali的答案。他提供了一些很好的清晰示例代碼來說明他的觀點。 –

回答

4

我不知道我確切地知道你在做什麼,但是,...

  • 它看起來像你正在填充一個3x3的普通JButtons網格的JPanel,
  • 並且按鈕上的按鈕顯示一個圖像的JButton中按下。
  • 但我沒有看到您在添加新按鈕之前刪除原始按鈕。
  • 在更換組件後,我也看不到您在puzpiece JPanel上調用revalidate(),然後repaint()
  • 甚至更​​重要的是,爲什麼在交換JButton的時候,更容易在已經由puzpiece JPanel持有的JButton中交換ImageIcons?這是我10分鐘前在評論中推薦的內容,但現在正在回答。
+0

感謝您的提示。是的,你所說的話反映了我想做的事情。我嘗試在以前的試驗和錯誤中調用revalidate()和repaint(),但它創建了additonal按鈕,而不是覆蓋原始的9個按鈕:O – iridescent

4

只需setIcon的說JButton,不重新添加JButtonJPanel,已經可以看到

一個小例子對於相同的:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

/** 
* Created with IntelliJ IDEA. 
* User: Gagandeep Bali 
* Date: 1/19/13 
* Time: 10:05 AM 
* To change this template use File | Settings | File Templates. 
*/ 
public class ButtonImageTest 
{ 
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon"); 
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon"); 
    private JButton button; 
    private int counter = 1; 

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

     JPanel contentPane = new JPanel(); 
     button = new JButton(); 
     button.setBorderPainted(false); 
     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (counter % 2 != 0) 
       { 
        button.setIcon(errorIcon); 
        counter = 2; 
       } 
       else 
       { 
        button.setIcon(infoIcon); 
        counter = 1; 
       } 
      } 
     }); 
     contentPane.add(button); 

     frame.setContentPane(contentPane); 
     frame.setSize(100, 100); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 
    public static void main(String... args) 
    { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new ButtonImageTest().displayGUI(); 
      } 
     }); 
    } 
} 
+1

是的,正如我在我之前發表的評論中所述。+ –

+0

很好的詳細示例代碼!我會給OP留言,回顧一下,因爲他可能還沒有看到它。 –

+0

Nah,沒關係,基本上我只是爲未來的訪問者添加的,你的答案已經得到了每個細節OP都在尋找:-) Thankyou雖然爲欣賞答案:-) –

相關問題