更新文本区域
Posted
技术标签:
【中文标题】更新文本区域【英文标题】:Updating a text area 【发布时间】:2012-10-28 04:32:42 【问题描述】:我在更新或“重新绘制”文本区域时遇到问题。我希望在 for 循环的每个循环中。我尝试了一些方法,例如调用paint();
和repaint();
函数以及使用text.append();
和text.setText();
,但这似乎不起作用。我还设置了 for 循环,以便它应该在每次迭代时附加文本。这是我的代码,非常感谢任何帮助。
尼克。
package ********_****_rps;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AutomatedRoshamboRPS extends javax.swing.JFrame
int num = 0;
int compnum = 0;
int earnings = 0;
int wins = 0;
int losses = 0;
int ties = 0;
int roshamboCount = 0;
int gamesPlayed = 0;
int i;
int games = 0;
String a = "", b;
void updateStats()
String statsToString = stats.getText();
if (num - compnum == 1 || num - compnum == -2) //won
wins++;
gamesPlayed++;
else if (num - compnum == 0) //tied
ties++;
gamesPlayed++;
else //lost
losses++;
gamesPlayed++;
earnings = +(wins * 2) - (losses * 2) + (roshamboCount * 10) - (roshamboCount * 2);
stats.setText("STATISTICS:"
+ "\nGames Played:" + gamesPlayed
+ "\nEarnings:" + earnings
+ "\nWins:" + wins
+ "\nTies:" + ties
+ "\nLosses:" + losses
+ "\nRoshambo:" + roshamboCount);
void game()
String userChoice;
compnum = (int) Math.floor((Math.random() * 3) + 1);
switch (num)
case 1:
userChoice = "ROCK";
break;
case 2:
userChoice = "PAPER";
break;
case 3:
userChoice = "SCISSORS";
break;
default:
userChoice = "ROCK";
if (num - compnum == 1 || num - compnum == -2)
text.append("\nYou won!");
a += userChoice;
else if (num - compnum == 0)
text.append("\nYou tied!");
else
text.append("\nYou lost!");
a = "";
if (a.equals("ROCKROCKROCK") || a.equals("PAPERPAPERPAPER") || a.equals("SCISSORSSCISSORSSCISSORS"))
roshamboCount++;
a = "";
if (text.getLineCount() > 10000)
text.setText("");
public AutomatedRoshamboRPS()
initComponents();
text.setText("ROCK PAPER SICSSORS"
+ " \nSelect your throw by clicking on one \nof the options to the left.\n"
+ "---------------------------------------------------");
rock.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
b = numGames.getText();
games = Integer.parseInt(b);
for (int l = 0; l < games; l++)
num = 1;
game();
updateStats();
);
paper.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
b = numGames.getText();
games = Integer.parseInt(b);
for (int l = 0; l < games; l++)
num = 2;
game();
updateStats();
);
scissors.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
b = numGames.getText();
games = Integer.parseInt(b);
for (int l = 0; l < games; l++)
num = 3;
game();
updateStats();
);
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
text = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
stats = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
numGames = new javax.swing.JTextField();
rock = new javax.swing.JToggleButton();
paper = new javax.swing.JToggleButton();
scissors = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jLabel1.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
jLabel1.setText("Automated Roshambo");
text.setColumns(20);
text.setEditable(false);
text.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
text.setLineWrap(true);
text.setRows(5);
jScrollPane1.setViewportView(text);
stats.setColumns(20);
stats.setEditable(false);
stats.setRows(5);
jScrollPane2.setViewportView(stats);
jLabel5.setText("Number of games");
jLabel6.setText("to simulate:");
rock.setIcon(new javax.swing.ImageIcon(getClass().getResource("/haughton_nick_rps/Rock.png"))); // NOI18N
rock.setContentAreaFilled(false);
paper.setIcon(new javax.swing.ImageIcon(getClass().getResource("/haughton_nick_rps/Paper.png"))); // NOI18N
paper.setContentAreaFilled(false);
scissors.setIcon(new javax.swing.ImageIcon(getClass().getResource("/haughton_nick_rps/Scissors.png"))); // NOI18N
scissors.setContentAreaFilled(false);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jLabel1)
.add(80, 80, 80))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(36, 36, 36)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(6, 6, 6)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(rock, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(scissors, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(paper))
.add(35, 35, 35)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel3)
.add(jLabel2)
.add(jLabel4)))
.add(jLabel5)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, numGames)
.add(org.jdesktop.layout.GroupLayout.LEADING, jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.add(18, 18, 18)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 244, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(32, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(18, 18, 18)
.add(jLabel1)
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane1)
.add(layout.createSequentialGroup()
.add(jLabel5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel6)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(numGames, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(0, 0, Short.MAX_VALUE)
.add(jLabel2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel4)
.add(208, 208, 208))
.add(layout.createSequentialGroup()
.add(rock, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(paper, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(scissors, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 0, Short.MAX_VALUE)))))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 125, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
// </editor-fold>
public static void main(String args[])
java.awt.EventQueue.invokeLater(new Runnable()
public void run()
new AutomatedRoshamboRPS().setVisible(true);
);
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField numGames;
private javax.swing.JToggleButton paper;
private javax.swing.JToggleButton rock;
private javax.swing.JToggleButton scissors;
private javax.swing.JTextArea stats;
private javax.swing.JTextArea text;
// End of variables declaration
【问题讨论】:
Timer, 可以应用于您的案例吗?看看这个example,可能会有所帮助。 否则你可以看看paintImmediately(Rectangle rec)或paintImmediately(int x, int y, int w, int h)看看这个example。 @GagandeepBali 他在 EDT 内循环,阻止任何重绘请求! @MadProgrammer :但paintImmediately()
(两种变体)方法必须在 EDT 上运行。尽管提供的代码中的一些代码片段似乎更适合 SwingWorker,如您的回答中所述。
@GagandeepBali 我对你的建议的问题不是在 EDT 上进行呼叫,这很好,这是 EDT 被阻止并且无法处理重绘的事实要求。老实说,OP 的程序并没有做任何复杂的事情需要paintImmediately
;) - 恕我直言
【参考方案1】:
“随着 for 循环的每个循环而更新” - 有问题。
据我所知,您的程序只是在 EDT 的内容中进行所有更新,从而违反了事件调度线程。这会阻止 UI 开始更新。
您可能希望阅读Concurrency in Swing 以了解更多信息。
更新
因此,我查看了您的代码并(尽我所能)对其进行了修改,以改用SwingWoker
。我玩了 2000 场游戏没有问题。
public class TestRockPaperScissors
public static void main(String[] args)
new TestRockPaperScissors();
public TestRockPaperScissors()
EventQueue.invokeLater(new Runnable()
@Override
public void run()
try
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
catch (ClassNotFoundException ex)
catch (InstantiationException ex)
catch (IllegalAccessException ex)
catch (UnsupportedLookAndFeelException ex)
AutomatedRoshamboRPS frame = new AutomatedRoshamboRPS();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
);
public class AutomatedRoshamboRPS extends javax.swing.JFrame
int num = 0;
int compnum = 0;
int earnings = 0;
int wins = 0;
int losses = 0;
int ties = 0;
int roshamboCount = 0;
int gamesPlayed = 0;
int i;
int games = 0;
String a = "", b;
public void updateStats(int state, int roshamboCount, String result)
text.append(result + "\n");
String statsToString = stats.getText();
if (state == 0) //won
wins++;
gamesPlayed++;
else if (state == 1) //tied
ties++;
gamesPlayed++;
else //lost
losses++;
gamesPlayed++;
earnings = +(wins * 2) - (losses * 2) + (roshamboCount * 10) - (roshamboCount * 2);
stats.setText("STATISTICS:"
+ "\nGames Played:" + gamesPlayed
+ "\nEarnings:" + earnings
+ "\nWins:" + wins
+ "\nTies:" + ties
+ "\nLosses:" + losses
+ "\nRoshambo:" + roshamboCount);
public void setGameState(boolean enabled)
numGames.setEnabled(enabled);
rock.setEnabled(enabled);
paper.setEnabled(enabled);
scissors.setEnabled(enabled);
public AutomatedRoshamboRPS()
initComponents();
text.setText("ROCK PAPER SICSSORS"
+ " \nSelect your throw by clicking on one \nof the options to the left.\n"
+ "---------------------------------------------------\n");
rock.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
b = numGames.getText();
games = Integer.parseInt(b);
setGameState(false);
new GameWorker(AutomatedRoshamboRPS.this, games, 1).execute();
// for (int l = 0; l < games; l++)
// num = 1;
// game();
// updateStats();
//
);
paper.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
b = numGames.getText();
games = Integer.parseInt(b);
setGameState(false);
new GameWorker(AutomatedRoshamboRPS.this, games, 2).execute();
// for (int l = 0; l < games; l++)
// num = 2;
// game();
// updateStats();
//
);
scissors.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
b = numGames.getText();
games = Integer.parseInt(b);
setGameState(false);
new GameWorker(AutomatedRoshamboRPS.this, games, 3).execute();
// for (int l = 0; l < games; l++)
// num = 3;
// game();
// updateStats();
//
);
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
text = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
stats = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
numGames = new javax.swing.JTextField();
rock = new javax.swing.JToggleButton();
paper = new javax.swing.JToggleButton();
scissors = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jLabel1.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
jLabel1.setText("Automated Roshambo");
text.setColumns(20);
text.setEditable(false);
text.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
text.setLineWrap(true);
text.setRows(5);
jScrollPane1.setViewportView(text);
stats.setColumns(20);
stats.setEditable(false);
stats.setRows(5);
jScrollPane2.setViewportView(stats);
jLabel5.setText("Number of games");
jLabel6.setText("to simulate:");
// rock.setIcon(new javax.swing.ImageIcon(getClass().getResource("/haughton_nick_rps/Rock.png"))); // NOI18N
rock.setText("Rock");
rock.setContentAreaFilled(false);
// paper.setIcon(new javax.swing.ImageIcon(getClass().getResource("/haughton_nick_rps/Paper.png"))); // NOI18N
paper.setText("Paper");
paper.setContentAreaFilled(false);
// scissors.setIcon(new javax.swing.ImageIcon(getClass().getResource("/haughton_nick_rps/Scissors.png"))); // NOI18N
scissors.setText("Scissors");
scissors.setContentAreaFilled(false);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jLabel1)
.add(80, 80, 80))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(36, 36, 36)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(6, 6, 6)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(rock, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(scissors, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(paper))
.add(35, 35, 35)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel3)
.add(jLabel2)
.add(jLabel4)))
.add(jLabel5)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, numGames)
.add(org.jdesktop.layout.GroupLayout.LEADING, jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.add(18, 18, 18)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 244, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(32, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(18, 18, 18)
.add(jLabel1)
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane1)
.add(layout.createSequentialGroup()
.add(jLabel5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel6)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(numGames, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(0, 0, Short.MAX_VALUE)
.add(jLabel2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel4)
.add(208, 208, 208))
.add(layout.createSequentialGroup()
.add(rock, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(paper, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(scissors, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 0, Short.MAX_VALUE)))))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 125, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap()));
pack();
// </editor-fold>
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField numGames;
private javax.swing.JToggleButton paper;
private javax.swing.JToggleButton rock;
private javax.swing.JToggleButton scissors;
private javax.swing.JTextArea stats;
private javax.swing.JTextArea text;
// End of variables declaration
public class GameWorker extends SwingWorker<Object, GameResults>
private int games;
private int userChoice;
private int roshamboCount;
private AutomatedRoshamboRPS parent;
public GameWorker(AutomatedRoshamboRPS parent, int games, int userChoice)
this.games = games;
this.userChoice = userChoice;
this.parent = parent;
protected String toString(int choice)
String value = "ROCK";
switch (choice)
case 1:
value = "ROCK";
break;
case 2:
value = "PAPER";
break;
case 3:
value = "SCISSORS";
break;
return value;
@Override
protected void done()
parent.setGameState(true);
@Override
protected void process(List<GameResults> chunks)
for (GameResults results : chunks)
parent.updateStats(results.state, results.getRoshamboCount(), results.getText());
@Override
protected Object doInBackground() throws Exception
for (int index = 0; index < games; index++)
String text = null;
int compnum = (int) Math.floor((Math.random() * 3) + 1);
int state = 2;
if (userChoice - compnum == 1 || userChoice - compnum == -2)
text = "You won! " + toString(userChoice) + " beats " + toString(compnum);
state = 0;
roshamboCount++;
else if (userChoice - compnum == 0)
text = "You tied! " + toString(userChoice) + " ties with " + toString(compnum);
state = 1;
else
text = "You lost! " + toString(userChoice) + " losses to " + toString(compnum);
state = 2;
publish(new GameResults(state, roshamboCount, text));
return null;
public class GameResults
private String text;
private int state;
private int roshamboCount;
public GameResults(int state, int roshamboCount, String text)
this.text = text;
this.state = state;
this.roshamboCount = roshamboCount;
public int getRoshamboCount()
return roshamboCount;
public int getState()
return state;
public String getText()
return text;
【讨论】:
非常感谢您抽出宝贵的时间,但是,我并没有真正理解所有内容,而且在我参加的 comp sci 课程中,我们还没有学过这些,有没有将 game() 和 updateStats() 函数放在另一个线程上的更简单方法?再次感谢, 其实这是最简单的解决方案。您创建自己的线程,然后负责将 UI 请求同步回事件调度线程。阅读我提供的链接,它将提供更多详细信息以上是关于更新文本区域的主要内容,如果未能解决你的问题,请参考以下文章