Array that Holds JButton Objects

前端 未结 4 1634
情歌与酒
情歌与酒 2020-12-10 19:08

Ok, so I\'m attempting to do an exercise from a book I\'m using to learn Java. Here is the code that I have so far:

import javax.swing.*;
import java.awt.Gri         


        
4条回答
  •  孤城傲影
    2020-12-10 19:46

    JButton[] buttons = new JButton[10]
    

    The line above is correct, but I see two points of confusion:

    1. This line:

      buttons[0] = "button0";
      

      should instead be as follows:

      buttons[0] = new JButton("button0");
      

      The reason is that in your code, you're trying to assign a String to buttons[0], instead of the expected JButton.

    2. Ok, so I tried declaring the array with the Buttons[] numbuttons line, but that just gave me the error: Multiple markers at this line -Buttons can not be resolved to a type -Buttons can not be resolved to a type

      Buttons is not a standard Java class. Do a case-sensitive search for Buttons and replace all matches with JButton.

    If you still have problems, please copy and paste the exact code for each variation that is not working.

提交回复
热议问题