如何使用 actionPerformed 方法和 jbutton 将值添加到整数数组列表?

How to add values to the integer array list using actionPerformed method & jbutton?

我对这段代码有一点小问题。我会说这是一件小事。每次按下按钮时,我都想向 arrayList 添加一个值。当我按下按钮时,现在发生了什么,它只保存已单击的最后一个值。我如何才能保存所有值?

这是一段代码...

public void actionPerformed(ActionEvent e) {

            seatArr = new ArrayList<Integer>();
            int a;

            if(e.getSource() instanceof JButton){

                bookedIcon = new ImageIcon("images/bookedSeat.png");

                a = Integer.parseInt(e.getActionCommand());
                seat = a;
                a =a- 1;
                seatsA[a].setIcon(bookedIcon);

                    seatArr.add(seat);
                try {
                    stmt.executeUpdate("UPDATE seats SET type = 'booked' WHERE seatNum ="+seat);
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

    }

我不知道可能是什么问题,我看起来没问题... 这是代码的其余部分....

//start of  class------------------------------------------------
public class SeatMap extends JFrame implements ActionListener{

    static Connection conn;
    static Statement stmt, stmt1;
    static ResultSet rs, rs1;
    JComboBox <Integer> adultCombo;
    JComboBox <String> childCombo;
    String[] adultArray = {"0", "1", "2", "3"};
    String[] childArray = {"0", "1", "2"};
    JLabel childLabel;
    Icon bookedIcon;
    JButton nextButton, previousButton;
    public static ArrayList<Integer> seatArr;

    int seat = 0;
    int counter;
    JButton[] seatsA = new JButton[30];


//start of main--------------------------------------------------
    public static void main(String [] args){
        JFrame frame = new SeatMap();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//closes the program when x is pressed
    }
//start of frame------------------------------------------------
    public SeatMap(){
        super("Pick a Seat");

        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://localhost/airlines","root", "");
            //Create a statement 
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT seatNum, type FROM seats");



        //---------------------------------------------------
        JPanel comboPanel = new JPanel();
        JLabel adultLabel = new JLabel("Adult Tickets:  ");
        childLabel = new JLabel("Child Ticket:  ");
        childLabel.setAlignmentX(LEFT_ALIGNMENT);
        childLabel.setVisible(false);

        adultCombo = new <String>JComboBox(adultArray);
        adultCombo.addActionListener(this);

        childCombo = new <String>JComboBox(childArray);
        childCombo.setVisible(false);

        comboPanel.add(adultLabel);
        comboPanel.add(adultCombo);
        comboPanel.add(childLabel);
        comboPanel.add(childCombo);
        Icon itbIcon = new ImageIcon("images/freeSeat.png");

        //setting center panel----------------------------------------------
            JPanel centerPanel = new JPanel();
            centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
            centerPanel.add(comboPanel);
        //setting label for seats------------------------------------------------
            JLabel label1 = new JLabel("Pick a seat: ", SwingConstants.CENTER);
            label1.setFont(new Font("Sans-serif", Font.PLAIN, 25));
            label1.setAlignmentX(CENTER_ALIGNMENT);
            centerPanel.add(label1);

            JPanel panel1 = new JPanel(new GridLayout(10,3));


        //for loop to print seats------------------------------------------------------------------------
            while(rs.next()){
                //System.out.println(rs.getString("seatNum"));

                int c = Integer.parseInt(rs.getString("seatNum"));
                 c = c - 1;

                seatsA[c] = new JButton(String.valueOf(c + 1));

                seatsA[c].setBackground(Color.WHITE);

                if(rs.getString("type").equals("free")){
                    seatsA[c].setIcon(itbIcon);
                    seatsA[c].addActionListener(this);
                }

                else if(rs.getString("type").equals("booked")){
                    bookedIcon = new ImageIcon("images/bookedSeat.png");
                    seatsA[c].setIcon(bookedIcon);
                    seatsA[c].setEnabled(false);
                }

                panel1.add(seatsA[c]);


            //}
            panel1.setAlignmentX(CENTER_ALIGNMENT);
            centerPanel.add(panel1);
            }
            //adding buttons---------------------------------------------

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
            buttonPanel.setAlignmentX(CENTER_ALIGNMENT);

            previousButton = new JButton("Previous");
            previousButton.setBackground(new Color(102,200,1));
            previousButton.setFont(new Font("Sans-serif", Font.PLAIN, 20));
            previousButton.setAlignmentX(JButton.CENTER_ALIGNMENT);
            previousButton.addActionListener(new ActionListener() { 
                  public void actionPerformed(ActionEvent e) { 
                        dispose();
                        try {
                            rs.close();
                            stmt.close();
                            conn.close();
                        } catch (SQLException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        seatArr.clear();
                        JFrame form = new Form();

                    }} );
            buttonPanel.add(previousButton);

            nextButton = new JButton("Next");
            nextButton.setBackground(new Color(102,200,1));
            nextButton.setFont(new Font("Sans-serif", Font.PLAIN, 20));
            nextButton.setAlignmentX(JButton.CENTER_ALIGNMENT);
            nextButton.addActionListener(new ActionListener() { 
                  public void actionPerformed(ActionEvent e) { 
                        try {

                            String resNum = Form.getReservationNum();
                            System.out.println(seatArr);
                            stmt.executeUpdate("UPDATE users SET seats='"+seatArr+"' WHERE reservationNum ='"+resNum+"'");

                            seatArr.clear();
                            dispose();

                            rs.close();
                            stmt.close();
                            conn.close();
                        } catch (SQLException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        JFrame ticket = new Ticket();
                      } 
                    } );


            buttonPanel.add(nextButton);

            centerPanel.add(buttonPanel);
            //-----------------------------------------------------------

    //building header and footer-------------------------------------------


            JPanel header = new JPanel();
            HeadAndFooter.head(header);
            JPanel footer = new JPanel();
            HeadAndFooter.footer(footer);


    //adding panels to the content pane------------------------------------
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BorderLayout());
            mainPanel.add(header, BorderLayout.NORTH);
            mainPanel.add(centerPanel, BorderLayout.CENTER);
            mainPanel.add(footer, BorderLayout.SOUTH);
            getContentPane().add(mainPanel);//adding panels to the frame
        //----------------------------------------------------------------------------------------------
        setVisible(true);
        setSize(700,650);



        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

public void actionPerformed(ActionEvent e) {

            seatArr = new ArrayList<Integer>();
            int a;

            if(e.getSource() instanceof JButton){

                bookedIcon = new ImageIcon("images/bookedSeat.png");

                a = Integer.parseInt(e.getActionCommand());
                seat = a;
                a =a- 1;
                seatsA[a].setIcon(bookedIcon);

                    seatArr.add(seat);
                try {
                    stmt.executeUpdate("UPDATE seats SET type = 'booked' WHERE seatNum ="+seat);
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

        String choice = adultCombo.getSelectedItem().toString();

        if(choice.equals("0")){
            childCombo.setVisible(false);
            childLabel.setVisible(false);   
        }

        else{
            childCombo.setVisible(true);
            childLabel.setVisible(true);
        }
    }

}

seatArr = new ArrayList();

问题是您每次执行操作时都在初始化 seatArr 列表,所以基本上它会重置列表并向其添加最新值。