2011-01-24 80 views
2

我一直跑到我班上的'找不到符号'错误。该变量在超类中明确声明,但子类无法看到它。除了子类RecordViewer中的JLabel的新构造函数外,我不会收到任何错误。Java类子类变量引用

class RecordViewer extends JDialog{ 
    private JButton next; 
    private JButton prev; 
    private JLabel label; 
    private int current; 

    public RecordViewer(CDinventoryItem [] array){ 
     super(); 
     current = 0; 
     final CDinventoryItem [] items = array; 

     label = new JLabel(items[getCurrent()]); 

预定义的toString从我CDinventoryItem类...

 @Override public String toString(){ 

     // Decimal foramting for the inventory values 
     NumberFormat dformat = new DecimalFormat("#0.00"); 

     // Formatting for the inventory output 
     StringBuilder ouput = new StringBuilder(); 
     String New_Line = System.getProperty("line.separator"); 

      ouput.append("The product number of my CD is: ").append(iPitemNumber).append (New_Line); 
      ouput.append("The title of the CD is: ").append(sPtitle).append (New_Line); 
      ouput.append("I have ").append(iPnumberofUnits).append(" units in stock.").append (New_Line); 
      ouput.append("The total value of my inventory on this product is: ").append(dformat.format(stockValue())).append (New_Line); 
      return ouput.toString(); 
     } 
+1

请提供整个错误消息。 – adamax 2011-01-24 20:39:10

回答

3

这是标准的Java JLabel吗?您试图传递CDinventoryItem类型的对象,并且JLabel将不具有处理该类型参数的构造函数,除非它扩展了String或Icon类。

2
  • 组织你的进口,使JLabel正常
  • JLabel进口没有定义构造函数把你的自定义类型。你需要在那里传递一个字符串。
+0

是的,我有导入javax.swing。*;在顶部。我刚刚添加了导入javax.swing.JLabel;如你所建议的。仍然没有变化。 – user569127 2011-01-24 20:35:05

+0

@ user569127是否使用IDE? – Bozho 2011-01-24 20:37:39

+0

是的,我正在使用NetBeans – user569127 2011-01-24 20:42:48

0

只是一个猜测,但你的问题的措辞意味着你想直接从一个子类使用私人领域?除了声明它们的类外,其他任何地方都不能看到私有字段,包括同一继承层次结构中的类。你可以把它保护起来,但是这是被忽视的 - 更好的是提供一个mutator方法来标记或者更好,然后在超类的构造函数中初始化它。