简体   繁体   English

从子类中删除JLabel

[英]Removing a JLabel from a subclass

Say I have this class: 说我有这堂课:

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);
    }
}

And this also: 而且这也:

public class EndView extends ExitView {

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

This code is stripped way down, but in my code this is not removing the JLabel in EndView . 这段代码被剥离了下来,但是在我的代码中,这并没有删除EndView的JLabel。 I am able to just title.setText("") but that's not really getting rid of it. 我可以只是title.setText("")但这并没有真正摆脱它。 Can anybody explain why it wouldn't remove the label? 谁能解释为什么不删除标签? Thanks. 谢谢。

you are creating a label in the base class constructor and adding this directly to the JPanel. 您正在基类构造函数中创建标签,并将其直接添加到JPanel。 But in the later check you don't use the reference to that created JLabel but some other. 但是在以后的检查中,您将不使用对创建的JLabel的引用。 b/c the variable is out of scope it might be difficult to reference it again. b / c该变量超出范围,可能很难再次引用它。 The fix is as Paco mentioned. 修复方法如Paco所述。 bring the variable out of the constructor scope and set it globably. 将变量带出构造函数范围并进行全局设置。

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
     }
}

Try making the jlabel a field of the class: 尝试使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);
    }
}

Edited 编辑
Look at this minimal example, it works: 看这个最小的例子,它的工作原理是:

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);
    }
}

Your error must be on other place. 您的错误必须在其他地方。 How do you use EndView? 您如何使用EndView?

When you use the extends ( some class ) in Java thenthe class that extends 在Java中使用扩展( 某些类 )时,扩展的类

this can see only the variables(Objects) that are out of constructor and other methods and are ( public or protected ) but not ( private ). 这只能看到不在构造函数和其他方法中并且是( 公共受保护 )但( 私有变量(对象 )。

public class ExitView extends JPanel {

private static final long serialVersionUID = 1L;
public JLabel title=new JLabel("Exit?"); //Here

public ExitView() {
   title.setFont(new Font("Ariel", Font.PLAIN, 44));
        this.add(title);
 }
}

The other 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)
 }
}

Also check this link implements Vs Extends why and why not.. 还要检查此链接是否实现了Vs Extends为什么以及为什么没有。

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

Can anybody explain why it wouldn't remove the label? 谁能解释为什么不删除标签? Thanks. 谢谢。

Because: 因为:

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);
    }
}

Then: 然后:

@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.
    }
}

There is a way to actually remove the component but it is messy. 有一种方法可以删除组件,但是很麻烦。 What I would suggest you do instead of having a JLabel containing another JLabel , change title to be a String . 我建议您执行的操作,而不JLabel包含另一个JLabel ,将title更改为String You can simply change the text property of the label by setting it to empty String, or by setting the visibility to false. 您可以通过将标签的text属性设置为空String或将可见性设置为false来简单地更改标签的text属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM