Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从子类中删除JLabel_Java - Fatal编程技术网

Java 从子类中删除JLabel

Java 从子类中删除JLabel,java,Java,假设我有这门课: public class ExitView extends JPanel { private static final long serialVersionUID = 1L; public ExitView() { JLabel title = new JLabel("Exit?"); title.setFont(new Font("Ariel", Font.PLAIN, 44)); this.add(

假设我有这门课:

public class ExitView extends JPanel {

    private static final long serialVersionUID = 1L;

    public ExitView() {
            JLabel title = new JLabel("Exit?");
    title.setFont(new Font("Ariel", Font.PLAIN, 44));
            this.add(title);
    }
}
这也是:

public class EndView extends ExitView {

    public ExitView() {
            this.remove(title);
    }
}

此代码被精简,但在我的代码中,这并不是在
EndView
中删除JLabel。我只能
title.setText(“”)
,但这并不能真正摆脱它。有人能解释为什么它不去掉标签吗?谢谢。

尝试将jlabel设置为类的字段:

public class ExitView extends JPanel {

    private static final long serialVersionUID = 1L;
    private JLabel title;

    public ExitView() {
            title = new JLabel("Exit?");
    title.setFont(new Font("Ariel", Font.PLAIN, 44));
            this.add(title);
    }
}
已编辑
看看这个最小的例子,它是有效的:

public class ExitView extends JPanel{
    protected JLabel label;

    public ExitView() {
        label = new JLabel("your label");
        this.add(label);
    }

    public static void main(String[] args) {
        JDialog dialog = new JDialog();
        EndView endView= new EndView();

        dialog.add(endView);
        dialog.pack();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }
}

class EndView extends ExitView {
    public EndView() {
        this.remove(label);
    }
}

你的错误一定在别的地方。如何使用EndView?

您正在基类构造函数中创建一个标签,并将其直接添加到JPanel中。但是在后面的检查中,您不使用对创建JLabel的引用,而是使用其他一些。b/c变量超出范围,可能很难再次引用它。修复方法如Paco所述。将变量带出构造函数范围并全局设置它

public class ExitView extends JPanel {

private static final long serialVersionUID = 1L;

    public ExitView() {
        JLabel title = new JLabel("Exit?"); // <- here you create a JLabel 
                                            // in that scope where is the               
                                            // reference for later use??
        title.setFont(new Font("Ariel", Font.PLAIN, 44));
        this.add(title);
    }
}

public class EndView extends ExitView {

     public ExitView() {
        this.remove(title); <- // the reference you create here doesn't 
                               // equals the JLabel created earlier
     }
}
公共类ExitView扩展了JPanel{
私有静态最终长serialVersionUID=1L;
公共存在视图(){
JLabel title=new JLabel(“Exit?”);//当您在Java中使用扩展(some class)时,则使用扩展的类

这只能看到构造函数和其他方法之外的变量(对象),它们是(公共的受保护的),而不是(私有的)

另一类:

@SuppressWarnings("serial")

public class EndView extends ExitView {

public EndView() {
        this.remove(title); //now it can see the JLabel cause
                            //it is out of constructor and other
                           //methods and is (public or protected)
 }
}

同时检查此链接

您还在EndView中使用ExitView构造函数

有人能解释一下为什么它不会去掉标签吗?谢谢

因为:

public class ExitView extends JPanel {

    private static final long serialVersionUID = 1L;

    public ExitView() {
        JLabel title = new JLabel("Exit?"); // local JLabel instance
    title.setFont(new Font("Ariel", Font.PLAIN, 44));
        this.add(title);
    }
}
然后:

有一种方法可以实际删除组件,但它很混乱。我建议您不要让
JLabel
包含另一个
JLabel
,而是将
title
更改为
String
。只需将标签的文本属性设置为空字符串,或将可见性设置为fa即可更改标签的文本属性lse.

将其更改为此(因为它完全有意义),并使可见性受到保护,但仍然得到相同的结果…没有将其从子类中删除。
public class ExitView extends JPanel {

    private static final long serialVersionUID = 1L;

    public ExitView() {
        JLabel title = new JLabel("Exit?"); // local JLabel instance
    title.setFont(new Font("Ariel", Font.PLAIN, 44));
        this.add(title);
    }
}
@SuppressWarnings("serial")

public class EndView extends ExitView {

    public EndView() {
        this.remove(title); //now it can see the JLabel cause
                            //it is out of constructor and other
                            //methods and is (public or protected)
                            //'title' is not the same 'title' JLabel
                            // set in the superclass.
    }
}