2014-04-04 1 views
0

나는 정말 붙어있어. 나는 다른 작품을 알고 있지만 버튼 그래픽 인터페이스에 업데이 트를 표시하지 않으며, 그들은 정적 수 없습니다. 벽에 부딪쳐 라. 어떤 도움을 주셔서 감사합니다! 당신의 ActionListener에서비 정적 인 정적 컨텍스트에서 참조 할 수 없습니다/그래픽 업데이트되지 않습니다

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

public class SlotMachinePanel extends JPanel{ 

private JPanel buttonPanel, newSlotsPanel, primary; 
protected int tokens = 5; 
protected int score = 0; 
private String[] resultOfSpin = {"Winner", "Loser"}; 
public slotPane sPane = new slotPane(); 


public SlotMachinePanel(){ 

    newSlotsPanel = new JPanel(); 
    newSlotsPanel.setPreferredSize(new Dimension(300, 100)); 
    newSlotsPanel.setBackground(Color.WHITE); 
    JLabel slotLabel = new JLabel("Current Tokens: " + getTokens() + " Result of Spin: " + sPane.getScore()); 
    newSlotsPanel.add(slotLabel); 
    newSlotsPanel.add(sPane); 

    buttonPanel = new JPanel(); 
    buttonPanel.setPreferredSize(new Dimension(300, 50)); 
    buttonPanel.setBackground(Color.BLUE); 

     JButton spin = new JButton ("Spin!"); 
     spin.addActionListener (new spinButton()); 

     JButton cashOut = new JButton ("Cash Out"); 
     cashOut.addActionListener (new cashOutButton()); 

     JLabel label = new JLabel ("Spin to play!"); 

     setPreferredSize (new Dimension(300, 40)); 
     setBackground (Color.WHITE); 

    buttonPanel.add(spin); 
    buttonPanel.add(cashOut); 
    buttonPanel.add(label); 

    primary = new JPanel(); 
    primary.setPreferredSize(new Dimension(350, 200)); 
    primary.setBackground(Color.RED); 
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 


    primary.add(newSlotsPanel); 
    primary.add(buttonPanel); 
    add(primary); 
} 

public int getTokens(){ return tokens; } 

public void setTokens(int x){ tokens += x; } 

public int getScore(){ return sPane.getScore(); } 

public void setScore(int x){ sPane.setScore(x); } 

public void spinSlot(){ sPane = new slotPane();} 

public void spinPressed(){ 
    setTokens(-1); 
    sPane.spin(); 
    setTokens(getScore()+sPane.getScore()); 
} 

private class spinButton implements ActionListener{ 
    public void actionPerformed (ActionEvent event){ 
    //this.setText("Spun!"); 
    spinPressed();   
    if(SlotMachinePanel.getTokens() <= 0){ 
     JOptionPane.showMessageDialog(null, "Spun Out! \n You have " + SlotMachinePanel.getTokens() + " tokens!"); 
      System.exit(0); 
    } 
    } 
} 

private class cashOutButton implements ActionListener{ 
    public void actionPerformed (ActionEvent event){ 
    JOptionPane.showMessageDialog(null, "Cashed Out! \n You get " + SlotMachinePanel.getTokens() + " tokens!"); 
    System.exit(0); 
    } 
} 
} 
+2

어쩌면 당신은 문제를 좁혀 야합니다. –

+0

@RC : 범위가 변경 되었기 때문에 작동하지 않을 것입니다. 그래서 컴파일러는'ActionListener'에서'getTokens()'메소드를 찾을 것입니다. – thobens

답변

1

spinButton 당신은 getTokens()에 정적 호출을 확인합니다. 이 값을 SlotMachinePanel.this.getTokens()으로 변경해야합니다. 이 방법을 사용하면 ActionListener에있는 SlotMachinePanel의 현재 인스턴스에 액세스 할 수 있습니다. cashOutButton의 ActionListener도 마찬가지입니다.

관련 문제