0

我试图在单击我的北按钮后将代表我的播放器耐力的双精度值添加到 jTextArea 中似乎无法做到这一点,这是我的代码:

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();

}
4

2 回答 2

1

您的课程似乎没有 implmeneting 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
        }
    });
于 2013-10-23T12:55:46.713 回答
0

尝试这个。

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

使用任何东西 + 字符串都会自动转换为字符串。

于 2013-10-23T12:45:30.863 回答