我正在嘗試創建自己的GUI庫,並且在超級類中遇到了一些困難(這對我來說是新的,並且找不到一個很好的學習來源)。但是我的所有組件都是從「GGUI」擴展而來,然後將它們存儲在一個ArrayList中以便於訪問。我的問題是,我得到一個錯誤:「隱式超級構造函數GrimGUI.GGUI()是未定義的,必須顯式調用另一個構造函數」。從列表中訪問超類的原始類
GrimGUI:
Button testScreen;
Slider firstSlider;
Slider secondSlider;
Text firstSliderText;
Text secondSliderText;
boolean onHome;
boolean onTest;
ArrayList<GGUI> gguiScreen = new ArrayList<GGUI>();
void setup() {
size(500, 500);
onHome = true;
background(255);
gguiScreen.add(new Button(75, 50, 40, 20));
testScreen = new Button(115, 50, 40, 20);
testScreen.cNormal = color(0, 255, 125);
firstSlider = new Slider(250, 250, 100);
secondSlider = new Slider(250, 280, 100);
firstSliderText = new Text(280, 250, "0");
secondSliderText = new Text(280, 280, "0");
}
void draw() {
background(255);
HomeScreen();
TestScreen();
endFix();
}
void HomeScreen() {
if (onHome) {
testScreen.Update();
firstSlider.Update();
firstSliderText.content = str(firstSlider.value);
firstSliderText.Update();
if (testScreen.released) {
onTest = true;
onHome = false;
}
}
}
void TestScreen() {
if (onTest) {
Button trash = (Button)gguiScreen.get(0);//Testing that I can get the only object placed in it to view on screen
trash.Update();
secondSlider.Update();
secondSliderText.content = str(secondSlider.value);
secondSliderText.Update();
if (trash.WasClicked()) {
onTest = false;
onHome = true;
}
}
}
按鈕:
class Button extends GGUI {
boolean visible, pressed, released, hovering;
color cNormal, cPressed, cReleased, cHovering;
float x, y, w, h;
Button(float X, float Y, float W, float H) {
visible = true;
x = X;
y = Y;
w = W;
h = H;
cNormal = color(75, 75, 75);
cPressed = color(100, 100, 100);
cReleased = color(125, 125, 125);
cHovering = color(175, 175, 175);
}
Button(boolean visibility, float X, float Y, float W, float H) {
visible = visibility;
x = X;
y = Y;
w = W;
h = H;
cNormal = color(75, 75, 75);
cPressed = color(100, 100, 100);
cReleased = color(125, 125, 125);
cHovering = color(175, 175, 175);
}
Button(boolean visibility, float X, float Y, float W, float H, color CNORMAL, color CPRESSED) {
visible = visibility;
x = X;
y = Y;
w = W;
h = H;
cNormal = CNORMAL;
cPressed = CPRESSED;
cReleased = color(125, 125, 125);
cHovering = color(175, 175, 175);
}
Button(boolean visibility, float X, float Y, float W, float H, color CNORMAL, color CPRESSED, color CHOVERING) {
visible = visibility;
x = X;
y = Y;
w = W;
h = H;
cNormal = CNORMAL;
cPressed = CPRESSED;
cHovering = CHOVERING;
cReleased = color(125, 125, 125);
}
Button(boolean visibility, float X, float Y, float W, float H, color CNORMAL, color CPRESSED, color CHOVERING, color CRELEASED) {
visible = visibility;
x = X;
y = Y;
w = W;
h = H;
cNormal = CNORMAL;
cPressed = CPRESSED;
cHovering = CHOVERING;
cReleased = CRELEASED;
}
void Update() {
CheckState();
if (visible) {//WILL NOT RENDER IF NOT VISIBLE
if (pressed && !released) {// IF PRESSED
fill(cPressed);
}
if (released && !pressed) {// IF RELEASED
fill(cReleased);
}
if (hovering && !released && !pressed) {// IF HOVERING
fill(cHovering);
}
if (!hovering && !released && !pressed) {// IF NORMAL/NOT ACTIVE
fill(cNormal);
}
stroke(1);
rect(x, y, w, h);
}
}
void CheckState() {
hovering = false;
released = false;
if (mouseX > x && mouseX < x + w) {
if (mouseY > y && mouseY < y + h) {
hovering = true;
}
}
if (hovering) {
if (mPressed) {
pressed = true;
released = false;
}
if (mClicked) {
released = true;
pressed = false;
}
} else {
pressed = false;
released = false;
hovering = false;
}
}
boolean WasClicked() {
return pressed;
}
}
GGUI:
class GGUI {
int x, y;
boolean visible;
color cBase, cSecondary;
GGUI(int _x, int _y, boolean _visible, color _cBase, color _cSecondary) {
x = _x;
y = _y;
visible = _visible;
cBase = _cBase;
cSecondary = _cSecondary;
}
void Update() {
println("Updated");
}
}
這是非常簡單的。只要你沒有在Super-Class中定義任何構造函數,Java就會在編譯時創建一個構造函數。如果你自己沒有調用超類,它還會從每個擴展類創建一個對這個構造函數的調用。 但是你定義了幾個不同的構造函數,並且在你的超類中沒有默認構造函數。由於您不得不至少調用一個超級構造函數,但Java不能爲您選擇,所以會引發錯誤。 要麼在你的超類中實現一個空的構造函數,要麼從你的每個本地構造函數中調用你的超級構造函數。 – DiabolicWords