Swing - 如何模仿为什么我的外观和感觉会绘制禁用的图标? [英] Swing - How to imitate the why my look and feel draws disabled icons?
问题描述
当您为 Swing 控件设置图标(比如 JButton
)时,会自动生成一个禁用的带有深灰色的图标.我希望能够模仿禁用状态更改图标颜色的方式,因此我可以稍微扭曲一下创建自己的禁用图标,并将其设置为禁用按钮.
When you set the icon for a Swing control (say a JButton
), a disabled icon with grayed-darker colors automatically generates. I want to be able to imitate the way the icon colors are changed for the disabled state, so I can create my own disabled icon with a little twist, and set it as the disabled button.
有没有办法实现这一点(除了首先实例化控件并检索它的图标,我正在寻找更直接、更简洁的方法)?
Is there a way to achieve this (other then first instantiating the control and retrieving it's icon, I'm looking for more straight-forward less hacky way)?
推荐答案
有没有办法实现这一点(除了首先实例化控件并检索它的图标
Is there a way to achieve this (other then first instantiating the control and retrieving it's icon
好吧,LAF 负责创建禁用的图标.AbstractButton
按钮类中的 getDisabledIcon()
方法如下所示:
Well the LAF is responsible for creating the disabled Icon. The getDisabledIcon()
method from the AbstractButton
button class looks something like this:
Icon disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, getIcon());
所以理论上 LAF getDisabledIcon() 方法期望组件作为参数传入.因此,您的问题的答案是:是的,需要先创建按钮".
So in theory the LAF getDisabledIcon() method expects the component to be passed in as a parameter. So the answer to your question would be: "yes, the button needs to be created first".
但是在实践中似乎(至少对于 Metal 和 Windows)该组件实际上并未用于创建图标,因此您可以执行以下操作:
However in practice it appears (for Metal and Windows at least) that the component is not actually used in the creation of the icon so you could do something like:
ImageIcon original = new ImageIcon( ... );
Icon disabled = UIManager.getLookAndFeel().getDisabledIcon(null, original);
使用这种方法是有风险的,因为其他 LAF 可能确实需要该组件来创建禁用的图标.
Using this approach is risky because other LAF's may indeed need the component to create the disabled Icon.
然而,我猜你可以创建一个单独的组件,然后用多个不同的图标调用这个方法,而不是传递 null,所以它仍然比为每个图标创建一个唯一的组件要好一些.
However, instead of passing null, I guess you could create a single component and then call this method with multiple different icons, so it would still be a little better than creating a unique component for each Icon.
这是我用来测试这种方法的 SSCCE:
Here is the SSCCE I used to test this approach:
import javax.swing.*;
import javax.swing.plaf.metal.*;
import java.awt.*;
import java.awt.image.*;
public class ButtonDisabledIcon extends JPanel
{
public ButtonDisabledIcon()
{
ImageIcon original = new ImageIcon( "dukewavered.gif" );
JButton button1 = new JButton( "Original" );
button1.setIcon( original );
add(button1);
JButton button2 = new JButton( "Disabled" );
button2.setIcon(original);
button2.setEnabled(false);
add(button2);
JButton button3 = new JButton( "LAF Disabled" );
button3.setIcon( UIManager.getLookAndFeel().getDisabledIcon(null, original) );
add(button3);
}
private static void createAndShowUI()
{
/*
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
*/
JFrame frame = new JFrame("Button Disabled Icon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ButtonDisabledIcon() );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
这篇关于Swing - 如何模仿为什么我的外观和感觉会绘制禁用的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!