gpt4 book ai didi

java - JComponent.revalidate() 在代码完成之前不会更新

转载 作者:行者123 更新时间:2023-12-01 16:02:45 26 4
gpt4 key购买 nike

**你好,我正在尝试用 java 创建一个存档器。这意味着我不断地从流中读取和写入。我希望能够更新 JProgressBar 以显示我已经写了多少。我的代码当前在存档中的每个条目之后更新进度栏。我的变量和所有内容都表明代码已更新,但它没有显示在屏幕上。每次进入存档后,我都会在包含进度条和文本的 JPanel 上调用 revalidate() 方法,但屏幕上没有任何反应。在写入存档的输出流完全完成写入之前,屏幕不会更新。然后,我看到完整的进度条,进度文本中显示“已写入 24 个文件中的 24 个”。

谁能告诉我如何在代码执行完毕之前更新图形?谢谢

这是我的代码:

WritingBox.java:

package org.apache.commons.compress;
import javax.swing.JDialog;
public class WritingBox extends javax.swing.JDialog {

/** Creates new form WritingBox */
public WritingBox(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
theprogressbar.setMaximum(1);
theprogressbar.setMinimum(0);
this.setLocation(parent.getLocationOnScreen().x+(parent.getWidth()-this.getWidth())/2, parent.getLocationOnScreen().y+(parent.getHeight()-this.getHeight())/2);
this.setVisible(true);
}
public void progressListener(Progress p) {
writtenxoutofx.setText("Written "+Integer.toString(p.doneSoFar)+" out of "+Integer.toString(p.total)+" files to the Archive");
theprogressbar.setValue(p.doneSoFar/p.total);
if (p.doneSoFar == p.total) this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
thecontainer.revalidate();
};
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

thecontainer = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
writtenxoutofx = new javax.swing.JLabel();
theprogressbar = new javax.swing.JProgressBar();

setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setName("Form"); // NOI18N

thecontainer.setName("thecontainer"); // NOI18N

org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(targzipmanager.TarGzipManagerApp.class).getContext().getResourceMap(WritingBox.class);
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N

writtenxoutofx.setText(resourceMap.getString("writtenxoutofx.text")); // NOI18N
writtenxoutofx.setName("writtenxoutofx"); // NOI18N

theprogressbar.setName("theprogressbar"); // NOI18N

javax.swing.GroupLayout thecontainerLayout = new javax.swing.GroupLayout(thecontainer);
thecontainer.setLayout(thecontainerLayout);
thecontainerLayout.setHorizontalGroup(
thecontainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(thecontainerLayout.createSequentialGroup()
.addContainerGap()
.addGroup(thecontainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(theprogressbar, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
.addComponent(jLabel1)
.addComponent(writtenxoutofx))
.addContainerGap())
);
thecontainerLayout.setVerticalGroup(
thecontainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(thecontainerLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(writtenxoutofx)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(theprogressbar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(thecontainer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(thecontainer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel thecontainer;
private javax.swing.JProgressBar theprogressbar;
private javax.swing.JLabel writtenxoutofx;
// End of variables declaration

}

进度.java

package org.apache.commons.compress;

public class Progress { //This is basically a Struct
public int total;
public int doneSoFar;
private Progress (){

}
public Progress(int d, int t){
total = t;
doneSoFar = t;
}
}

TarArchive.java:d​​oSave 方法

public void doSave(OutputStream output) throws ArchiveException {
// Stream initializing
//BufferedInputStream origin = null;

//out.setMethod(ZipOutputStream.DEFLATED);
//byte data[] = new byte[BUFFER];

// get a list of files from current directory
// less than one file leads to an exception
Iterator iterator = this.getEntryIterator();
if(!iterator.hasNext()) {
throw new ArchiveException("There must be at least one file to be pack.");
}
JFrame jf = targzipmanager.TarGzipManagerApp.getApplication().getMainFrame();
WritingBox wb = new WritingBox(jf, false);
TarOutputStream out = null;
int x = 0;
try {
out = new TarOutputStream(new BufferedOutputStream(output));
while(iterator.hasNext()) {
x++;
ArchiveEntry archiveEntry = (ArchiveEntry)iterator.next();
InputStream fInputStream = archiveEntry.getStream();

TarEntry entry = new TarEntry(archiveEntry.getName());
entry.setModTime( 0 );
entry.setSize( fInputStream.available() );
entry.setUserID( 0 );
entry.setGroupID( 0 );
entry.setUserName( "avalon" );
entry.setGroupName( "excalibur" );
entry.setMode( 0100000 );
out.putNextEntry( entry );
out.copyEntryContents( fInputStream );
out.closeEntry();
wb.progressListener(new Progress(x, entries.size()));
}
} catch (IOException e) {
throw new ArchiveException("Creation of this archive failed cause of IOExceptions.", e);
} finally {
try {
out.close();
} catch (IOException e1) {
throw new ArchiveException("Creation of this archive failed cause of IOExceptions.", e1);
}
}
}

有人可以帮助我吗?即使我在每次输入后调用 revalidate(),它也只会在代码停止并开始等待时更新。 **

最佳答案

听起来您正在 AWT 事件调度线程 (EDT) 上执行文件 I/O。这会阻止任何重画或事件处理。在不同的线程上运行 I/O,并通过 java.awt.EventQueue.invokeLater 更新 GUI。

关于java - JComponent.revalidate() 在代码完成之前不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3410127/

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