【发布时间】:2012-05-07 07:43:11
【问题描述】:
当 Netbeans 扩展 JPanel 时,是否会因为它使用继承而不是组合而在引用自动生成类的实例时产生问题?
因为 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 类?
-
Overview 在 Main 类中实例化,但它太垃圾了,我省略了它,因为它是自动生成的;它在
initComponents方法中。 -
我认为是你的问题。查看错误:
location: variable overview of type JPanel。概览是面板的一个变量。您没有实例化 Overview 对象。 -
它被声明为该类型。如何更改 GUI builder 中的声明?
-
我认为变量的类型是
JPanel,而不是Overview- 使用net.bounceme.dur.nntp.view.Overview overview = new net.bounceme.dur.nntp.view.Overview();
标签: java swing oop user-interface netbeans