【问题标题】:adding a double to a JTextArea?向 JTextArea 添加双精度?
【发布时间】:2013-10-23 12:36:26
【问题描述】:

我试图在单击我的北按钮后将代表我的玩家耐力的双精度值添加到 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();

}

【问题讨论】:

  • 你的意思是要在JTextArea中显示耐力值吗?
  • 那有什么问题呢?
  • 你在课堂上实现了动作监听器吗?
  • 可怜的调试器:试着放一个 System.out.println("Here");上面代码中的语句并单击按钮,如果它不打印代码永远不会到达这一点,因此侦听器有缺陷,这应该有助于缩小问题范围。
  • 很明显 .. 但是您是否为 JTextArea 设置了行/列? JTextArea 是否正在显示?

标签: java swing jtextarea


【解决方案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
        }
    });

【讨论】:

    【解决方案2】:

    试试这个。

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

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

    【讨论】:

    • 对不起,我错过了按钮部分,您对动作监听器的回复确实是他需要的。
    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多