为什么程序在我的 showPanel 方法中的 setVisible 处抛出错误 "cannot find symbol"?
Why is the program throwing an error "cannot find symbol" at setVisible in my showPanel method?
为什么 setVisible
方法抛出一个错误,指出在我的 showPanel
方法中找不到符号?
它没有意义,因为我引用了存储在 ArrayList
中的 JPanel
,所以它应该能够使用 setVisible
.
public class mainFrame extends javax.swing.JFrame {
/**
* Creates new form mainFrame
*/
private ArrayList list;
public mainFrame() {
initComponents();
this.setSize(500,500);
int h=this.getHeight();
int w=this.getWidth();
homePanel homePnl = new homePanel();
this.add(homePnl);
homePnl.setLocation(0,0);
homePnl.setSize(w,h);
homePnl.setVisible(true);
DeploymentInfoPanel infoPanel = new DeploymentInfoPanel();
this.add(infoPanel);
infoPanel.setLocation(0,0);
infoPanel.setSize(w,h);
atomServerPanel atomPnl = new atomServerPanel();
this.add(atomPnl);
atomPnl.setLocation(0,0);
atomPnl.setSize(w,h);
autoDeploymentPanel autoPnl = new autoDeploymentPanel();
this.add(autoPnl);
autoPnl.setLocation(0,0);
autoPnl.setSize(w,h);
list = new ArrayList<>();
list.add(homePnl);
list.add(infoPanel);
list.add(atomPnl);
list.add(autoPnl);
this.pack();
}
public void showPanel(int panelNum){
list.get(1).setVisible(true);
}
private ArrayList list;
您没有指定将添加到 ArrayList 的对象类型。所以默认情况下 get() 方法将 return 一个 Object 的实例。 Object
没有 setVisible(…) 方法
当您定义 ArrayList 时,您应该使用:
private ArrayList<Component> list;
现在编译器知道您正在向 ArrayList 添加 Component
个实例。
事实上,编译器会检查以确保您只添加 Component
。
编译时也会去掉警告信息。
另外 class 名称应以大写字符开头。有时会,有时不会:
DeploymentInfoPanel infoPanel = new DeploymentInfoPanel();
...
atomServerPanel atomPnl = new atomServerPanel();
...
autoDeploymentPanel autoPnl = new autoDeploymentPanel();
注意论坛如何突出显示正确命名的 classes 使代码更易于阅读?
遵循 Java 惯例并保持一致。
最后,要在框架的同一区域显示多个面板,您应该使用 Card Layout。
为什么 setVisible
方法抛出一个错误,指出在我的 showPanel
方法中找不到符号?
它没有意义,因为我引用了存储在 ArrayList
中的 JPanel
,所以它应该能够使用 setVisible
.
public class mainFrame extends javax.swing.JFrame {
/**
* Creates new form mainFrame
*/
private ArrayList list;
public mainFrame() {
initComponents();
this.setSize(500,500);
int h=this.getHeight();
int w=this.getWidth();
homePanel homePnl = new homePanel();
this.add(homePnl);
homePnl.setLocation(0,0);
homePnl.setSize(w,h);
homePnl.setVisible(true);
DeploymentInfoPanel infoPanel = new DeploymentInfoPanel();
this.add(infoPanel);
infoPanel.setLocation(0,0);
infoPanel.setSize(w,h);
atomServerPanel atomPnl = new atomServerPanel();
this.add(atomPnl);
atomPnl.setLocation(0,0);
atomPnl.setSize(w,h);
autoDeploymentPanel autoPnl = new autoDeploymentPanel();
this.add(autoPnl);
autoPnl.setLocation(0,0);
autoPnl.setSize(w,h);
list = new ArrayList<>();
list.add(homePnl);
list.add(infoPanel);
list.add(atomPnl);
list.add(autoPnl);
this.pack();
}
public void showPanel(int panelNum){
list.get(1).setVisible(true);
}
private ArrayList list;
您没有指定将添加到 ArrayList 的对象类型。所以默认情况下 get() 方法将 return 一个 Object 的实例。 Object
当您定义 ArrayList 时,您应该使用:
private ArrayList<Component> list;
现在编译器知道您正在向 ArrayList 添加 Component
个实例。
事实上,编译器会检查以确保您只添加 Component
。
编译时也会去掉警告信息。
另外 class 名称应以大写字符开头。有时会,有时不会:
DeploymentInfoPanel infoPanel = new DeploymentInfoPanel();
...
atomServerPanel atomPnl = new atomServerPanel();
...
autoDeploymentPanel autoPnl = new autoDeploymentPanel();
注意论坛如何突出显示正确命名的 classes 使代码更易于阅读?
遵循 Java 惯例并保持一致。
最后,要在框架的同一区域显示多个面板,您应该使用 Card Layout。