2014-02-07 58 views
1

我正在製作一個統計程序來練習我在Java中的GUI技能。將JButton放在堆棧上

我有一個程序,通過按下他們的名字打一個JButton來記錄籃球運動員的統計數據。然後它將統計數據添加到運行總數並更新記分牌。

我已經到了創建撤消按鈕的時候了。

因此,每次執行操作時,我都會將源按鈕添加到一堆JButton中。還有一些涉及鑄造,所以像這樣結束:

JButton source = (JButton) e.getSource(); 
theStack.push(source); 

後來,在actionPerformed方法我試圖通過撤銷函數來調用:

if(source.getText().equals("Undo")){ 
    System.out.println("Undo"); 
    JButton last = this.theStack.pop(); 
    System.out.println(last.getText()); //Works fine. 
    System.out.println(last.getName()); //Produces a null value. 
    int player = Integer.parseInt(last.getName().trim()); 
    undo(player, last.getText(), activePlayers); 
} 

爲什麼我收到了一個空名稱。 Eclipse在嘗試將名稱轉換爲int時發生異常,因爲它正在轉換空值。我在actionPerformed的其他部分使用.getName(),但不是在這裏?

我的名字設置代碼,在for循環中做了很多次。

output[i][j] = new JButton("Make Two Points"); 
output[i][j].setName(i + ""); 

問題是最簡單的形式。

public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 
     ArrayList<Integer> activePlayers = new ArrayList<Integer>(); 
     activePlayers.add(player0Select.getSelectedIndex()); 
     activePlayers.add(player1Select.getSelectedIndex()); 
     activePlayers.add(player2Select.getSelectedIndex()); 
     activePlayers.add(player3Select.getSelectedIndex()); 
     activePlayers.add(player4Select.getSelectedIndex()); 

     JButton source = (JButton) e.getSource(); 
     theStack.push(source); 

     if(source.getText().equals("Make Two Points")){ 
      this.makeTwoPoints(source.getName(), activePlayers); //source.getName() works here. 
      System.out.println("Two Points"); 
     } 
     if(source.getText().equals("Undo")){ 
      System.out.println("Undo"); 
      JButton last = this.theStack.pop(); 
      System.out.println(last.getText()); 
      System.out.println(last.getName()); //last.getName() produces null here. 
      int player = Integer.parseInt(last.getName().trim()); 
      undo(player, last.getText(), activePlayers); 
     } 
} 

回答

1

因爲你永遠不設置JButton的名字,也不應該你。每個組件都有一個可通過setName(...)方法設置的名稱屬性,並且如果setter方法從未被調用,則該名稱爲空。但是這個屬性的意義是什麼?這裏不多。

如果這是我的項目,我不會堆棧JButtons,而是堆疊模型對象,或者控件(Actions)。讓我們不要將我們的模型與我們的觀點混合。


編輯

對於我的意思一個簡單的例子,

你可以有一個StatAction枚舉是有你的三個(或更多統計動作),例如,

public enum StatAction { 
    MAKE_2_PTS("Make Two Points"), MISS_2_PTS("Miss Two Points"), 
    MAKE_3_PTS("Make Three Points"); 

    private String text; 

    private StatAction(String text) { 
     this.text = text; 
    } 

    @Override 
    public String toString() { 
    return text; 
    } 

    public String getText() { 
     return text; 
    } 

} 

你可以有一個播放器類,可以包括名稱字段以及List<StatAction>,例如它可以有...

public class Player { 
    private String name; 
    private List<StatAction> statActionList = new ArrayList<>(); 

    // .... 

    public String getName() { 
     return name; 
    } 

    public void addStatAction(StatAction statAction) { 
     statActionList.add(statAction); 
    } 

    public void removeStatAction(StatAction statAction) { 
     statActionList.remove(statAction); 
    } 

    public void removeLastStatAction() { 
     if (statActionList.size() > 0) { 
     statActionList.remove(statActionList.size() - 1); 
     } 
    } 

    //..... 

} 

然後撤消可以從播放列表中刪除最後一個StatAction。統計數據的顯示可以通過偵聽器實時更改。

+0

當我生成它們時,我確實設置了它們,該名稱對應於列號,它將其轉換爲我的程序中的活動播放器。 –

+0

@TrevorHutto:JVM正在告訴你,否則我相信JVM。請顯示您的設置代碼。 –

+0

@TrevorHutto:您需要創建併發布可測試[mcve](http://stackoverflow.com/help/mcve),以便我們能夠充分理解您的問題。 –