カスタム ListCellRenderer があり、デフォルトの Nimbus 選択背景色を使用したいと考えています。次の方法で色を検索できます。
Color selectionBackground = UIManager.getColor("nimbusSelectionBackground");
印刷すると、 Nimbus default colorsと同じ値になります。しかし、JPanel で使用すると別の灰色になります。UIManager の色を使用するにはどうすればよいですか?
私がする時:
setBackground(Color.RED);
JPanels の背景は赤で表示されますが、実行すると:
setBackground(selectionBackground);
「selectionBackground」の色は使用されませんが、灰色です。
例とスクリーンショットを次に示します。
背景は次のようにする必要があります。
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);
}
}