2013-11-09 82 views
1

因此,我正在製作一個遊戲,當我點擊一個JButton時,我想要更改屏幕上已經存在的圖像的圖像,但是我沒有試過,因此我在這裏尋求幫助。如何在JFrame中單擊JButton時更改現有圖像? (Java)

這是我的代碼:

package frametest; 
import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class FrameTest extends JFrame implements ActionListener { 

    JPanel totalGUI, player1, buttons; 
    JLabel p1a, p1b; 
    JButton change1, change2; 
    ImageIcon C2 = new ImageIcon("Pictures\\cards\\C2.png"); 
    ImageIcon SK = new ImageIcon("Pictures\\cards\\SK.png");  
    ImageIcon HJ = new ImageIcon("Pictures\\cards\\HJ.png"); 

    public JPanel createContentPane(){ 
     JPanel totalGUI = new JPanel(); 
     totalGUI.setBackground(new Color(52, 186, 119)); 
     totalGUI.setLayout(null); 

     //Speler 1 
     JPanel player1 = new JPanel(); 
     player1.setLocation(240,431); 
     player1.setSize(190,110); 
     player1.setBackground(new Color(52, 186, 119)); 
     totalGUI.add(player1); 

     JLabel p1a = new JLabel(); 
     p1a.setIcon(C2); 
     p1a.setLocation(0,0); 
     player1.add(p1a); 
     pack(); 

     JLabel p1b = new JLabel(); 
     p1b.setIcon(SK); 
     p1b.setLocation(0,20); 
     player1.add(p1b); 
     pack(); 

     //Knoppen 
     JPanel buttons = new JPanel(); 
     buttons.setLayout(null); 
     buttons.setLocation(700,435); 
     buttons.setSize(200,90); 
     totalGUI.add(buttons); 

     JButton change1 = new JButton("Jack of Hearts"); 
     change1.setLocation(0,0); 
     change1.setSize(200,30); 
     change1.addActionListener(this); 
     buttons.add(change1);  

     JButton change2 = new JButton("King of Spades"); 
     change2.setLocation(0,30); 
     change2.setSize(200,30); 
     change2.addActionListener(this); 
     buttons.add(change2); 

     totalGUI.setOpaque(true); 
     return totalGUI; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == change1) { 
      p1a.setIcon(HJ); 
     } else if(e.getSource() == change2) { 
      p1a.setIcon(SK); 
     } 
    } 

    private static void createAndShowGUI() { 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     JFrame frame = new JFrame("Poker Game"); 

     FrameTest demo = new FrameTest(); 
     frame.setContentPane(demo.createContentPane()); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(1000,600);  
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    }} 

看來我必須寫更多的在這個崗位,以便能夠將它張貼,但我不知道該寫什麼......

回答

1

的問題在於你在方法createContentPane()內重新聲明瞭你的變量。

public JPanel createContentPane(){ 
     JPanel totalGUI = new JPanel(); 
     /**/ 
} 

應該

public JPanel createContentPane(){ 
     totalGUI = new JPanel(); 
     /**/ 
} 

你必須做同樣的player1, buttons, p1a, p1b, change1, change2方法createContentPane()裏面。

+0

哦,我的上帝感謝一大堆!我被困在這個問題上好幾天了,答案很簡單。謝謝! – user2971821