开发者社区> 问答> 正文

为什么程序在showPanel方法中的setVisible抛出错误“找不到符号”?

为什么setVisible方法抛出错误,提示我的showPanel方法中找不到符号?由于我引用的是一个JPanel存储区,ArrayList因此没有任何意义,因此应该可以使用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);
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-25 20:50:34 588 0
1 条回答
写回答
取消 提交回答
  • private ArrayList list;
    

    您没有指定要添加到ArrayList的Object的类型。因此,默认情况下,get()方法将返回Object的实例。没有setVisible(...)方法Object

    定义ArrayList时,应使用:

    private ArrayList<Component> list;
    

    现在,编译器知道您正在将Component实例添加到ArrayList中。

    实际上,编译器将检查以确保仅添加Component。

    编译时,它也会摆脱警告消息。

    另外,类名也应以大写字母开头。有时您会做,有时却不会:

    DeploymentInfoPanel infoPanel = new DeploymentInfoPanel();
    ...
    atomServerPanel atomPnl = new atomServerPanel();
    ...
    autoDeploymentPanel autoPnl = new autoDeploymentPanel();
    

    注意到论坛如何突出显示正确命名的类,从而使代码更易于阅读?

    遵循Java约定并保持一致。

    最后,要在框架的同一区域显示多个面板,您应该使用Card Layout

    回答来源:Stack Overflow

    2020-03-25 20:53:47
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载