Java 向JTextArea添加一个double?
当我点击我的北方按钮后,我试图在jTextArea中添加一个代表我的球员耐力的双倍值。我的代码如下:Java 向JTextArea添加一个double?,java,swing,jtextarea,Java,Swing,Jtextarea,当我点击我的北方按钮后,我试图在jTextArea中添加一个代表我的球员耐力的双倍值。我的代码如下: private void northButtonActionPerformed(java.awt.event.ActionEvent evt) { game.playerMove(MoveDirection.NORTH); update(); double playerStamina
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();
}
public类LemurIslandUI扩展了javax.swing.JFrame
{
私人游戏;
私人玩家;
/**
*为狐猴岛创建一个新的JFrame。
*
*@param game要在此帧中显示的游戏对象
*/
公共LemurIslandUI(最终游戏)
{
这个游戏=游戏;
初始化组件();
createGridSquarePanels();
更新();
}
私有void createGridSquarePanels(){
int rows=game.getIsland().getNumRows();
int columns=game.getIsland().getNumColumns();
LemurIsland.removeAll();
setLayout(新的GridLayout(行、列));
对于(int row=0;row
试试这个
jtextraea1.setText(“耐力:+player.getStaminaLevel());
使用anything+string会自动转换为string。您的类似乎没有实现
ActionListener
,因此不会触发按钮上的操作
你的班级声明应该是:
public class LemurIslandUI extends javax.swing.JFrame implements ActionListener
并将按钮操作的代码放入:
public void actionPerformed(ActionEvent e) {}
或者,您可以使用匿名类
来实现按钮的代码,而不是让类实现ActionListener
。类似于:
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
//code
}
});
你的意思是你想在JTextArea中显示耐力值吗?那么它有什么问题吗?你在你的类中实现了action listener吗?可怜的人调试器:尝试放置System.out.println(“此处”);在上面的代码中声明,然后单击按钮,如果它没有打印,则代码永远不会到达这一点,因此侦听器存在缺陷,这将有助于缩小问题的范围。很明显..但是您是否为JTextArea设置了行/列?是否显示JTextArea?很抱歉,我错过了按钮部分,您关于操作侦听器的答复是r他真正需要的是什么。