2014-08-31 51 views
1

添加一個私有字段I類有兩個相關的類SpaceInvadersApp和HighScoreTableModel在Java中

我需要一個新的私人領域在SpaceInvadersApp的開頭添加到SpaceInvadersApp類類型HighScoreTableModel的,並初始化這個領域構造函數。

這是我的嘗試,我認爲我沒有做正確。

public class SpaceInvadersApp extends JFrame { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

private final GamePanel game; 

private HighScoreTableModel highScoreTableModel; // unsure about this 

final private JMenuItem menuItemGamePause; 

/** 
* Create new Space Invaders application. 
* @throws HeadlessException 
*/ 
public SpaceInvadersApp() throws HeadlessException { 

    highScoreTableModel = new HighScoreTableModel("Name", "Score"); //unsure about this 

繼承人從HighScoreTableModel類

package spaceinvaders.highscores; 

import java.util.*; 
import javax.swing.event.*; 
import javax.swing.table.*; 

public class HighScoreTableModel implements TableModel { 


private List<String> col1StringList = new ArrayList<String>(); 
private List<Integer> col2IntegerList = new ArrayList<Integer>(); 
private String col1Name, col2Name; 

private List<TableModelListener> listenerList = new ArrayList<TableModelListener>(); 

public HighScoreTableModel(String col1Name, String col2Name) { 
    this.col1Name = col1Name; 
    this.col2Name = col2Name; 
+3

什麼是你的問題? – 2014-08-31 23:46:22

+0

上面的代碼不起作用。看到/ /不確定這個 – Zhauo 2014-08-31 23:47:33

+1

你沒有給所有的'final'字段賦值,但究竟是什麼*不起作用,以及它如何不能工作*? – 2014-08-31 23:48:57

回答

2

考慮找這個類的包培訓相關的代碼中,需要在同一個包或進口。

另一個細節是您的變量menuItemGamePause,它需要進行初始化,因爲它是final

final private JMenuItem menuItemGamePause = new JMenuItem();