我已经讨论了这段代码多年了,并且不知道如何让我的JProgressBar真正出现在GUI上。当我运行应用程序时,JLabel出现在JPanel中,但JProgressBar没有,我该如何解决这个问题?
(注意:JProgressBar可以正常工作和更新,它不会出现在GUI上)
jPanel9.setLayout(new BorderLayout());
jPanel9.add(new JLabel(theTask.getName() + theTask.getDes()), BorderLayout.NORTH);
JProgressBar progress = new JProgressBar(0, theTask.getMax());
progress.setStringPainted(true);
progress.setBorderPainted(true);
progress.setValue(theTask.getProgress());
progress.setString(theTask.getProgress() + "/" + progress.getMaximum());
progress.setPreferredSize(new Dimension(100,100));
progress.setVisible(true);
jPanel9.add(progress, BorderLayout.SOUTH);
jPanel9.revalidate();
jPanel9.repaint();
System.out.println(progress.getString());
System.out.println("Progress: " + theTask.getProgress());
System.out.println("Max: " + theTask.getMax());
答案 0 :(得分:0)
我通过制作一个自定义面板解决了这个问题,并将其放入我的gui中,而不是我想要的,但是它完成了工作:
public class ProgressPanel extends javax.swing.JPanel {
/**
* Creates new form ProgressPanel
*/
public ProgressPanel() {
initComponents();
jProgressBar1.setMinimum(0);
}
public ProgressPanel(int max, String s)
{
jProgressBar1.setMinimum(0);
jProgressBar1.setMaximum(max);
changeLabel(s);
initComponents();
}
public void setMax(int i)
{
jProgressBar1.setMaximum(i);
}
public void changeProgress(int i)
{
jProgressBar1.setValue(i);
jProgressBar1.setString(i + "/" + jProgressBar1.getMaximum());
}
public void changeLabel(String s)
{
jLabel1.setText(s);
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jProgressBar1 = new javax.swing.JProgressBar();
setLayout(new java.awt.BorderLayout());
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("jLabel1");
add(jLabel1, java.awt.BorderLayout.PAGE_START);
jProgressBar1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
add(jProgressBar1, java.awt.BorderLayout.CENTER);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JProgressBar jProgressBar1;
// End of variables declaration