Ho un ListCellRenderer personalizzato e desidero utilizzare il colore di sfondo predefinito di selezione Nimbus. Posso occhiata il colore con:Come utilizzare un colore Nimbus predefinito con UIManager?
Color selectionBackground = UIManager.getColor("nimbusSelectionBackground");
e se lo stampo, ha gli stessi valori su Nimbus default colors. Ma quando lo uso su un JPanel ottengo un diverso colore grigio, come posso usare il colore da UIManager?
Quando faccio:
setBackground(Color.RED);
backround JPanels è mostrato in rosso, ma quando lo faccio:
setBackground(selectionBackground);
Il colore "selectionBackground" è non usato, ma un colore grigio .
Ecco un esempio e screenshot:
Lo sfondo dovrebbe essere:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class PanelColor {
public static void main(String[] args) {
// switch to Nimbus Look And Feel
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
try {
UIManager.setLookAndFeel(info.getClassName());
} catch (Exception e) { e.printStackTrace(); }
break;
}
}
Color selectionBackground = UIManager.getColor("nimbusSelectionBackground");
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(300,50));
panel.add(new JLabel(selectionBackground.toString()), BorderLayout.NORTH);
// is not showing the selectionBackground color
panel.setBackground(selectionBackground);
JFrame frame = new JFrame();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
È possibile che la seguente domanda possa essere d'aiuto con Nimbus: http://stackoverflow.com/questions/5840599/jbutton-background-on-nimbus-laf? –
Wow, molto strano. Ho controllato il codice: il colore restituito è di tipo javax.swing.plaf.ColorUIResource. Ma non vedo come ciò possa causare problemi poiché è una sottoclasse di Colore. Non c'è nulla di speciale nel codice di ColorUIResource, nessun riferimento all'interfaccia utente o altro. Come suggerito da Joey, 'selectionBackground = new Color (selectionBackground.getRGB());' funziona. – toto2