當Netbeans擴展JPanel時,當引用自動生成的類的實例時,是否會產生問題,因爲它使用繼承而不是組合?class is -a-JPanel when it have -a
由於Netbeans聲明JPanel爲private javax.swing.JPanel overview;
,因此實例具有一種JPanel類型,它應具有一種Overview類型。
如何調用setGroup方法,並將Overview作爲自動生成的JPanel保存?我試着像這樣:
Main.java
private void groupsPropertyChange(java.beans.PropertyChangeEvent evt) {
String s = evt.getNewValue().toString();
LOG.fine("new group: " + s);
overview.setGroup(s);
}
與Overview.java:
public class Overview extends javax.swing.JPanel {
...
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
}
,但得到這個編譯錯誤:
init:
Deleting: /home/thufir/NetBeansProjects/SwingNNTP/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/SwingNNTP/build/built-jar.properties
Compiling 1 source file to /home/thufir/NetBeansProjects/SwingNNTP/build/classes
warning: [options] bootstrap class path not set in conjunction with -source 1.5
/home/thufir/NetBeansProjects/SwingNNTP/src/net/bounceme/dur/nntp/view/Main.java:59: error: cannot find symbol
overview.setGroup(s);
symbol: method setGroup(String)
location: variable overview of type JPanel
1 error
1 warning
/home/thufir/NetBeansProjects/SwingNNTP/nbproject/build-impl.xml:628: The following error occurred while executing this line:
/home/thufir/NetBeansProjects/SwingNNTP/nbproject/build-impl.xml:285: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)
你在哪裏實例化Overview類? – Stephan
概述在Main類中實例化,但它太垃圾了,我省略了它,因爲它是自動生成的;它在'initComponents'方法中。 – Thufir
我認爲你的問題。看看這個錯誤:'位置:JPanel類型的變量概述「。概述是面板的一個變量。你沒有實例化一個Overview對象。 – Stephan