gpt4 book ai didi

Java Swing 模块化配色方案

转载 作者:搜寻专家 更新时间:2023-11-01 01:51:32 49 4
gpt4 key购买 nike

我正在使用 Java 的 Swing 工具包设置一个大型 GUI(比我以前做过的任何东西都大),我想设置我自己的自定义配色方案来绘制颜色,以便所有颜色定义都在一个地方。为此,我决定制作一个名为 ColorPalette 的伪静态顶级类(应用自 https://stackoverflow.com/a/7486111/4547020 帖子),其中包含一个 SchemeEnum 程序员在其中设置整个 GUI 的配色方案。

我希望颜色选择独立于配色方案知识。有谁知道设计模式或有效的方法来做到这一点?我不完全相信我当前的设置是实现此目的的最佳方式,但我想设置一个模块化设计,在其中添加更多 ColorEnumsSchemeEnums(在编译时,而非运行时)。

为了清楚起见,我希望程序员能够简单地选择一个 ColorEnum 并返回一个 java.awt.Color 对象基于 ColorEnum 和定义的 SchemeEnum

例如:

        // Use the BASIC color scheme
ColorPalette.setCurrentScheme(ColorPalette.SchemeEnum.BASIC);

// Set button backgrounds
testButton.setBackground(ColorPalette.ColorEnum.DARK_RED.getColor());
testButton2.setBackground(ColorPalette.ColorEnum.BLUE.getColor());

应该返回不同于

Color对象
        // Use the DARK color scheme
ColorPalette.setCurrentScheme(ColorPalette.SchemeEnum.DARK);

// Set button backgrounds
testButton.setBackground(ColorPalette.ColorEnum.DARK_RED.getColor());
testButton2.setBackground(ColorPalette.ColorEnum.BLUE.getColor());

因为它们有不同的 SchemeEnums,即使它们从 ColorPalette 请求相同的颜色。这样,更改 SchemeEnum 只需更改一行代码即可更改 GUI 中的每种颜色(或者甚至可以在运行时更改颜色)。

我听说过 HashTables 被用于像这样的大数据存储,但我不知道它们是如何工作的。这可能适用于此吗?

到目前为止,这是我的代码。提前致谢!

package common.lookandfeel;

import java.awt.Color;

/**
* Class which contains the members for the color scheme used throughout the project.
* <p>This class is essentially static (no constructor, class is final, all members static) and
* should not be instantiated.
*/
public final class ColorPalette
{
/**
* The list of color schemes to choose from.
*/
public static enum SchemeEnum
{
BASIC, DARK, METALLIC
}

/**
* The list of color descriptions to choose from.
*/
public static enum ColorEnum
{
LIGHT_RED(256,0,0), RED(192,0,0), DARK_RED(128,0,0),
LIGHT_GREEN(0,256,0), GREEN(0,192,0), DARK_GREEN(0,128,0),
LIGHT_BLUE(0,0,256), BLUE(0,0,192), DARK_BLUE(0,0,128),
LIGHT_ORANGE(256,102,0), ORANGE(256,102,0), DARK_ORANGE(192,88,0),
LIGHT_YELLOW(256,204,0), YELLOW(256,204,0), DARK_YELLOW(192,150,0),
LIGHT_PURPLE(136,0,182), PURPLE(102,0,153), DARK_PURPLE(78,0,124);

private int red;
private int green;
private int blue;

private ColorEnum(int r, int g, int b)
{
this.red = r;
this.green = g;
this.blue = b;
}

/**
* Get the selected color object for this Enum.
* @return The color description as a Color object.
*/
public Color getColor()
{
// WANT TO RETURN A COLOR BASED ON currentScheme
return new Color(red, green, blue);
}
}

private static SchemeEnum currentScheme = SchemeEnum.BASIC;

/**
* Default constructor is private to prevent instantiation of this makeshift 'static' class.
*/
private ColorPalette()
{
}

/**
* Get the color scheme being used on this project.
* @return The current color scheme in use on this project.
*/
public static SchemeEnum getCurrentScheme()
{
return currentScheme;
}

/**
* Set the overall color scheme of this project.
* @param currentPalette The color scheme to set for use on this project.
*/
public static void setCurrentScheme(SchemeEnum cp)
{
currentScheme = cp;
}

/**
* Main method for test purposes only. Unpredictable results.
* @param args Command line arguments. Should not be present.
*/
public static void main(String[] args)
{
// Declare and define swing data members
JFrame frame = new JFrame("Test Environment");
CustomButton testButton = new CustomButton ("Hello World");
CustomButton testButton2 = new CustomButton ("I am a button!");

// Use a particular color scheme
ColorPalette.setCurrentScheme(ColorPalette.SchemeEnum.BASIC);

// Set button backgrounds
testButton.setBackground(ColorPalette.ColorEnum.DARK_RED.getColor());
testButton2.setBackground(ColorPalette.ColorEnum.BLUE.getColor());

// Place swing components in Frame
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(testButton, BorderLayout.NORTH);
frame.getContentPane().add(testButton2, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);

// Set allocated memory to null
frame = null;
testButton = null;
testButton2 = null;

// Suggest garbage collecting to deallocate memory
System.gc();
}
}

最佳答案

看起来和听起来您只需要将 SchemeEnum 组合成由 ColorEnum 组成,就像您让 ColorEnum 由 rgb 值组成一样。

public static enum SchemeEnum
{
// Don't really know what colors you actually want
BASIC(ColorEnum.RED, ColorEnum.GREEN, ColorEnum.ORANGE),
DARK(ColorEnum.DARK_RED, ColorEnum.DARK_GREEN, ColorEnum.DARK_ORANGE),
METALLIC(ColorEnum.LIGHT_RED, ColorEnum.LIGHT_GREEN, ColorEnum.LIGHT_ORANGE);

// nor know how many colors make up a scheme
public ColorEnum mainColor;
public ColorEnum secondaryColor;
public ColorEnum borderColor;

private SchemeEnum(ColorEnum mainColor, ColorEnum secondaryColor,
ColorEnum borderColor)
{
this.mainColor = mainColor;
this.secondaryColor = secondaryColor;
this.borderColor = borderColor;
}
}

然后,使用如下代码,其中颜色基于所选方案:

testButton.setBackground(ColorPalette.getCurrentScheme().mainColor.getColor());

关于Java Swing 模块化配色方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28417136/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com