java - 每次按下按钮时将值添加到 ArrayList<Number>

标签 java arraylist actionlistener

这是我在这里发表的第一篇文章,希望我能写对!!

我做了什么,我创建了一个 NxN 板,其中 JButtons 指示从 [0, 0] 到 [9, 9] 的坐标。 每次单击按钮时,控制台都会显示坐标,我尝试做的是将这些坐标保存在 ArrayList 中,通过按另一个窗口中的第二个按钮来显示该坐标...没什么花哨的,抱歉,只是把我的头绕过去基本概念...

问题是我无法将值保存到 ArrayList 中,并且一旦按下第二个按钮我就无法调用它...... 附上我的类(class)的代码...每个代码都在不同的文件中。

Board.java

public class Board{

public Board(){

    JFrame win = new JFrame ();
    GridLayout layout = new GridLayout(10, 10);
    win.setLayout(layout);
    for (int row1 = 0; row1 < 10 ; row1 = row1+1){
        for (int col1 = 0; col1 < 10; col1 = col1+1){
            JPanel jp = new JPanel();
            JButton jb = new JButton("[" + row1 + "," + col1 + "]");
            jb.addActionListener(new ButtonEventHandler(row1, col1));
            jp.add(jb);
            win.add(jp);
        }
        win.setVisible(true);
        win.pack();
}

    JFrame win2 = new JFrame("Stored values");
    win2.setVisible(true);
    JPanel jp2 = new JPanel();
    win2.add(jp2);
    JButton jb2 = new JButton("Check Log");
    jb2.addActionListener(new Position(win2, jb2));
    jp2.add(jb2);
    win2.pack();
}}

ButtonEventHandler.java

public class ButtonEventHandler implements ActionListener {


private int _row1;
private int _col1;
private ArrayList<Number> _list;


public ButtonEventHandler(int row1, int col1){

    _row1 = row1;
    _col1 = col1;
    _list = new ArrayList<Number>();
}

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("Position: " + _row1 + ", " + _col1);
    _list.add(_row1);
    _list.add(_col1);
}

public ArrayList<Number> getList(){

    return _list;
}}

Position.java

public class Position implements ActionListener  {

private JFrame _win2;

private JButton _jb2;
private int _row1;
private int _col1;
private ArrayList<Number> _list;

private ButtonEventHandler beh = new ButtonEventHandler(_row1, _col1);


public Position(JFrame win2, JButton jb2){
    _win2 = win2;
    _jb2 = jb2;     
}

@Override
public void actionPerformed(ActionEvent e) {

    System.out.println(beh.getList());      
}
}

非常感谢您的帮助!!

塞布

最佳答案

代码中的问题是您没有一个数组列表:您有许多数组列表。每个按钮处理程序都有自己的!

您应该创建一个数组列表,并通过将其传递给所有处理程序的构造函数来在所有处理程序之间共享它。

public Position(JFrame win2, JButton jb2, AttayList<Number> list){
    _win2 = win2;
    _jb2 = jb2;
    _list = list;
}

public ButtonEventHandler(int row1, int col1, AttayList<Number> list) {
    _row1 = row1;
    _col1 = col1;
    _list = list;
}

public Board(){
    JFrame win = new JFrame ();
    GridLayout layout = new GridLayout(10, 10);
    win.setLayout(layout);
    ArrayList myList = new ArrayList<Number>();
    // In the code below, use myList as the last parameter to the constructors of ButtonEventHandler and Position
    ...

}

关于java - 每次按下按钮时将值添加到 ArrayList<Number>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10441976/

相关文章:

java - JButton打印一封信

java - 将 Java 对象从 JSP 传递到 Servlet

java - Android:Arraylist 声明期间出现问题

java - 尝试对多个按钮使用 1 个 Action 监听器

java - 重复项未被删除

android - 在android中使用Arraylist搜索字符串

jsf - 无法让 MenuItem 调用其 ActionListeners

java - 使用 JMockit 从模拟构造函数返回实际实例

java - 使用元注释触发注释处理器

java - 在java程序中打开另一个面板后面板不会释放