简体   繁体   English

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

[英]Netbeans array created in Design view isn't accessible

I was trying to create a simple minesweeper game (that's user-customizable). 我试图创建一个简单的扫雷游戏(用户可自定义)。 In writing it, I tried to create a grid of JButtons (via a JButton array) by using the netbeans design view and entering this code into a JPanel : 在编写它时,我尝试使用netbeans设计视图(通过JButton数组)创建JButtons网格并将此代码输入到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]);
}

Later, when I access it in a method via 稍后,当我通过

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

It gives me an error saying, "cannot find symbol" . 这给我一个错误,说"cannot find symbol" I tried configuring the JPanel so that it was public, but I'm still not able to edit it outside the pre-generated code in my methods. 我尝试配置JPanel ,使其公开,但是仍然无法在方法中预先生成的代码之外对其进行编辑。

This is the error I get when I click on a button trying to set its image to something: 这是我在单击按钮尝试将其图像设置为某种东西时遇到的错误:

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)

Any help would be greatly appreciated! 任何帮助将不胜感激!

I'm assuming that this section of code: 我假设这段代码:

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]);
}

was entered by you manually through one of the "code" editing options in Netbeans. 是您通过Netbeans中的“代码”编辑选项之一手动输入的。

You need to take JButton mines[] = new JButton [gridXVal*gridYVal]; 您需要使用JButton mines[] = new JButton [gridXVal*gridYVal]; and declare it at the class level and modify your "custom code" to initialise this variable... 并在类级别进行声明,并修改您的“自定义代码”以初始化此变量...

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>      

This is NetBeans' generated code, including the mines JButton array. 这是NetBeans生成的代码,包括我的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>                        

And I try to access the array in the clickDraw method: 我尝试在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);*/
    }

That method is called in an ActionListener, listener: 该方法在ActionListener侦听器中调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM