【问题标题】:Do I need another method to use the ColorFactory class?我需要另一种方法来使用 ColorFactory 类吗?
【发布时间】:2012-07-25 16:05:31
【问题描述】:

我刚刚发现在我的第一个 Swing 项目中使用的这个非常棒的 ColorFactory 类。真的很酷:我现在可以将主类中的命名颜色(例如“Crimson”或“mediumaquamarine”)传递给createContentPaneContainer 方法。

代码:

frame.setContentPane(ContentPaneCreator.createContentPane("darkorange"));`

问题:

我需要public final void setBackground(Color color, JPanel contentPane) 方法吗?一切都可以在createContentPane() 方法中完成吗?感谢您的帮助。

import java.awt.Color;
import java.awt.Container;
import javax.swing.JPanel;

public final class ContentPaneCreator extends JPanel {

    private static final long serialVersionUID = 1L;

    public static Container createContentPane(String color) {

        JPanel contentPane = new JPanel();

        // awesome txt to Color conversions using the ColorFactory().getColor();
        // written by The Lobo Project
        new ContentPaneCreator().setBackground(
                new ColorFactory().getColor(color), contentPane);

        contentPane.setOpaque(true);
        return contentPane;
    }

    public final void setBackground(Color color, JPanel contentPane) {
        contentPane.setBackground(color);
    }
)

【问题讨论】:

    标签: java swing methods


    【解决方案1】:

    有几件事可能需要更仔细的审查:

    【讨论】:

      【解决方案2】:

      回答你的问题 - 我不明白为什么(或者为什么你需要开始,但是干草)。

      扩展答案:

      应该是:(如果我们正在查看同一段代码)

      ColorFactory.getInstance().getColor(colorName);
      

      否则,您会在每个实例化时创建颜色图,这只是一种浪费。

      我也不确定你为什么需要扩展 JPanel,但这不是我的代码:P

      【讨论】:

      • 哇.. 哇。我正在扩展 JPanel,因为我预计将来会使用它的功能。
      • 嘿,这是你的设计,如果你需要它,你就需要:)
      猜你喜欢
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 2021-01-28
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多