gpt4 book ai didi

java - 为 ProgressBar Java 添加容器监听器

转载 作者:行者123 更新时间:2023-12-01 12:32:40 24 4
gpt4 key购买 nike

请帮忙。该代码不起作用。进度条已创建,其中显示 0% 就这样。我对 java 不太了解。我正在寻求帮助。

原始代码:

 jProgressBar1.setName("");
jProgressBar1.setValue(0);
jProgressBar1.setStringPainted(true);
jProgressBar1.addContainerListener(new java.awt.event.ContainerAdapter() {
public void componentAdded(java.awt.event.ContainerEvent evt) {
jProgressBar1ComponentAdded(evt);
}
});
private void jProgressBar1ComponentAdded(java.awt.event.ContainerEvent evt) {

Task tk = new Task();
tk.run();

}

class Task extends Thread {
public Task(){}

public void run(){
for(int i =0; i<= 100; i++){
final int progress = i;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jProgressBar1.setValue(progress);

}
});
}}}

最新代码:

import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;

import javax.swing.*;




public class ini extends JFrame {


public ini() {
initComponents();

}

private void initComponents() {

setResizable(false);
jLabel1 = new JLabel();
jProgressBar1 = new JProgressBar(0,100);
jProgressBar1.addContainerListener(new ContainerAdapter() {
@Override
public void componentAdded(ContainerEvent e) {
if (jProgressBar1 == e.getChild()) {
System.out.println("progressBar!");
new Thread(new Task()).start();
}
}
});

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Criminal Records");

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jLabel1.setText("Criminal Records");

jProgressBar1.setName("");
jProgressBar1.setValue(0);
jProgressBar1.setStringPainted(true);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(204, 204, 204)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(215, 215, 215)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(242, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(312, 312, 312)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(348, Short.MAX_VALUE))
);

pack();

}


public static void main(String args[]) {


try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ini.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ini.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ini.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ini.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ini().setVisible(true);
}
});
}

private class Task implements Runnable {
private static final long SLEEP_TIME = 100;

@Override
public void run() {
for (int i = 0; i <= 100; i++) {
final int progress = i;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jProgressBar1.setValue(progress);

}
});
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


private javax.swing.JLabel jLabel1;
private javax.swing.JProgressBar jProgressBar1;
}

最佳答案

我发现您使用线程存在一个主要问题和一个小问题:

  • 小问题:您应该实现 Runnable,而不是扩展 Thread,因为这将线程运行的代码与线程分开,遵循单一责任规则,并为线程运行的代码提供了更大的灵 active ,因为现在它如果需要的话可以扩展其他类。
  • 启动线程时,请对其调用 .start(),而不是 .run(),因为后者将保证不会创建后台线程.

至于您的另一个问题,即使用 ContainerListener 的问题,我等待您对问题的澄清,以及您创建和发布您的 minimal code example program供我们审查、测试并可能修复。

<小时/>

编辑
根据您的评论,

I want to show a progress-bar in a frame which show 0-100%. I created the frame and added the progress bar. But it shows only 0% and not moving.

同样,您还有一些线程问题需要修复。您还需要在其中放置一个 Thread.sleep 来减慢 for 循环的速度。否则,即使使用适当的线程,您也不会看到进展。

I want to invoke the function when progressbar is added to the frame.

您正在将 ContainerListener 添加到 JProgressBar,但这不起作用,因为您没有向进度条添加任何内容。相反,您需要将监听器添加到保存进度条的容器中。但即便如此,这可能不是开始这一过程的好方法。但让我看看我能做什么......请稍等。

<小时/>

例如,

import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;

import javax.swing.*;

public class JProgressExample extends JPanel {
private JProgressBar jProgressBar1 = new JProgressBar(0, 100);

public JProgressExample() {
addContainerListener(new ContainerAdapter() {
@Override
public void componentAdded(ContainerEvent e) {
if (jProgressBar1 == e.getChild()) {
System.out.println("progressBar!");
new Thread(new Task()).start();
}
}
});
jProgressBar1.setName("");
jProgressBar1.setValue(0);
jProgressBar1.setStringPainted(true);

add(jProgressBar1);
}

private class Task implements Runnable {
private static final long SLEEP_TIME = 100;

@Override
public void run() {
for (int i = 0; i <= 100; i++) {
final int progress = i;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jProgressBar1.setValue(progress);

}
});
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

private static void createAndShowGui() {
JProgressExample mainPanel = new JProgressExample();

JFrame frame = new JFrame("JProgressExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 为 ProgressBar Java 添加容器监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25822901/

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