简体   繁体   English

如何使我的JProgressBar出现在我的JPanel中?

[英]How can I get my JProgressBar to appear in my JPanel?

I've been going over this code for ages, and have no idea how to get my JProgressBar to actually appear on the GUI. 我已经花了很长时间查看该代码,并且不知道如何让我的JProgressBar真正出现在GUI上。 When I run the app, the JLabel appears in the JPanel, but the JProgressBar does not, how do I fix this? 当我运行该应用程序时,JLabel会出现在JPanel中,但JProgressBar却没有,如何解决此问题?

(note: the JProgressBar does work and updates, it just doesn't appear on the GUI) (注意: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());

I resolved the issue by making a custom panel, and dropping it into my gui in place, not quite what I wanted, but it got the job done: 我通过制作一个自定义面板并将其放到我的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM