控制自定义 Synth Look 中复选框的布局感觉

发布于 2024-09-08 21:11:17 字数 326 浏览 9 评论 0原文

我正在尝试使用 Synth 实现自定义的“蒸汽朋克”主题外观和感觉 - 基本上提供 SynthStyle、SynthPainter 和 SynthStyleFactory 的自定义版本。

我没有使用任何 XML,即一切都是通过 Java API 完成的。总的来说,这工作得很好,而且实际上开始看起来相当不错。

然而,我在使用一些“复合”组件(例如 JCheckBox)时遇到了麻烦。当在 SynthPainter 上调用 PaintCheckBoxBackground() 时,坐标指的是 JCheckBox 覆盖的整个区域。

我应该如何确定该区域中的哪个位置需要分别绘制复选框图标和文本?

I'm trying to implement a custom "Steampunk" themed look and feel using Synth - basically providing custom versions of SynthStyle, SynthPainter and SynthStyleFactory.

I am not using any XML, i.e. everything is done through the 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. When the paintCheckBoxBackground() is called on the SynthPainter the coordinates refer to the whole area covered by the JCheckBox.

How should I determine where in this region the check box icon and text separately need to be drawn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2024-09-15 21:11:17

嗯,mb以下内容会有所帮助
(代码将放入与 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);            
    }
}

请考虑到,这里仅考虑最常见的情况(左右对齐,图标的文本右侧)。
对于更复杂的情况处理,请参阅 SwingUtilities.layoutCompoundLabel 的源代码

Well, mb the following will help
(the code is to put into your custom painter associated with 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文