Java继承,使用super()
分配的这一部分是为了显示来自java库的继承。我必须创建一个java类,该类在调用时将颜色标签设置为适当的背景:例如,如果main说JLabel colorfulLabel=new JLabelColor.Blue;它创建了一个蓝色背景的标签 以下是我当前的彩色标签类代码:Java继承,使用super(),java,swing,inheritance,constructor,Java,Swing,Inheritance,Constructor,分配的这一部分是为了显示来自java库的继承。我必须创建一个java类,该类在调用时将颜色标签设置为适当的背景:例如,如果main说JLabel colorfulLabel=new JLabelColor.Blue;它创建了一个蓝色背景的标签 以下是我当前的彩色标签类代码: import javax.swing.*; import java.awt.*; public class colorfulLabel extends JLabel{ /*constructor uses one c
import javax.swing.*;
import java.awt.*;
public class colorfulLabel extends JLabel{
/*constructor uses one color parameter to represent background color
creates label using background color
calls parent constructor using super()
private Color color;*/
public colorfulLabel(Color color){
super(color);
JLabel l1 = new JLabel();
setBackground(color);
}
}
ps:是的,这只是一小段代码,但我在GUI方面遇到了极大的困难,更不用说实现继承了
我在尝试调用父构造函数时出错
您试图调用一个不存在的构造函数,supercolor。请检查以下内容,以了解哪些构造函数是合法的。你为什么要调用这个构造函数?
在JLabel扩展类中创建另一个JLabel没有什么意义。
如果计划查看背景色,可能需要在构造函数内部使对象不透明。
最好传入文本,或者通过添加颜色参数来模拟其他有效的JLabel构造函数:
public class ColorLabel extends JLabel {
public ColorLabel(String text, Color bg) {
super(text); // **this** super makes sense
setBackground(bg);
setOpaque(true);
}
// + overloads for other constructors that accept Icon or text and Icon, or
// text, Icon and position,....
}
您试图调用一个不存在的构造函数,supercolor。请检查以下内容,以了解哪些构造函数是合法的。你为什么要调用这个构造函数?
在JLabel扩展类中创建另一个JLabel没有什么意义。
如果计划查看背景色,可能需要在构造函数内部使对象不透明。
最好传入文本,或者通过添加颜色参数来模拟其他有效的JLabel构造函数:
public class ColorLabel extends JLabel {
public ColorLabel(String text, Color bg) {
super(text); // **this** super makes sense
setBackground(bg);
setOpaque(true);
}
// + overloads for other constructors that accept Icon or text and Icon, or
// text, Icon and position,....
}
试试这个:
// by convention class names should start with an uppercase character
public colorfulLabel(Color color) { character
super(); // it's not necessary in this case, it's implicit
setBackground(color);
}
JLabel没有将颜色作为参数接收的构造函数,因此这就是导致错误的原因。还请注意,您实际上不需要为此扩展类,也不需要添加额外的属性或功能,通过类型为JLabel的实例对setBackground的简单调用即可满足您的所有需要 试试这个:
// by convention class names should start with an uppercase character
public colorfulLabel(Color color) { character
super(); // it's not necessary in this case, it's implicit
setBackground(color);
}
JLabel没有将颜色作为参数接收的构造函数,因此这就是导致错误的原因。还请注意,您实际上不需要为此扩展类,也不需要添加额外的属性或功能,通过类型为JLabel的实例对setBackground的简单调用即可满足您的所有需要 按照惯例,所有类名都以大写字符开头。尽可能快地遵守它,这将使您以后的Java生活更加轻松。我在尝试调用父构造函数时出错。-当您询问某个错误时,请始终发布整个错误消息。请不要让我们猜。按照惯例,所有类名都以大写字母开头。尽可能快地遵守它,这将使您以后的Java生活更加轻松。我在尝试调用父构造函数时出错。-当您询问某个错误时,请始终发布整个错误消息。请不要让我们猜。就像我说的,我对java中的GUI很反感。它处于试错阶段,但任务的这一部分说明如下:一个名为ColorfulLabel的类扩展了JLabel。构造函数将使用一个颜色参数来表示标签的bg颜色。构造函数将创建具有特定bg颜色的jlabel对象。不要忘记使用Super调用父构造函数,然后设置bg颜色。例如:ColorfulLabel blueLabel=新的ColorfulLabelColor.Blue@user3288392:这不是一个特定于GUI的问题。这只不过是一个学习阅读JavaAPI类型的问题,就像我说的,我对Java中的GUI很反感。它处于试错阶段,但任务的这一部分说明如下:一个名为ColorfulLabel的类扩展了JLabel。构造函数将使用一个颜色参数来表示标签的bg颜色。构造函数将创建具有特定bg颜色的jlabel对象。不要忘记使用Super调用父构造函数,然后设置bg颜色。例如:ColorfulLabel blueLabel=新的ColorfulLabelColor.Blue@user3288392:这不是一个特定于GUI的问题。这其实只不过是一个学习阅读Java API类型的问题。我这么做只是因为作业中指定了这样做的部分……或者我假设是这样的……上面写着:一个名为ColorfulLabel的类扩展了JLabel。构造函数将使用一个颜色参数来表示标签的bg颜色。构造函数将创建具有特定bg颜色的jlabel对象。不要忘记使用Super调用父构造函数,然后设置bg颜色。例如:ColorfulLabel blueLabel=新的ColorfulLabelColor。Blue@user3288392好的,这就是我上面的答案。请看editI,之所以这样做,是因为指定的赋值部分……或者我假设是这样……它说:一个名为ColorfulLabel的类扩展了JLabel。构造函数将使用一个颜色参数来表示标签的bg颜色。构造函数将创建具有特定bg颜色的jlabel对象。不要忘记使用Super调用父构造函数,然后设置bg颜色。ex:ColorfulLabel bl
ueLabel=新颜色FULLABELCOLOR。Blue@user3288392好的,这就是我上面的答案。请参见编辑