Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

java - Do I need another method to use the ColorFactory class?

I just found this really great ColorFactory class that I am using in my first Swing project. It is really cool: I can now pass a named color from my main class, like "crimson" or "mediumaquamarine" for example, to the createContentPane Container method.

Code:

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

Question:

Do I need the public final void setBackground(Color color, JPanel contentPane) method at all? Can everything be done inside createContentPane() method instead? Thank you for your help.

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);
    }
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Answer to your question - I can't see why (or why you needed to start with, but hay).

Extended answer:

It should be: (if we're looking at the same piece of code)

ColorFactory.getInstance().getColor(colorName);

Other wise you creating the color map on each instantiation, which is just a waste.

I'm also not sure why you need to extend JPanel, but it's not my code :P


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...