无法访问在“设计”视图中创建的Netbeans数组

时间:2017-07-26 02:18:41

标签: java netbeans

我试图创建一个简单的扫雷游戏(用户可自定义)。 在编写时,我尝试使用netbeans设计视图创建JButtons网格(通过JButton数组)并将此代码输入JPanel

JButton mines[] = new JButton [gridXVal*gridYVal];
mineGrid.setLayout(new java.awt.GridLayout(gridXVal,gridYVal));
for (int i = 0 ; i < mines.length ; i++)
        {
            mines [i] = new JButton(" ");
            mines [i].setActionCommand ("" + i);
            mines[i].addActionListener(listener);
            //mines [i].setBorder (null);
            //mines [i].setBorderPainted (false);
            mineGrid.add (mines [i]);
}

稍后,当我通过方法

访问它时
ImageIcon icon = new ImageIcon(mineCounter+".png");
mines[a].setIcon(icon);

它给我一个错误,"cannot find symbol"。我尝试配置JPanel以便它是公开的,但我仍然无法在我的方法中预先生成的代码之外编辑它。

当我点击试图将图像设置为某个按钮的按钮时,这是我得到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: 
   Uncompilable source code - Erroneous sym type: <any>.setIcon
    at gameScreen.clickDraw(gameScreen.java:227)
    at gameScreen$3.actionPerformed(gameScreen.java:170)

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

我假设这段代码:

mineGrid.setLayout(new java.awt.GridLayout(1, 0));
JButton mines[] = new JButton [gridXVal*gridYVal];
mineGrid.setLayout(new java.awt.GridLayout(gridXVal,gridYVal));
for (int i = 0 ; i < mines.length ; i++)
{
    mines [i] = new JButton(" ");
    mines [i].setActionCommand ("" + i);
    mines[i].addActionListener(listener);
    //mines [i].setBorder (null);
    //mines [i].setBorderPainted (false);
    mineGrid.add (mines [i]);
}
您通过Netbeans中的一个“代码”编辑选项手动输入

你需要取JButton mines[] = new JButton [gridXVal*gridYVal];并在课程级别声明它并修改你的“自定义代码”来初始化这个变量......

private JButton mines[];

private void initComponents() {
    //...
    mineGrid.setLayout(new java.awt.GridLayout(1, 0));
    JButton mines[] = new JButton [gridXVal*gridYVal];
    mineGrid.setLayout(new java.awt.GridLayout(gridXVal,gridYVal));
    for (int i = 0 ; i < mines.length ; i++)
    {
        mines [i] = new JButton(" ");
        mines [i].setActionCommand ("" + i);
        mines[i].addActionListener(listener);
        //mines [i].setBorder (null);
        //mines [i].setBorderPainted (false);
        mineGrid.add (mines [i]);
    }
    //...
}// </editor-fold>      

答案 1 :(得分:0)

这是NetBeans&#39;生成的代码,包括矿山JButton数组。

    private void initComponents() {

        ctText = new javax.swing.JLabel();
        ct = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        ct1 = new javax.swing.JLabel();
        testBtn = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        mineGrid = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        ctText.setText("BombCount:");

        ct.setText("###");

        jLabel1.setText("Time:");

        ct1.setText("000");

        testBtn.setText("Reset");
        testBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testBtnActionPerformed(evt);
            }
        });

        mineGrid.setLayout(new java.awt.GridLayout(1, 0));
        JButton mines[] = new JButton [gridXVal*gridYVal];
        mineGrid.setLayout(new java.awt.GridLayout(gridXVal,gridYVal));
        for (int i = 0 ; i < mines.length ; i++)
        {
            mines [i] = new JButton(" ");
            mines [i].setActionCommand ("" + i);
            mines[i].addActionListener(listener);
            //mines [i].setBorder (null);
            //mines [i].setBorderPainted (false);
            mineGrid.add (mines [i]);
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(ctText)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ct)
                .addGap(53, 53, 53)
                .addComponent(testBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ct1)
                .addGap(45, 45, 45))
            .addComponent(jSeparator1)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mineGrid, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ctText)
                    .addComponent(ct)
                    .addComponent(jLabel1)
                    .addComponent(ct1)
                    .addComponent(testBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mineGrid, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

我尝试在clickDraw方法中访问数组:

public void clickDraw(int a) {
        System.out.println(Arrays.deepToString(mineField));
        int row = a / gridXVal;
        int col = a % gridYVal;
        int mineCounter = 0;

        if (mineField[row][col] == 1) {
            loseGame();
        }
        if (mineField[row + 1][col] == 1) {
            mineCounter++;
        }
        if (mineField[row - 1][col] == 1) {
            mineCounter++;
        }
        if (mineField[row][col + 1] == 1) {
            mineCounter++;
        }
        if (mineField[row][col - 1] == 1) {
            mineCounter++;
        }
        if (mineField[row + 1][col + 1] == 1) {
            mineCounter++;
        }
        if (mineField[row + 1][col - 1] == 1) {
            mineCounter++;
        }
        if (mineField[row - 1][col + 1] == 1) {
            mineCounter++;
        }
        if (mineField[row - 1][col - 1] == 1) {
            mineCounter++;
        }
        ImageIcon icon = new ImageIcon(mineCounter+".png");
        mines[a].setIcon(icon);
        /*if (mineCounter ==0)
         adjacentDraw(a);*/
    }

在ActionListener,listener:

中调用该方法
ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JButton) {
                int clickedTile = Integer.parseInt(e.getActionCommand());
                clickDraw(clickedTile);
            }
        }
    };