2016-04-11 18 views
0

all Bu kod parçasında küçük sorunum var. Küçük bir şey olduğunu söyleyebilirim. Düğmeye her basışımda arrayList'e bir değer eklemek istiyorum. Düğmeye bastığımda şimdi ne oluyor, sadece tıklanan son değeri kaydediyor. Tüm değerleri saklamak için nasıl yapabilirim? İşte actionPerformed method & jbutton'u kullanarak tamsayı dizi listesine değerler nasıl eklenir?

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

    } 

Ben sorun olabilir hiçbir fikrim yok ... kod parçasıdır, bu İşte ... bana tamamdır, kodun geri kalanı ....

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

} 

cevap

1

seatArr = new ArrayList();

Sorun, eylemi her gerçekleştirdiğinizde seatArr listesini başlatıyor olmanızdır, bu nedenle temel olarak listeyi sıfırlar ve en son değeri ona ekler.

+0

Gerçekten ... teşekkürler! –

İlgili konular