gpt4 book ai didi

Java - JProgressBar 不会更改值,JLabel 不会更改文本

转载 作者:行者123 更新时间:2023-12-02 09:33:33 24 4
gpt4 key购买 nike

我正在用Java编写一个程序,我想添加一个加载页面,其中显示一个进度条和一个标签,说明正在加载的内容以及显示完成进度的栏,我已经设置了所有内容,所以应该工作但没有,我不知道出了什么问题(我是java新手,所以请怜悯)

我尝试过将一个 boolean 值默认设置为 false,并且仅在执行“after-all-set-code”之后(我正在使用 netbeans 创建 GUI)并且当我调用函数来更新文本/进度条,如果 boolean 值仍然设置为 false,它将等待一秒钟并重试,直到“all-set-code”将其更改为 true,但这似乎不起作用。

这是在我的main类中

public class Xtra {

public static loadingPage LP = new loadingPage();

public static void main(String[] args) {

// Show loading page
LP.main(args);

// Set loading
LP.setLoading(0, "Fetching Config...");

// Get Config
// Durring the "getConfig" function it calls the function "LP.setLoading()" multiple times to update the loading bar & text
Xtra.getConfig();

// Set loading
LP.setLoading(0, "Loading user data...");

}

}

这是我在 loadingPage 中的 setLoading() 类:

public void setLoading(int per, String txt) {

if ("".equals(txt)) {

setLoadingValue(per);

} else {

setLoadingText(txt);
setLoadingValue(per);

}

}

这是我的setLoadingValue()函数:

    public void setLoadingValue(int x) {

// This will turn true when the after-all-set code runs
while (!loadingBarLoaded) {

// Try to wait a second,
// Do this so it doesn't take up too much CPU
try {

// Wait a second
TimeUnit.SECONDS.sleep(1);

// Print to console
System.out.println("Loading value not loaded,\nWaiting another second");

// If there is an error waiting
} catch (InterruptedException ex) {

// Alert user
System.out.println("You cannot sleep now, monsters are nearby");

}

}

// After loaded boolean turns true

// Alert user of change
System.out.println("Setting loading value to " + x + "%");

// Change value
loadingBar.setValue(x);

}

这是我的setLoadingText()函数:

    public void setLoadingText(String txt) {

// This will turn true when the after-all-set code runs
while (!loadingTextLoaded) {

// Try to wait a second,
// Do this so it doesn't take up too much CPU
try {

// Wait a second
TimeUnit.SECONDS.sleep(1);

// Print to console
System.out.println("Loading text not loaded,\nWaiting another second");

// If there is an error waiting
} catch (InterruptedException ex) {

// Alert user
System.out.println("You cannot sleep now, monsters are nearby");

}

}

// After loaded boolean turns true

// Alert user of change
System.out.println("Setting loading text to \"" + txt + "\"");

// Change value
loadingText.setText(txt);

}

我的 after-all-set-codeloadingTextLoaded = trueloadingBarLoaded = true 取决于完成的内容

它应该更新进度条的文本和值,但事实并非如此,它正在将设置加载文本输出到“...”到控制台和也将控制台的加载值设置为 ...%,但不更改组件的实际值或文本。

我做错了什么?

简约问题:(主文件)


// Inside main function
// Show loading page
LP.main(args);

LP.loadingBar.setValue(50);
LP.loadingText.setText("Hello");
// nothing changes. (Both are public so I can call them from different files

这就是 loadingBarloadingText 变量并声明为

public javax.swing.JProgressBar loadingBar;
public javax.swing.JLabel loadingText;

loadingBarLoadedloadingTextLoaded 声明如下

public boolean loadingTextLoaded = false;
public boolean loadingBarLoaded = false;

loadingTextLoadedloadingBarLoaded 并在所有生成的代码将它们放置在 all-set-code 之后的窗口内之后更改为 true已运行(我正在使用 NetBeans GUI 生成器,因此生成了代码)

在loadingPage.java中,它是这样布局的。

主窗口,有一个面板覆盖整个主窗口,里面有三个东西,顶部中间有一个标签,它有应用程序名称“Xtra”,下面是loadingText 小标题,显示当前正在加载的内容,在其下方,有一个进度条 loadingBar

Here is a screen shot of the layout

抱歉,如果我没有遵循不成文的编码规则,我一周前才开始接触 java。

最佳答案

了解 Swing 中的线程。您必须使用 SwingWorker并在 doInBackground() 方法中进行加载并返回进度。

import javax.swing.*;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class Main {

private JProgressBar prog;

public static void main(String[] args) throws InvocationTargetException, InterruptedException {

SwingUtilities.invokeAndWait(()-> new Main().initGUI());
}

private void initGUI(){
JDialog dialog = new JDialog();
dialog.setTitle("Progress");
prog = new JProgressBar();
prog.setMaximum(100);
dialog.add(prog);
dialog.pack();
dialog.setVisible(true);
BackgroundWorker bw =new BackgroundWorker();
bw.execute();
}

private class BackgroundWorker extends SwingWorker<String, Integer>{
@Override
protected String doInBackground() throws Exception {
try{Thread.sleep(1000);}catch(Exception e){}
publish(10);
try{Thread.sleep(1000);}catch(Exception e){}
publish(20);
try{Thread.sleep(1000);}catch(Exception e){}
publish(30);
try{Thread.sleep(1000);}catch(Exception e){}
publish(70);
try{Thread.sleep(1000);}catch(Exception e){}
publish(100);
return "finished";
}

@Override
protected void process(List<Integer> chunks) {
prog.setValue(chunks.get(chunks.size()-1));
}
}
}

重要的是,您不能从 doInBackground() 方法访问任何 Swing 组件,因为该方法不是从 Swing 事件调度线程调用的。

关于Java - JProgressBar 不会更改值,JLabel 不会更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57748079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com