我正在构建一个从服务器下载N个元素的java程序.我想要一个工作进度条,显示"已经下载的元素"/ N百分比.现在,我通过向线程传递对GUI对象的引用来更新进度条值,但它使代码"丑陋"(GUI类创建线程,其中包含对GUI本身的引用,创建一种循环).有没有更好的方法呢?
如何使进度条慢慢降低时间限制?
class GamePanel extends JPanel implements MouseListener, ActionListener
{
private JButton quit;
private JButton q;
private Font loadFont;
public GamePanel()
{
setBackground(Color.blue); // sets background color
this.setLayout(null);
quit = new JButton("Quit");
quit.addActionListener(this);
quit.setBounds(550, 700, 100, 30);
this.add(quit);
q = new JButton("Questions");
q.addActionListener(this);
q.setBounds(100, 100, 120, 30);
this.add(q);
loadFont = new Font("Serif", Font.PLAIN, 30);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(80, 100, 610, 560);
g.setColor(Color.white);
g.fillRect(90, 110, 110, 100);// 1st column
g.fillRect(90, 220, 110, 100);//
g.fillRect(90, 330, 110, 100);// …Run Code Online (Sandbox Code Playgroud) 我正在为我正在开发的项目构建一个相当复杂的GUI.重要的是,它包含(除此之外)JTabbedPane具有12个以上复杂GUI组件的窗格.
我正在尝试做的是JProgressBar在我实例化这些窗格时进行显示(创建和配置;做所有事情都不能实际显示).实际上,我希望得到类似Eclipse启动画面的最终结果:

这是(更新为包含SplashScreen)伪代码,我正在尝试做什么:
ProgramManager:
private setupGUI() {
mainGUI = new MainGUI(); // only instantiates internal JFrame field
mainGUI.setup();
}
MainGUI:
public void setup() {
//create and configure progress bar
final SplashScreen ss = SplashScreen.getSplashScreen();
JProgressBar jpb = new JProgressBar(){
Graphics g = ss.createGraphics();
@Override
public void paint(Graphics pG) {
// TODO Auto-generated method stub
super.paint(g);
}
};
jpb.setValue(0);
setup1stTab();
//update Progress
setup2ndTab();
//update progress
etc....
ss.close();
}
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的,或者如果我只是错了,请告诉我.我环顾四周,看到有人提到了Threading/SwingWorker,但是在搞乱了那个和观察者/可观察的东西之后(诚然只是一点点),我仍然无法弄明白.
我想使用JProgressBar,但我没有任何关于完成任务需要多长时间的进度测量.我的想法是有一个显示关闭进程状态的进度条,但我不知道它需要多长时间,而且我无法编辑执行关闭过程的类.
有没有办法使用JProgressBar而没有任何进展指示?
我想使用JProgressBar它必须在一秒钟内加载.我不想等待任何任务完成.只想在一秒钟内填写进度条.所以我写下面的代码.但它不起作用.进度条没有填补.我是Java新手.请任何人都可以帮助我.
public void viewBar() {
progressbar.setStringPainted(true);
progressbar.setValue(0);
for(int i = 0; i <= 100; i++) {
progressbar.setValue(i);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
progressbar.setValue(progressbar.getMinimum());
}
很好的工作,现在我只想知道为什么如果我加入while循环指令System.out.println下面的进度显示在两个,cmd和Pgbar在桂?:
while(progress < 99){
System.out.println("into while of PBar Thread progress = "+progress);
if(progress != Path.operationProgress){
operationProgressBar.setValue(progress);
progress = Path.operationProgress;
operationProgressBar.repaint(); } }
Run Code Online (Sandbox Code Playgroud)
需要一些帮助,我不能让JProgressBar更新,我不能使用SwingWorker,我必须解决这个没有它.变量Path.operationProgress是来自"Path"类实例的静态变量,它是从另一个线程更新的,所以我认为PBar和Path实例都是在用户的线程中执行而不是在EDT中执行.这是进度条的代码:
Run Code Online (Sandbox Code Playgroud)import javax.swing.*; public class Pbar extends Thread { JProgressBar operationProgressBar; public Pbar(JProgressBar operationProgressBar) { this.operationProgressBar = operationProgressBar; } @Override public void run() { int progress = Path.operationProgress; while(progress < 99) { if(progress != Path.operationProgress) { operationProgressBar.setValue(progress); progress = Path.operationProgress; operationProgressBar.repaint(); }}} }这是启动线程的操作:
Run Code Online (Sandbox Code Playgroud)private javax.swing.JProgressBar operationProgressBar; private javax.swing.JLabel pathImage; private javax.swing.JButton simulatedAnnelingButton; public class TSPGUI extends javax.swing.JFrame …
我正在使用Netbeans Editor使用Java开发一个小的GUI应用程序.我在JFrame中添加了一个简单的进度条.我正在用JDK7开发这个项目
我想将背景颜色从默认橙色更改为个人颜色.我已经尝试了颜色变化的所有属性但是当我运行程序时颜色仍然相同.
我已经尝试过使用ProgressBar1.setBackground(new java.awt.Color(0,204,255));
和
UIManager.put("ProgressBar.background", Color.YELLOW);
UIManager.put("ProgressBar.foreground", Color.MAGENTA);
UIManager.put("ProgressBar.selectionBackground", Color.red);
UIManager.put("ProgressBar.selectionForeground", Color.green);
Run Code Online (Sandbox Code Playgroud)
相同的结果.....背景总是橙色
这是我的测试项目的代码
public class Frame extends javax.swing.JFrame
{
public Frame()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jProgressBar1 = new javax.swing.JProgressBar();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jProgressBar1.setBackground(new java.awt.Color(0, 204, 255));
jProgressBar1.setValue(75);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void …Run Code Online (Sandbox Code Playgroud) 我正在尝试添加进度条.一切正常,我没有任何错误.但进度条从0%变为100%,甚至没有通过它之间的值(我的意思是它太快了,用户无法看到进度条块填写)
pr = new JProgressBar();
pr(0);
pr(true);
..
public void iterate(){
while (i<=20000){
pr.setValue(i);
i=i+1000;
try{
Thread.sleep(150);
}catch (Exception e){
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
单击i按钮时,我调用该iterate()方法,我希望它可以逐步更新进度条.相反,它会暂停一段时间,然后显示一个完整的进度条.
我怎么解决这个问题?
2.)我不喜欢进度条选项卡的默认蓝色.我需要改变颜色.我试过 pr.setForeground(Color.GRAY);
pr.setBackground(Color.RED);但它没用.
我正在用Java制作一个游戏,我正在使用JProgressBar一个健康栏.我想使用图像(而不是颜色)JProgressBar,但我无法做到.我尝试过使用paint方法,paintComponent方法,创建一个新类,但它不起作用.愿有人请帮帮我吗?
我有一个使用a的GUI JProgressBar.GUI具有JBUTTONs以下代码:
public void updateProgressBar(int x) {
System.out.println(javax.swing.SwingUtilities.isEventDispatchThread());
healthBar.setValue(x);
}
Run Code Online (Sandbox Code Playgroud)
但我也做了一个循环,定期调用相同的方法.
public class Loop implements Runnable {
public void run() {
while (true) { Thread.sleep(2000); updateProgressBar(0); }
}
}
Run Code Online (Sandbox Code Playgroud)
现在,据我所知,任何改变我的GUI都需要从EDT执行.JProgressBar.setValue(x)更改我的GUI,当从Loop类调用它时,isEventDispatchThread检查失败,这是好的和明白的.然而,我无法理解的是,如果这也意味着我应该使用SwingUtilities.invokeLater()on setValue().我担心的是,因为我不知道setValue()实际上是如何工作的,所以我invokeLater()不必要地使用它,甚至在使用它时破坏了一些东西.
我不知道如何更好地问我的问题:如果我知道改变我的GUI的方法没有从EDT调用,那么我是否也知道我必须使用invokeLater()?
java ×10
jprogressbar ×10
swing ×10
colors ×1
concurrency ×1
image ×1
long-integer ×1
nimbus ×1
oracle ×1
swingworker ×1
timer ×1