Swing 改变 JSpinner 的前后颜色 [英] Swing change the JSpinner back and fore colors
问题描述
如何在 Swing 中更改 JSpinner
的背景和前景色?
除了覆盖 paint()
方法之外,还有什么简单的解决方案吗?
设置背景或前景色不起作用,至少对我而言.
我还测试了所有默认的外观和感觉,但没有成功.
JDK:1.7.0.51 Win764
提前致谢
How can I change the background and foreground color of a JSpinner
in swing?
is there any simple solution except override the paint()
method?
setting the background or foreground color doesn't work, at least for me.
I also tested with all default look and feels, no success.
JDK: 1.7.0.51 Win764
Thanks in advance
推荐答案
JSpinner
是一个复合组件.简单地说:默认情况下,它包含一个显示值的 JFormattedTextField
,以及两个用于增加和减少值的按钮.
The JSpinner
is a compound component. Simply said: By default, it conains a JFormattedTextField
that shows the value, and two buttons for increasing and decreasing the value.
因此,设置这些颜色的一种方法可能是从微调器获取文本字段,并设置其前景色和背景色:
One way to set these colors may thus be to obtain the text field from the spinner, and set its foreground and background color:
Component c = spinner.getEditor().getComponent(0);
c.setForeground(Color.BLUE);
c.setBackground(Color.RED);
但您可能必须使其更加健壮,以确保它适用于所有 LookAndFeels 和所有编辑器,而不管微调器(和编辑器)在内部是如何构建的.
but you might have to make this more robust to make sure that it works for all LookAndFeels, and all editors, regardless of how the spinner (and the editor) are constructed internally.
EDIT 回应评论:它在这种情况下有效吗?
EDIT in response to the comment: Does it work in this case?
import java.awt.Color;
import java.awt.Component;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class SpinnerColors
{
public static void main(String[] args)
{
// Optionally:
setLookAndFeel();
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void setLookAndFeel()
{
try
{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSpinner spinner = new JSpinner();
setColors(spinner, Color.BLUE, Color.RED);
f.getContentPane().add(spinner);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static void setColors(
JSpinner spinner, Color foreground, Color background)
{
JComponent editor = spinner.getEditor();
System.out.println("Editor "+editor);
int n = editor.getComponentCount();
for (int i=0; i<n; i++)
{
Component c = editor.getComponent(i);
System.out.println("Component "+i+": "+c);
if (c instanceof JTextField)
{
c.setForeground(foreground);
c.setBackground(background);
}
}
}
}
请注意,如果您使用Nimbus"外观和感觉,事情可能会更加困难.Nimbus 以忽略背景颜色而闻名......
Note that if you are using the "Nimbus" look and feel, things might be more difficult. Nimbus is known for notoriously ignoring background colors....
EDIT2 回应第二条评论:基本上可以访问按钮,但对于默认的 L&F,这些是专门的按钮 (BasicArrowButton
),其中paint
方法被覆盖以绘制微小的向上"和向下"箭头.所以在这些上设置前景不会有任何影响.相反,您必须修改 L&F 默认值,这可能总是很麻烦.所以这可能会也可能不会达到您想要的效果:
EDIT2 in response to the second comment: It is basically possible to access the buttons, but for the default L&Fs, these are specialized buttons (BasicArrowButton
) where the paint
method is overridden to paint the tiny "up" and "down" arrows. So setting the foreground on these will not have any effect. Instead, you'd have to modify the L&F defaults, which may always be a hassle. So this might or might not have the effect that you want to achieve:
private static void setButtonColors(
JSpinner spinner, Color foreground, Color background)
{
int n = spinner.getComponentCount();
for (int i=0; i<n; i++)
{
Component c = spinner.getComponent(i);
if (c instanceof JButton)
{
c.setForeground(foreground); // Has no effect
c.setBackground(background);
}
}
}
这篇关于Swing 改变 JSpinner 的前后颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!