import javax.swing.*; import java.awt.*; import java.util.ArrayList; import java.util.List; public class SeleccionarAsientoV extends JPanel { JPanel panel; private JPanel pSuperior = new JPanel(new FlowLayout(FlowLayout.LEFT)); private JButton bRegresar = new JButton("Regresar."); private int noAsientosSeleccionados = 0; private JLabel lSeleccionar = new JLabel("Asientos seleccionados: " + noAsientosSeleccionados); private JPanel pInferior = new JPanel(new FlowLayout(FlowLayout.CENTER)); private int total = 0; private JLabel lTotal = new JLabel("Total: " + total + "$"); private JButton bSeleccionar = new JButton("Seleccionar"); private JPanel pSala = new JPanel(new GridBagLayout()); private JToggleButton [][]bAsientos; private Asiento [][] asientos; List asientosSeleccionados = new ArrayList(); public SeleccionarAsientoV(Asiento [][] parAsientos) throws HeadlessException { this.setLayout(new BorderLayout()); asientos = parAsientos; agregarComponentes(); añadirListeners(); crearCeldas(7,10); } public void agregarComponentes(){ pSuperior.add(this.bRegresar); pSuperior.add(lSeleccionar); pInferior.add(this.lTotal); pInferior.add(this.bSeleccionar); this.add(pSuperior,BorderLayout.NORTH); this.add(pInferior,BorderLayout.SOUTH); } public void crearCeldas(int parX, int parY){ GridBagConstraints con = new GridBagConstraints(); bAsientos = new JToggleButton[parX][parY]; String zona = ""; JToggleButton tfAux; for(int i = 0; i < parX; i++){ for(int j = 0; j < parY; j ++){ tfAux = new JToggleButton(""); if( ( (j >= 2) && (j <= 7) ) && ( (i >= 2) && (i <= 6) ) ){ tfAux.setBackground(Color.PINK); zona = "A"; tfAux.setText("200$"); } if( ((i == 1) && (j > 0) && (j < 9)) || ((j == 1) && (i > 0)) || ((j == 8) && (i > 0)) ){ tfAux.setBackground(Color.ORANGE); zona = "B"; tfAux.setText("130$"); } if( (i == 0) || (j == 0) || (j == 9)){ tfAux.setBackground(Color.YELLOW); zona = "C"; tfAux.setText("65$"); } if(asientos[i][j].isDisponibilidad()){ tfAux.setBackground(Color.DARK_GRAY); tfAux.setText("ND"); } tfAux.setSize(100,100); //tfAux.addKeyListener(new GestiónTecladoEscribirCaracter(this,i,j)); tfAux.addActionListener(new GestiónBotónSeleccionarAsiento(new Asiento(i,j,zona,asientos[i][j].isDisponibilidad()),this)); con.gridx = j; con.gridy = i; con.gridheight = 1; con.gridwidth = 1; System.out.println(i+" "+j+ " "+bAsientos.length); bAsientos[i][j] = tfAux; pSala.add(bAsientos[i][j],con); } } pSala.setBounds(0,0,700,700); this.add(pSala,BorderLayout.CENTER); } public void añadirListeners(){ //bRegresar.addActionListener(new GestiónBotónCambiarVentana(this,new ventana2().panelV2)); } public void seleccionarAsientos(Asiento asiento){ int precio = 0; int x = asiento.getX(), y = asiento.getY(); String zona = asiento.getZona(); switch (zona){ case "A": precio = 200; break; case "B": precio = 130; break; case "C": precio = 65; break; } if(!bAsientos[x][y].getText().equals("ND")) { if (bAsientos[x][y].isSelected()) { noAsientosSeleccionados++; total += precio; asientosSeleccionados.add(asiento); } else { noAsientosSeleccionados--; total -= precio; for(int a = 0; a < asientosSeleccionados.size() ;a++){ if(asientosSeleccionados.get(a).equals(asiento)){ asientosSeleccionados.remove(a); } } } }else{ bAsientos[x][y].setSelected(false); } System.out.print("["); for(int a = 0; a < asientosSeleccionados.size() ;a++){ System.out.print(asientosSeleccionados.get(a).getX() + " " + asientosSeleccionados.get(a).getY() + ", " ); } System.out.println("]"); lSeleccionar.setText("Asientos seleccionados: " + noAsientosSeleccionados); lTotal.setText("Total: " + total + "$"); } }