2013-10-23 46 views
0

我尝试添加一个代表我的球员的体力到一个JTextArea双值后,我点击我的北按钮不能似乎做我的继承人代码:向JTextArea添加一个double?

private void northButtonActionPerformed(java.awt.event.ActionEvent evt) 
{            
    game.playerMove(MoveDirection.NORTH); 
    update(); 
    double playerStamina = player.getStaminaLevel(); 

    //tried this 
    String staminaLevel = Double.toString(playerStamina); 
    jTextArea1.setText("Stamina: " + staminaLevel); 
}     

即时通讯新这里对不起,如果这是不正确的

继承人的主要方法

public class Main 
{ 
/** 
* Main method of Lemur Island. 
* 
* @param args the command line arguments 
*/ 
public static void main(String[] args) 
{ 
    // create the game object 
    final Game game = new Game(); 
    // create the GUI for the game 
    final LemurIslandUI gui = new LemurIslandUI(game); 
    // make the GUI visible 
    java.awt.EventQueue.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      gui.setVisible(true); 
     } 
    }); 
} 

和继承人的类

public class LemurIslandUI extends javax.swing.JFrame 
{ 
private Game game; 
private Player player; 
/** 
* Creates a new JFrame for Lemur Island. 
* 
* @param game the game object to display in this frame 
*/ 
public LemurIslandUI(final Game game) 
{ 
    this.game = game;  
    initComponents(); 
    createGridSquarePanels(); 
    update();  
} 

private void createGridSquarePanels() { 

    int rows = game.getIsland().getNumRows(); 
    int columns = game.getIsland().getNumColumns(); 
    LemurIsland.removeAll(); 
    LemurIsland.setLayout(new GridLayout(rows, columns)); 

    for (int row = 0; row < rows; row++) 
    { 
     for (int col = 0; col < columns; col++) 
     { 
      GridSquarePanel panel = new GridSquarePanel(game, row, col); 
      LemurIsland.add(panel); 
     } 
    } 
} 

/** 
* Updates the state of the UI based on the state of the game. 
*/ 
private void update() 
{ 
    for(Component component : LemurIsland.getComponents()) 
    { 
     GridSquarePanel gsp = (GridSquarePanel) component; 
     gsp.update(); 
} 
    game.drawIsland(); 

} 
+0

您的意思是您想要在JTextArea中显示耐力值吗? – Levenal

+0

那么它有什么问题? – Boann

+0

你是否在你的类中实现了动作监听器? –

回答

1

你的班级似乎没有暗示ActionListener,因此你的按钮上的动作不会被触发。

类声明应该是:

public class LemurIslandUI extends javax.swing.JFrame implements ActionListener 

而且把代码为你的按钮动作中:

public void actionPerformed(ActionEvent e) {} 

或者,您也可以使用anonymous class来实现你的按钮的代码,而不是让你的班级实施ActionListener。例如:

final JButton button = new JButton(); 

    button.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent actionevent) 
     { 
      //code 
     } 
    }); 
0

试试这个。

jTextArea1.setText("Stamina: " + player.getStaminaLevel());

使用任何+字符串做自动转换为String。

+0

我怀疑这会解决原来的问题。 –

+0

对不起,我错过了按钮部分,你对动作监听者的回复确实是他需要的。 –