我有一个问题,可能有一个简单的修复,但我似乎无法得到它的工作。制作程序等待,直到点击一个按钮
我需要我的程序暂停或等待,直到用户在继续之前选择跳过按钮或正反馈按钮。 我认为这将需要基本的线程,但我不知道如何实现它。任何帮助将appreacited。
gui显示在一个单独的类(GUI)中,其余的是另一个类。 代码有点乱,因为它在12小时内被编码为Hackfest。
编辑:解决它我自己删除按钮监听器,并使变量静态。
public void onStatus(Status status) {
//this is a listener from the Twitter4J class. Every time new Tweet comes in it updates.
for (int i = 0; i <= posWords.length; i++) {
if (status.getText().toLowerCase().contains(gui.sCrit.getText())
&& (status.getText().toLowerCase().contains(posWords[i].toLowerCase()))) {
//If the tweet matches this criteria do the following:
String tweet;
Status tempStoreP;
System.out.println("Flagged positive because of " +posWords[i].toLowerCase()+" " + i);
tempStoreP = status;
tweet = tempStoreP.getUser().getName() + ":" + tempStoreP.getText() + " | Flagged as positive \n\r";
gui.cTweet.setText(tweet);
//Write to log
try {
wPLog.append("~" + tweet);
} catch (IOException e) {
}
//Add action listeneer to gTweet.
//here is my problem. I want it to wait until a user has clicked one of these buttons before moving on and getting the next Tweet. It has to pause the thread until a button is clicked then it can only move on to getting another Tweet.
//the problem is that the button listener uses local variables to work.
gui.gTweet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (gui.pTweet.getText().equalsIgnoreCase("")) {
gui.pTweet.setText("Please type a response");
} else if (gui.pTweet.getText().equalsIgnoreCase("Please type a response")) {
gui.pTweet.setText("Please type a response");
} else {
try {
Status sendPTweet = twitter
.updateStatus("@" + tempStoreP.getUser().getScreenName() + " "
+ gui.pTweet.getText());
} catch (TwitterException e1) {
}
}
gui.gTweet.removeActionListener(this);
}
});
//add Aaction listert to sTweet
gui.sTweet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
wPLog.append("Skipped \n\r");
} catch (IOException e1) {
}
gui.sTweet.removeActionListener(this);
}
});
}
谢谢你的帮助。在附注中,如果任何人都可以告诉我为什么当点击该按钮时,它会循环并用相同的消息来垃圾邮件,这会有所帮助。谢谢。
如果代码是一团糟,那么清理它然后发布。为什么要打扰任何人,即使你不喜欢? – zubergu
是否有按钮启动将用户移动到下一个屏幕而不是选项的操作? – npinti
*“代码有点乱,因为它是在12小时内为Hackfest编码的。”*这是没有任何借口,在将它倾倒在这里之前不要改进它。为了尽快提供更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。顺便说一下,这种类型的问题通常使用模态对话框来处理,但我不能基于可用的有限信息来判断这是否符合要求。 –