简体   繁体   English

在自定义Synth Look&Feel中控制复选框的布局

[英]Controlling layout of a checkbox in a custom Synth Look & Feel

I'm trying to implement a custom "Steampunk" themed look and feel using Synth - basically providing custom versions of SynthStyle, SynthPainter and SynthStyleFactory. 我正在尝试使用Synth实现自定义的“ Steampunk”主题外观,基本上是提供SynthStyle,SynthPainter和SynthStyleFactory的自定义版本。

I am not using any XML, ie everything is done through the Java API. 我没有使用任何XML,即一切都通过Java API完成。 In general this is working just fine and actually starting to look pretty decent. 总的来说,这很好,实际上看起来还不错。

However I am having trouble with some "composite" components such as JCheckBox. 但是,我在使用某些“复合”组件(例如JCheckBox)时遇到了麻烦。 When the paintCheckBoxBackground() is called on the SynthPainter the coordinates refer to the whole area covered by the JCheckBox. 在SynthPainter上调用paintCheckBoxBackground()时,坐标指的是JCheckBox覆盖的整个区域。

How should I determine where in this region the check box icon and text separately need to be drawn? 我应如何确定需要在该区域的哪个位置分别绘制复选框图标和文本?

Well, mb the following will help (the code is to put into your custom painter associated with JCheckBox): 嗯,下面的内容会有所帮助(将代码放入与JCheckBox关联的自定义画板上):

public void paintCheckBoxBackground(SynthContext context, Graphics g, int x, int y, int w, int h){

    AbstractButton c = (AbstractButton) context.getComponent();
    String text = c.getText();
    Icon icon = c.getIcon();
    if (icon == null){
        icon = context.getStyle().getIcon(context, "CheckBox.icon");
    };        
    int iconTextGap = c.getIconTextGap();       

    Rectangle componentRect = new Rectangle();
    Rectangle iconR = new Rectangle();

    Insets insets = new Insets(0, 0, 0, 0);
    if (context.getRegion().isSubregion()){
        insets = context.getStyle().getInsets(context, insets);
    }
    else{
        insets = context.getComponent().getInsets(insets);
    }

    componentRect.x = insets.left;
    componentRect.y = insets.top;
    componentRect.width = c.getWidth() - (insets.left + insets.right);
    componentRect.height = c.getHeight() - (insets.top + insets.bottom);

    if (icon != null){
        iconR.x += componentRect.x;
        iconR.y += componentRect.y;
        iconR.width = icon.getIconWidth();
        iconR.height = icon.getIconHeight();

        g.setColor(Color.GREEN);
        g.fillRect(iconR.x, iconR.y, iconR.width, iconR.height);
    }
    if (text != null){
        g.setColor(Color.RED);
        int textPos = iconR.x + iconR.width + iconTextGap;
        g.fillRect(textPos, iconR.y, c.getWidth() - insets.right - textPos, componentRect.height);            
    }
}

Please, take into account, that here only the most common case is taken into account (Left-Right alignment, text right of the icon). 请注意,这里仅考虑最常见的情况(左对齐,图标文本右对齐)。 For more complex case treatment see source code for SwingUtilities.layoutCompoundLabel 有关更复杂的案例处理,请参见SwingUtilities.layoutCompoundLabel源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM