有没有办法在Java Jframe中自动编辑按钮的属性?

发布于 2025-02-06 06:59:06 字数 1398 浏览 3 评论 0原文

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

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

发布评论

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

评论(1

捎一片雪花 2025-02-13 06:59:06

您可以...

使用“ nofollow noreferrer”>“建筑商”

该概念是提供一个工作流程,使您可以应用所需的所有属性,然后将该对象“构建”,例如...

public class ButtonBuilder<Builder extends ButtonBuilder<Builder>> {
    private JButton button;

    public ButtonBuilder() {
        button = new JButton();
    }
    
    public Builder withText(String text) {
        button.setText(text);
        return (Builder)this;
    }
    
    public Builder withIcon(Icon icon) {
        button.setIcon(icon);
        return (Builder)this;
    }
    
    public Builder borderPainted(boolean painted) {
        button.setBorderPainted(painted);
        return (Builder)this;
    }
    
    public Builder contentAreaFilled(boolean painted) {
        button.setContentAreaFilled(painted);
        return (Builder)this;
    }
    
    public Builder focusPainted(boolean painted) {
        button.setFocusPainted(painted);
        return (Builder)this;
    }
    
    public JButton build() {
        return button;
    }
}

nb:请注意,建造者的常见模式是将属性存储在某些中一种缓存/查找,然后在您调用构建时应用它们,但是在这种情况下,将它们直接应用于按钮本身

nb:显然,我只提供了一个小子集您可能需要为按钮指定的属性,您需要添加其余的;)

,然后您可以使用类似的内容来构建按钮

JButton happyButton = new ButtonBuilder()
        .withText("Happy")
        .withIcon(new ImageIcon(ImageIO.read(getClass().getResource("/images/happy.png"))))
        .borderPainted(false)
        .focusPainted(false)
        .contentAreaFilled(false)
        .build();

但这不是我问的,我不想每次创建一个按钮

重新申请所有属性

是的,我知道,我都必须重新应用所有属性。一旦拥有“基础”构建器,就可以制作一个或多个“自定义”扩展名,例如,可以直接应用默认值...

public class MyCustomButtonBuilder extends ButtonBuilder<MyCustomButtonBuilder> {
    public MyCustomButtonBuilder(String text) {
        this(text, null);
    }
    
    public MyCustomButtonBuilder(Icon icon) {
        this(null, icon);
    }
    
    public MyCustomButtonBuilder(String text, Icon icon) {
        super();
        withText(text)
                .withIcon(icon)
                .borderPainted(false)
                .focusPainted(false)
                .contentAreaFilled(false);
    }
}

然后可以使用类似的东西...

JButton sadButton = new MyCustomButtonBuilder("Sad", new ImageIcon(ImageIO.read(getClass().getResource("/images/Sad.png"))))
        .build();

您可以...

使用a < a href =“ https://www.javatpoint.com/factory-method-design-pattern#:%7e:text = a%20factory%20Pattern%20Pattern%20OR%20Factory,%20Instance%20Instance%20OF%20The%20CLASS。” ”

public class ButtonFactory {
    public static JButton makePlainButton() {
        return makePlainButton(null, null);
    }

    public static JButton makePlainButton(String text) {
        return makePlainButton(text, null);
    }

    public static JButton makePlainButton(String text, Icon icon) {
        JButton btn = new JButton(text, icon);
        btn.setBorderPainted(false);
        btn.setContentAreaFilled(false);
        btn.setFocusPainted(false);
        btn.setOpaque(false);
        return btn;
    }
}

rel =“ nofollow noreferrer

JButton heartButton = ButtonFactory.makePlainButton(
        "Heart", 
        new ImageIcon(ImageIO.read(getClass().getResource("/images/Heart.png")))
);

”>“工厂模式 根据您的共同需求。

您可以...

结合两个概念,例如...

public class ButtonFactory {
    public static JButton makePlainButton() {
        return makePlainButton(null, null);
    }

    public static JButton makePlainButton(String text) {
        return makePlainButton(text, null);
    }

    public static JButton makePlainButton(String text, Icon icon) {
        return new ButtonBuilder()
                .withText(text)
                .withIcon(icon)
                .borderPainted(false)
                .focusPainted(false)
                .contentAreaFilled(false)
                .build();
    }
}

You could...

Use a "builder pattern"

The concept is to provide a workflow which allows you to apply all the properties you want and then have that object "built", for example...

public class ButtonBuilder<Builder extends ButtonBuilder<Builder>> {
    private JButton button;

    public ButtonBuilder() {
        button = new JButton();
    }
    
    public Builder withText(String text) {
        button.setText(text);
        return (Builder)this;
    }
    
    public Builder withIcon(Icon icon) {
        button.setIcon(icon);
        return (Builder)this;
    }
    
    public Builder borderPainted(boolean painted) {
        button.setBorderPainted(painted);
        return (Builder)this;
    }
    
    public Builder contentAreaFilled(boolean painted) {
        button.setContentAreaFilled(painted);
        return (Builder)this;
    }
    
    public Builder focusPainted(boolean painted) {
        button.setFocusPainted(painted);
        return (Builder)this;
    }
    
    public JButton build() {
        return button;
    }
}

nb: Note, a common pattern for a builder is store the properties in some kind of cache/lookup, which are then applied when you call build, but in this case, it's just easier to apply them directly to the button itself

nb: Obviously, I've only supplied a small subset of properties you might want to specify for a button, you'll need to add the rest ;)

And then you can build the button using something like...

JButton happyButton = new ButtonBuilder()
        .withText("Happy")
        .withIcon(new ImageIcon(ImageIO.read(getClass().getResource("/images/happy.png"))))
        .borderPainted(false)
        .focusPainted(false)
        .contentAreaFilled(false)
        .build();

But that's not what I asked, I don't want to have to have reapply the all the properties each time I want to create a button

Yes, I know, I was getting to it. Once you have a "base" builder, you could make one or more "custom" extensions, which could apply default values directly, for example...

public class MyCustomButtonBuilder extends ButtonBuilder<MyCustomButtonBuilder> {
    public MyCustomButtonBuilder(String text) {
        this(text, null);
    }
    
    public MyCustomButtonBuilder(Icon icon) {
        this(null, icon);
    }
    
    public MyCustomButtonBuilder(String text, Icon icon) {
        super();
        withText(text)
                .withIcon(icon)
                .borderPainted(false)
                .focusPainted(false)
                .contentAreaFilled(false);
    }
}

which could then be used something like...

JButton sadButton = new MyCustomButtonBuilder("Sad", new ImageIcon(ImageIO.read(getClass().getResource("/images/Sad.png"))))
        .build();

You could...

Use a "factory pattern", for example...

public class ButtonFactory {
    public static JButton makePlainButton() {
        return makePlainButton(null, null);
    }

    public static JButton makePlainButton(String text) {
        return makePlainButton(text, null);
    }

    public static JButton makePlainButton(String text, Icon icon) {
        JButton btn = new JButton(text, icon);
        btn.setBorderPainted(false);
        btn.setContentAreaFilled(false);
        btn.setFocusPainted(false);
        btn.setOpaque(false);
        return btn;
    }
}

which could be used something like...

JButton heartButton = ButtonFactory.makePlainButton(
        "Heart", 
        new ImageIcon(ImageIO.read(getClass().getResource("/images/Heart.png")))
);

The factory pattern could allow you to supply a multiple number of different types of buttons, configured different based on your common needs.

You could...

Combine the two concepts, for example...

public class ButtonFactory {
    public static JButton makePlainButton() {
        return makePlainButton(null, null);
    }

    public static JButton makePlainButton(String text) {
        return makePlainButton(text, null);
    }

    public static JButton makePlainButton(String text, Icon icon) {
        return new ButtonBuilder()
                .withText(text)
                .withIcon(icon)
                .borderPainted(false)
                .focusPainted(false)
                .contentAreaFilled(false)
                .build();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文