【问题标题】:Output thread values into a text box将线程值输出到文本框中
【发布时间】:2013-04-09 16:42:17
【问题描述】:

我正在尝试将银行帐户的值输出到文本框中。该程序是一个银行账户模拟,随着模拟的进行,线程会发生变化。目前这些值正在输出到控制台。

这是当前输出的代码:

public BankAccount()
{
    accountBalance = 0 ;
}

public synchronized void deposit(int addAmount, String name)
{
    // the 'name' argument holds the name of the source of this credit 

    accountBalance+=addAmount ;
    System.out.println(name + " added " + addAmount) ;
    System.out.println("Account balance is now standing at " + accountBalance);
}

public synchronized void withdraw(int takeAmount, String name)
{
    // the 'name' argument holds the name of the bill being paid

    accountBalance-=takeAmount ;
    System.out.println(name + " took " + takeAmount) ;
    System.out.println("Account balance is now standing at " + accountBalance);
}

public int getBalance()
{
    return accountBalance ;
}

【问题讨论】:

  • 您的问题是“如何向我的程序添加 UI?”
  • 我在您的问题中没有看到任何与 UI 相关的代码。你打算使用什么样的文本框?摇摆 ? html ?

标签: java multithreading user-interface


【解决方案1】:

因为您将生成多个线程,每个线程都具有更新单个JTextArea 的能力,您需要注意称为事件队列的概念。 Here 是一个解释您可以采取的方法的页面。

【讨论】:

  • 抱歉信息不足。 Jason,我已经设置了 UI,但是从代码生成的值会输出到控制台。我需要将它们放在面板上的文本框中(女巫已经有了)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 2022-01-13
  • 1970-01-01
相关资源
最近更新 更多