Java JLabel通过setText更改值,但不在屏幕上更新
好吧,我已经在一段代码上工作了将近2天了,但无法解决这个问题 期望的行为 下面的代码应该一个接一个地显示10个字符串(下一个字符串替换上一个字符串),间距为aprox。200毫秒 q1 问题2 第三季度 …等等,直到q10 当用户按ENTER键时,此显示顺序开始 反映的行为 屏幕等待一个近似值。按下后2秒,然后显示q10 更多信息Java JLabel通过setText更改值,但不在屏幕上更新,java,swing,jlabel,Java,Swing,Jlabel,好吧,我已经在一段代码上工作了将近2天了,但无法解决这个问题 期望的行为 下面的代码应该一个接一个地显示10个字符串(下一个字符串替换上一个字符串),间距为aprox。200毫秒 q1 问题2 第三季度 …等等,直到q10 当用户按ENTER键时,此显示顺序开始 反映的行为 屏幕等待一个近似值。按下后2秒,然后显示q10 更多信息 标签stringText在执行过程中更改了值(我通过写入控制台找到了该值),但在屏幕上没有更新(JFrame) 标签通过单击按钮上的事件更改值,其他所有内容保持不变(
stringText
在执行过程中更改了值(我通过写入控制台找到了该值),但在屏幕上没有更新(JFrame)displayQuestion(int number)
有一些不必要的行。我把它们都放在这里,因为我不确定什么能起作用。事实上,什么都没用李>
代码
package sush4;
import java.util.Date;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Sush4 {
// Timer control variables
static long holdTimeWord = 200L;
static long elapsedTime = 0L;
// Counter for Strings displayed
static int i = 0;
// Strings in use
static String[] questionStack = {"q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10"};
// UI: String display variables
static JLabel stringText;
static JFrame mainWindow;
// Key binding action object
private static Action userKeyCommand;
/// Display the question
private static void displayQuestion(int number) {
mainWindow.remove(stringText);
stringText.setText(questionStack[number]);
mainWindow.add(stringText);
mainWindow.setVisible(true);
mainWindow.revalidate();
mainWindow.repaint();
}
private static void q120(){
//// Initiate the text
for(i = 0; i < questionStack.length; i++) {
displayQuestion(i);
//// And wait for Word hold time
long startTime = System.currentTimeMillis();
elapsedTime = 0L;
// Now wait for event to happen
while ( (elapsedTime < holdTimeWord) ) {
elapsedTime = (new Date()).getTime() - startTime;
}
}
}
Sush4() {
//// Create the Window
mainWindow = new JFrame("Sush");
mainWindow.setSize(700, 500);
mainWindow.setLayout(new FlowLayout());
//// And add key bindings for user events
userKeyCommand = new UserKeyCommand();
JRootPane rootPane = mainWindow.getRootPane();
rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "doEnterAction");
rootPane.getActionMap().put("doEnterAction", userKeyCommand);
// Terminate the program when the user closes the application.
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setResizable(false);
//// Add the text label
stringText = new JLabel("Random Text");
mainWindow.add(stringText);
//// Finally, display the frame.
mainWindow.setVisible(true);
}
static class UserKeyCommand extends AbstractAction {
public void actionPerformed( ActionEvent tf ) {
q120();
}
}
public static void main(String[] args) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Sush4();
}
});
}
}
软件包sush4;
导入java.util.Date;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类Sush4{
//定时器控制变量
静态长保持时间字=200L;
静态长延时时间=0升;
//显示字符串的计数器
静态int i=0;
//正在使用的字符串
静态字符串[]questionStack={“q1”、“q2”、“q3”、“q4”、“q5”、“q6”、“q7”、“q8”、“q9”、“q10”};
//UI:字符串显示变量
静态jlabelstringtext;
静态JFrame主窗口;
//键绑定操作对象
私有静态动作userKeyCommand;
///显示问题
私有静态无效显示问题(整数){
主窗口。删除(stringText);
setText(questionStack[number]);
mainWindow.add(stringText);
mainWindow.setVisible(true);
mainWindow.revalidate();
mainWindow.repaint();
}
私有静态无效q120(){
////启动文本
对于(i=0;i
计时器通过一个while循环-这可能不是大多数人喜欢的,但让我们暂时忘记它
实际上,我们不能忘记这个while循环,因为这是造成麻烦的原因。请参见,q120()
方法在单击按钮时被调用:
static class UserKeyCommand extends AbstractAction {
@Override // don't forget @Override annotation
public void actionPerformed( ActionEvent tf ) {
q120();
}
}
这意味着此代码在的上下文中执行。这是一个单独的特殊线程,其中必须创建/更新Swing组件,并且必须执行事件处理(即操作事件)。如果这个线程中有一个循环等待某些条件继续,我们将阻止EDT,GUI将无法重新绘制自己,直到线程解锁
对于重复任务(如你的问题),考虑使用A。对于具有中间结果的繁重任务,考虑使用A代替。我怀疑OP的循环仅试图对预期的(在生产中:非循环)行为进行模型/单元测试。因此,如果是这样的话,计时器或SwingWorker可能就没有必要了。@ryvantage,尽管可能不太可能有人发布与有问题的代码完全不同的代码。在任何情况下,使用Swing Timer都是执行问题中所述操作的方法:下面的代码应该一个接一个地显示10个字符串(下一个替换上一个),间距为aprox。200毫秒。除非我遗漏了一些东西。我的评论主要是为了让OP知道,如果他的真正应用程序不使用循环(以及那种骇人的延迟系统),那么他就不需要计时器或swing worker,问题可能会自行解决。这不是挖苦或纠正你的答案。