【问题标题】:Getting an error whilst adding items to an arraylist将项目添加到数组列表时出错
【发布时间】:2014-03-13 23:28:16
【问题描述】:

当我尝试将项目添加到数组列表时出现错误,我不确定我做错了什么。

ArrayList<JButton> buttonList = new ArrayList<JButton>();
buttonList.add(button);
buttonList.add(button1);
buttonList.add(button2);

我得到的错误是:

Syntax error on token "button", VariableDeclaratorId expected after this token
Syntax error on token "button1", VariableDeclaratorId expected after this token
Syntax error on token "button2", VariableDeclaratorId expected after this token

完整代码;

public buttons {
    JButton button = new JButton(); 
    JButton button1 = new JButton();
    JButton button2 = new JButton();

    ArrayList<JButton> buttonList = new ArrayList<JButton>();

    buttonList.add(button);
    buttonList.add(button1);
    buttonList.add(button2);

    public static void main(String args[]) {
    }
}

【问题讨论】:

  • 你在哪里声明 button button1button2
  • 请出示您的全部代码。看起来ArrayList&lt;JButton&gt; buttonList 是你的类的一个字段,你没有在类构造函数中初始化它。
  • buttons 应该是一个类吗?

标签: java swing arraylist


【解决方案1】:

问题是不能直接在类定义中添加Java代码,而是在方法中添加。

移动这部分代码:

buttonList.add(button);
buttonList.add(button1);    
buttonList.add(button2);

进入类构造函数:

public buttons() {
    buttonList.add(button);
    buttonList.add(button1);    
    buttonList.add(button2);
}

另外,最好在类构造函数中初始化这些变量:

public buttons {

    JButton button; 
    JButton button1 = new JButton();
    JButton button2 = new JButton();

    ArrayList<JButton> buttonList;

    public buttons() {
        button = new JButton();
        button1 = new JButton();
        button2 = new JButton();
        buttonList = new ArrayList<JButton>();
        //...
        buttonList.add(button);
        buttonList.add(button1);
        buttonList.add(button2);
    }

    public static void main(String args[]){

    }
}

【讨论】:

  • 我必须在同一个类的其他方法中使用变量,这就是我将它们设为全局的原因。
  • @user3288493 问题不在于使变量全局,问题在于如何使用它们。如果您注意到,我的回答将变量保留为您的类中的字段,只需将初始化移动到类构造函数。
【解决方案2】:

放在main方法中

public static void main(String args[]){
    JButton button = new JButton(); 
    JButton button1 = new JButton();
    JButton button2 = new JButton();

     ArrayList<JButton> buttonList = new ArrayList<JButton>();
        buttonList.add(button);
        buttonList.add(button1);
        buttonList.add(button2);

}

【讨论】:

    【解决方案3】:

    或者,您也可以在此代码中使用 instance initializer(注意 buttonList.add() 语句周围的左大括号和右大括号):

    public class Button {
      JButton button = new JButton();
      JButton button1 = new JButton();
      JButton button2 = new JButton();
      ArrayList<JButton> buttonList = new ArrayList<JButton>();
    
      {
        buttonList.add(button);
        buttonList.add(button1);
        buttonList.add(button2);
      }
    
      // rest of the code ...
    }
    

    【讨论】:

      【解决方案4】:

      变量: 按钮、按钮 1 或按钮 2 不存在

      试试

      ArrayList<JButton> buttonList = new ArrayList<JButton>();
      JButton button = new JButton();
      buttonList.add(button);
      JButton button1 = new JButton();
      buttonList.add(button1);
      JButton button2 = new JButton();
      buttonList.add(button2);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-03
        • 2015-06-12
        • 1970-01-01
        • 2017-05-04
        • 2022-06-29
        • 1970-01-01
        • 1970-01-01
        • 2021-07-09
        相关资源
        最近更新 更多