Sto utilizzando il componente JProgressBar
insieme ai valori predefiniti dell'interfaccia utente Nimbus. Il problema è che quando voglio cambiare manualmente il colore della barra di ogni progressbar, io uso BasicProgressBarUI
impostando la funzione JProgressBar.setUI()
. Questo crea più problemi perché vorrei solo cambiare il colore della barra e sembra che perdo l'aspetto predefinito della barra dei comandi di jprogress (Border, backgroundcolor scompare).JProgressBar Modifica dinamicamente del colore della barra
Così posso impostare UIDefaults
di Nimbus ProgressBar quando il codice viene inizializzato. Funziona.
Ma voglio cambiare dinamicamente il colore della barra di ogni barra di avanzamento.
C'è un altro modo per cambiare il colore della barra di JProgressBar
?
public class ProgressGenerator extends JFrame {
protected int minValue = 0;
protected int maxValue = 100;
protected int counter = 0;
protected JProgressBar progressBar;
public ProgressGenerator() {
super("JProgressBar Demo");
setSize(300, 100);
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
progressBar = new JProgressBar();
progressBar.setMinimum(minValue);
progressBar.setMaximum(maxValue);
progressBar.setStringPainted(true);
progressBar.setForeground(Color.GREEN);
JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread runner = new Thread() {
public void run() {
counter = minValue;
while (counter <= maxValue) {
Runnable runme = new Runnable() {
public void run() {
progressBar.setValue(counter);
}
};
SwingUtilities.invokeLater(runme);
counter++;
try {
Thread.sleep(100);
} catch (Exception ex) {
}
}
}
};
runner.start();
}
});
getContentPane().add(progressBar, BorderLayout.CENTER);
getContentPane().add(start, BorderLayout.WEST);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String[] args) {
new ProgressGenerator();
}
}
per una migliore aiuto prima inserire una [SSCCE] (http://sscce.org/), a breve, eseguibile, compilabile, altrimenti fare per cercare, Google può restituisce che – mKorbel
See [questo] (http://stackoverflow.com/questions/7174420/change-colors-for-jprogressbar-with-nimbus) domanda/risposta simile, che mostra come usare 'UIDefaults' e' putClientProperty' per cambiare un singolo colore 'JProgressBar' –
Il problema corrente è Quando cambio il colore della barra usando setForeground (Color.GREEN); cambia il Colore SelectionBackground. E sembra che setBackgroundColor (Color.RED); il comando non fa nulla – mbasol