【发布时间】: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);
}
)
【问题讨论】: