簡體   English   中英

從子類中刪除JLabel

[英]Removing a JLabel from a subclass

說我有這堂課:

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("")但這並沒有真正擺脫它。 誰能解釋為什么不刪除標簽? 謝謝。

您正在基類構造函數中創建標簽,並將其直接添加到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
     }
}

嘗試使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?

在Java中使用擴展( 某些類 )時,擴展的類

這只能看到不在構造函數和其他方法中並且是( 公共受保護 )但( 私有變量(對象 )。

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

另一類:

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

還要檢查此鏈接是否實現了Vs Extends為什么以及為什么沒有。

您還在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);
    }
}

然后:

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

有一種方法可以刪除組件,但是很麻煩。 我建議您執行的操作,而不JLabel包含另一個JLabel ,將title更改為String 您可以通過將標簽的text屬性設置為空String或將可見性設置為false來簡單地更改標簽的text屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM