gpt4 book ai didi

java swing进度条不显示

转载 作者:行者123 更新时间:2023-12-01 13:06:14 25 4
gpt4 key购买 nike

当用户单击我的 swing 应用程序中的某些按钮时,我试图显示不确定的进度条。我为进度条编写了一个通用类,每次单击这几个按钮时都会调用该类。进度条窗口显示在任务栏中,但不显示在屏幕上。为什么会发生这种情况以及如何解决它?

感谢您的帮助。

编辑:

我的应用程序的 SSCE:进度条类

package mainform;

public class ProgressBarIndeterminate extends javax.swing.JFrame {
public ProgressBarIndeterminate() {
initComponents();
}


private void initComponents() {

jProgressBar1 = new javax.swing.JProgressBar();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setAlwaysOnTop(true);
setBackground(new java.awt.Color(255, 255, 255));
setUndecorated(true);
setResizable(false);

jProgressBar1.setBackground(new java.awt.Color(255, 255, 255));
jProgressBar1.setForeground(new java.awt.Color(153, 255, 0));
jProgressBar1.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
jProgressBar1.setFocusable(false);
jProgressBar1.setIndeterminate(true);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
);

pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProgressBarIndeterminate().setVisible(true);
}
});
}
private javax.swing.JProgressBar jProgressBar1;
}

ViewRec 类:

package mainform;
public class ViewRec extends javax.swing.JFrame {
public ViewRec() {
initComponents();
try{
Thread.sleep(10000);
}
catch(Exception e){}
}

private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
setResizable(false);

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"E-Mail ID", "Customer Name", "City", "State", "Industry", "Priority"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false
};

public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(jTable1);

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

pack();
}
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
}

类 ComposeMail:

package mainform;
public class ComposeMail extends javax.swing.JFrame {
ProgressBarIndeterminate pbi=new ProgressBarIndeterminate();

public ComposeMail() {
initComponents();
}


private void initComponents() {

jButton4 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jButton4.setText("View Recipients");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

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()
.addContainerGap()

.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
))))));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(jButton4))
)));
pack();
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
pbi.setVisible(true);
new ViewRec().setVisible(true);
pbi.dispose();
}
public static void main(String[] args){
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ComposeMail().setVisible(true);
}
});
}


private javax.swing.JButton jButton4;
}

编译并运行 ComposeMail 以重现我的问题。

最佳答案

将会显示您的进度条 - 如果 Swing 是线程安全的。

删除 pbi.dispose(),您应该会看到进度条。

您将其设置为可见,但随后您立即创建了一个 ViewRec 实例 - 并且 ViewRec 将阻止其操作的线程。 ViewRec 构造完成后,将调用 setVisible(),并立即再次处理进度条。

您可以在 official Tutorials 中找到有关如何使用进度条的信息。由 Oracle 提供。

关于java swing进度条不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23244448/

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