2015-02-05 20 views
0

刪除一個JLabel說我有這個類:從子類

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

該代碼被剝離一路下滑,但在我的代碼,這是不刪除的JLabel在EndView。我能夠只是title.setText("")但這並沒有真正擺脫它。任何人都可以解釋爲什麼它不會刪除標籤嗎?謝謝。

回答

0

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

+0

將其更改爲此(因爲它總是有意義的)並使得可見性受到保護,但仍得到相同的結果...不從子類中刪除它。 – user3226170 2015-02-06 00:08:30

+0

編輯與工作示例。 – 2015-02-06 07:44:49

1

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

當您使用extends(一些類)中的Java擴展

這隻能看到變量(對象)出構造等方法和thenthe類(公開保護)但不是(私人)。

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

還要檢查這個環節implements Vs Extends why and why not..

0

任何人都可以解釋爲什麼它不會刪除標籤嗎?謝謝。

因爲:

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,更改titleString。只需將標籤的文本屬性設置爲空字符串或將可見性設置爲false即可。