- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用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-code
是 loadingTextLoaded = true
和 loadingBarLoaded = 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
这就是 loadingBar
和 loadingText
变量并声明为
public javax.swing.JProgressBar loadingBar;
public javax.swing.JLabel loadingText;
loadingBarLoaded
和 loadingTextLoaded
声明如下
public boolean loadingTextLoaded = false;
public boolean loadingBarLoaded = false;
loadingTextLoaded
和 loadingBarLoaded
并在所有生成的代码将它们放置在 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/
我正在尝试将 2 个 JProgressBars 组合在一起,以便我可以表示有关单个事物的统计信息。 我的问题是如何将两个进度条组合或叠加才能起作用? 我想到的一个解决方案是将 2 个进度条放在一个
继续 Setting the colors of a JProgressBar text我希望能够根据进程状态在我的程序中设置 JProgressBar 的文本颜色,而不偏离系统外观。 来自 Sett
注意到这个链接后: http://www.newscientist.com/blogs/nstv/2010/12/best-videos-of-2010-progress-bar-illusion.h
我正在尝试在这个问题的投票答案中找到的代码:Download file using java apache commons? 是一个下载应用,稍微看一下,(我对JFrames和ActionEvents
我只是想在 JFrame 中使用 Windows L&F .现在在使用进度条时,默认颜色是绿色,类似于其他 windows 功能中使用的颜色,如复制文件等。在复制文件的情况下覆盖)。如何在我的进度条的
你好堆栈交换器, 我在 java Swing 中的进度条有问题。我认为我的困惑源于我对线程和 Swing 事件队列的理解不足(我不太了解 java 线程,以及 AWTEventQueue 上到底发生了
如何从另一个线程更新 JProgressBar.setValue(int)? 我的次要目标是尽可能少地上课。 这是我现在拥有的代码: // Part of the main class.... pp.
我正在尝试使用 JProgressBar 作为简单游戏的健康栏。我试图让它完整且可见以启动程序。这是我的 JProgressBar 代码: progressBar = new JProgres
我无法更新我的进度条...这是我的代码 Thread t=new Thread(new Runnable(){ public void run(){ int i
您好,我想知道是否可以扩展 JProgressBar 以使用 double 值来表示最小值最大值,而不是 int。谢谢。 最佳答案 如果您知道值的范围和精度,那么使用包装器就很容易做到。我已将这种方法
我正在尝试将 JProgressBar 添加到我的程序中,但它不会更新!该值仅在推理 100% 后才会更改。这是我的方法。 public void downloadImages(List images
我正在使用 JProgressBar 在我的框架上显示进度条。进度条设置为不确定模式,因为我不知道任务何时结束。显示的不是正常的进度条,而是奇怪的橙色波浪。 任务运行时波浪会持续移动。结束后,该值设置
我最近的项目遇到了一个严重的问题:我正在一个名为 writer(...) 的方法中进行长时间比较,大约需要 20 秒。该方法在 View 类中进行标记,我的 GUI 元素也在其中设置。当方法进行比较时
以下是将图像转换为字节数组的简单代码(本论坛已显示): File imgPath= new File(textFiled_Path.getText()); BufferedImage original
我已经尝试从库接收字节来设置我的 JProgressBar 更新很长一段时间,但不幸的是我没有得到我想要的结果。 问题是我不知道如何从lib接收字节以及如何在接收字节时更新JProgressBar所有
我想在我的代码中添加一个 JProgressBar,它计数到 5(以秒为单位)。之后,它将产生新的值,但我当前的问题是,我什至无法用我的 ActionListener 制作 ProgressBar,所
我在函数中使用 POI 来填充 Excel 文档的内容(需要 10 秒),当我调用函数时,我想使用 JProgressBar 来查看进度,但按钮和程序被阻止,我需要在其他线程中制作吗?我该怎么做?我的
我编写了一个非常简单的代码来在此处显示它,我有一个按钮应该显示 JDialog 来检查进度状态,我正在使用延迟调用来完成 EDT 并且我的循环不在 run 方法中,那么为什么我的栏没有更新呢?这是代码
我一直在尝试更改 jProgressBar 颜色,但一直是橙色 这是属性 运行 我想将颜色更改为绿色或其他颜色,但不知道如何实现 最佳答案 尝试这个教学示例。它将修改 JProgressBar 的 4
我在创 build 置为不确定的 JProgressBar 时遇到问题。以下代码是我对 JProgressBar 的实现,是从另一个类调用/构造的: public class Progress imp
我是一名优秀的程序员,十分优秀!