我使用Java的Swing工具箱設置了一個大型GUI(比以前做的任何工作都大),並且我想設置自己的自定義配色方案來繪製顏色,以便所有顏色定義在一個地方。爲此,我決定製作一個名爲ColorPalette
(應用於https://stackoverflow.com/a/7486111/4547020後)的僞靜態頂級類別,其中包含SchemeEnum
,其中程序員爲整個GUI設置顏色方案。Java Swing模塊化配色方案
我希望顏色選擇獨立於顏色方案的知識。有沒有人知道設計模式或有效的方法來做到這一點?我不完全相信,我目前的設置是實現這一目標的最佳方式,但我想建立一個模塊化的設計,它不會打擾添加更多ColorEnums
或SchemeEnums
(在編譯時,不運行)。
爲了清楚起見,我想程序員能夠簡單地選擇一個ColorEnum
並獲得返回的基礎上,ColorEnum
和定義SchemeEnum
一個java.awt.Color
對象。
例如:
// 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());
應該比
// 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());
返回不同Color
對象,因爲它們有不同的SchemeEnums
,即使它們是從ColorPalette
請求相同的顏色。這樣,更改一行代碼更改(或顏色甚至可以在運行時更改)GUI中更改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();
}
}
覺得這將是簡單直接定義顏色到UIManager的或創建自己的外觀和感覺,也許使用[Synth](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/synth.html) – MadProgrammer 2015-02-09 20:43:32
大多數JComponents都有屬性數組,例如作爲JButton,這些屬性對於所有可能的事件(選擇,按下,武裝,...)都是不同的,使用自定義L&F,在某些情況下可以設置顏色方案(以避免重新發明輪子) – mKorbel 2015-02-10 08:30:35
Java Swing模塊化顏色方案== UIManager中的密鑰循環 – mKorbel 2015-02-10 08:31:38