













































Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!














































public FinestraPrincipal() { finestraPrincipal = this; this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("aplicacio"); iniciaColeccions(); // this.getContentPane().add(new FinestraDesti(this), // BorderLayout.CENTER); this.setBounds(0, 0, 1000, 499); this.setIconImage(new ImageIcon("img/ico.png").getImage()); this.setResizable(false); Utilitat.centrarFinestra(this); this.setJMenuBar(afegirMenu()); n = new imatgeinici(this); this.getContentPane().add(n, BorderLayout.CENTER); // this.setBounds(0, 0, 466, 459); Utilitat.centrarFinestra(this); internalActual = n; this.setVisible(true); } private void iniciaColeccions() { if(D==null){ D = new HashMap<Integer, Destinacio>(); } if(Ico==null){ Ico = new HashMap<Integer, Interes>(); } } public Map<Integer, Destinacio> getD() { return D; } public void setD(Map<Integer, Destinacio> d) { D = d;
} public Map<Integer, Interes> getIco() { return Ico; } public void setIco(Map<Integer, Interes> ico) { Ico = ico; } private JMenuBar afegirMenu() { // Menu menuBar = new JMenuBar(); // Gestionar destinacio menu = new JMenu("Gestió Destinacions"); menu.setMnemonic(KeyEvent.VK_D); menu.getAccessibleContext().setAccessibleDescription("menus"); menuBar.add(menu); // Opció menú Destinacions menuItem = new JMenuItem("Taula de Dades", KeyEvent.VK_A); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription("Taula de Dades"); menuItem.addActionListener(this); menuItem.setActionCommand("Taulades"); menu.add(menuItem); // Punts interes menu = new JMenu("Punts Interes"); menu.setMnemonic(KeyEvent.VK_P); menu.getAccessibleContext().setAccessibleDescription("menus"); menuBar.add(menu); // Opció menú punts interes menuItem = new JMenuItem("Taula de Dades", KeyEvent.VK_A); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription("Taula de Dades"); menuItem.addActionListener(this); menuItem.setActionCommand("Taulainte"); menu.add(menuItem); // Punts interes return menuBar;
Vista: Codi de la Classe: public class FinestraTaulaDesti extends JInternalFrame implements ActionListener { DefaultTableModel model = new MeuModel(); JTable taula = new JTable(model); JPanel panellBotons; JScrollPane jsp; JLabel tDesti; JButton alta; JButton baixa; JButton modificar; FinestraPrincipal FinestraPrincipal; JInternalFrame pantalla1; private FinestraAltaDetallDesti fd; private FinestraModificacioDetallDesti fmd; FinestraTaulaDesti fad; MouseEvent punt; Destinacio destinacio; Map<Integer,Destinacio> totesDestinacions; Map<Integer, Destinacio> D = new HashMap<Integer, Destinacio>(); // GridBagLayout gridBagLayout;
public FinestraTaulaDesti(FinestraPrincipal FinestraPrincipal) { super(); fad = this; crearComponents(); this.FinestraPrincipal = FinestraPrincipal; inicialitzacions(); taulaDesti(); // crearComponents(); posicionarComponents(); afegirComponents(); this.getContentPane().setLayout(gridBagLayout); this.setVisible(true); } private void afegirComponents() { this.add(tDesti); this.add(jsp); this.add(panellBotons); } public FinestraPrincipal getFinestraPrincipal() { return FinestraPrincipal; } private void posicionarComponents() { GridBagConstraints res = new GridBagConstraints(); res.insets = new Insets(2, 2, 2, 2); Container c = this.getContentPane(); ((JComponent) c).setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // Destinacions res.gridx = 0;// amplada res.gridy = 0; res.gridwidth = 1; res.gridheight = 1; res.fill = GridBagConstraints.NONE; res.anchor = GridBagConstraints.CENTER; res.weightx = 0; res.weighty = 0; gridBagLayout.setConstraints(tDesti, res); tDesti.setForeground(Color.WHITE); // Taula res.gridx = 0;// amplada res.gridy = 1; res.gridwidth = 1; res.gridheight = 1; res.fill = GridBagConstraints.BOTH; res.anchor = GridBagConstraints.WEST; res.weightx = 1; res.weighty = 1; gridBagLayout.setConstraints(jsp, res); // borrar.setForeground(Color.RED); // Botons res.gridx = 0;// amplada res.gridy = 2; res.gridwidth = 1; res.gridheight = 1;
destinacio.setId(destinacio.getId()); destinacio.setItinerari(destinacio.getItinerari()); // des.setTePlatja(tePlatja()); FinestraPrincipal.getD().put(destinacio.getId(), destinacio); D.put(destinacio.getId(), destinacio); Destinacio desti = D.get(1); System.out.println(D); // this.destinacio = destinacio; // for(Destinacio d: D.values()){ // } // destinacio=des; } //Destinacio desti public void taulaDesti() { Map<Integer,Destinacio> totesDestinacions=FinestraPrincipal.getD(); for(Map.Entry<Integer, Destinacio> entrades: totesDestinacions.entrySet()){ Object[] objFfila = new Object[9]; objFfila[0] = entrades.getValue().getId(); objFfila[1] = entrades.getValue().getContinent(); objFfila[2] = entrades.getValue().getPais(); objFfila[3] = entrades.getValue().getCapital(); objFfila[4] = entrades.getValue().getCiutat(); objFfila[5] = entrades.getValue().getPoblacio(); objFfila[6] = entrades.getValue().getCP(); objFfila[7] = entrades.getValue().tePlatja; objFfila[8] = entrades.getValue().getItinerari(); //FinestraPrincipal.getD().put(desti.getId(), desti); model.addRow(objFfila); } } public void taulaDestiModificacio(Destinacio desti) { Object[] objFfila = new Object[9]; objFfila[0] = desti.getId(); objFfila[1] = desti.getContinent(); objFfila[2] = desti.getPais(); objFfila[3] = desti.getCapital(); objFfila[4] = desti.getCiutat(); objFfila[5] = desti.getPoblacio(); objFfila[6] = desti.getCP(); objFfila[7] = desti.tePlatja; objFfila[8] = desti.getItinerari(); model.addRow(objFfila); } public void setDestinacio(Destinacio destinacio) { this.destinacio = destinacio; } private void esborrarFila() { int files[] = taula.getSelectedRows();
if (files.length > 0) { int i; for (i = files.length - 1; i >= 0; i--) { FinestraPrincipal.getD().remove(taula.getValueAt(files[i], 0)); model.removeRow(files[i]); //borrar col·leccio System.out.println(files[i]); System.out.println(D); } } } public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if (obj instanceof JButton) { JButton boto = (JButton) obj; if (boto.getActionCommand().equals("alta"){ fd = new FinestraAltaDetallDesti(fad); fd.setVisible(true); } if (boto.getActionCommand().equals("baixa")) { esborrarFila(); } if (boto.getActionCommand().equals("modificar")) { int columna = 0; if (taula.getSelectedRow() >= 0 && columna >= 0) { Object valor = model.getValueAt(taula.getSelectedRow(), columna); Integer i = (Integer) valor; Destinacio d = D.get(i); fmd = new FinestraModificacioDetallDesti(fad, d); esborrarFila(); } } } } private void modificarFila() { model.setRowCount(0); } public class MeuModel extends DefaultTableModel { /** Primera columna Boolean, segona Integer i la resta Object */ public Class getColumnClass(int columna) { if (columna == 7) return Boolean.class; return Object.class; } public boolean isCellEditable(int row, int column) { if (column == 3) return true; return false; } } }
ButtonGroup but; JCheckBox siBox; JTextField paisT; JCheckBox noBox; JLabel iti; JTextField titi; JButton destiCrear; JButton bot2; JInternalFrame pantalla1; Destinacio destinacio; JFrame FinestraPrincipal; private FinestraAltaDetallInteres detallI; private FinestraAltaDetallDesti detall; private FinestraTaulaDesti FAltaDesti; GridBagLayout gridBagLayout; Set
p1.setForeground(Color.GRAY); // id res.gridx = 1; res.gridy = 0; res.gridwidth = 1; res.gridheight = 1; res.weightx = 0; res.weighty = 0; res.fill = GridBagConstraints.NONE; res.anchor = GridBagConstraints.WEST; gridBagLayout.setConstraints(p2, res); p2.setForeground(Color.ORANGE); // p2.setForeground(Color.blue); // textID res.gridx = 2; res.gridy = 0; res.gridwidth = 1;// amplada res.gridheight = 1; res.weightx = 0; res.weighty = 0; res.fill = GridBagConstraints.NONE; res.anchor = GridBagConstraints.WEST; gridBagLayout.setConstraints(idT, res); // Continent res.gridx = 1; res.gridy = 1; res.gridwidth = 1; res.gridheight = 1; // res.weightx = 10; // res.weighty = 0; res.fill = GridBagConstraints.NONE; p4.setForeground(Color.WHITE); gridBagLayout.setConstraints(p4, res); // textContinent res.gridx = 2; res.gridy = 1; res.gridwidth = 1;// amplada res.gridheight = 1; // res.weightx = 0; // res.weighty = 0; res.weightx = 0; res.weighty = 0; res.fill = GridBagConstraints.NONE; gridBagLayout.setConstraints(continentT, res); // Pais res.gridx = 1; res.gridy = 2; res.gridwidth = 1; res.gridheight = 1; // res.weightx = 5; // res.weighty = 0; res.fill = GridBagConstraints.NONE; continentTt.setForeground(Color.WHITE); gridBagLayout.setConstraints(continentTt, res); // textPais res.gridx = 2; res.gridy = 2; res.gridwidth = 1; res.gridheight = 1;
res.gridwidth = 1; res.gridheight = 1; res.fill = GridBagConstraints.NONE; p11.setForeground(Color.WHITE); gridBagLayout.setConstraints(p11, res); // cptext res.gridx = 2; res.gridy = 6; res.gridwidth = 1; res.gridheight = 1; res.fill = GridBagConstraints.NONE; gridBagLayout.setConstraints(cpT, res); // teplatja res.gridx = 1; res.gridy = 7; res.weightx = 0; res.weighty = 0; res.gridwidth = 1; res.gridheight = 2; res.fill = GridBagConstraints.NONE; // res.anchor=GridBagConstraints.VERTICAL; p13.setForeground(Color.WHITE); gridBagLayout.setConstraints(p13, res); // site res.gridx = 2; res.gridy = 7; res.gridwidth = 1; res.gridheight = 1; // res.weightx = 10; // res.weighty = 10; res.fill = GridBagConstraints.NONE; siBox.setForeground(Color.WHITE); siBox.setBackground(new Color(0, 0, 130)); gridBagLayout.setConstraints(siBox, res); // note res.gridx = 2; res.gridy = 8; res.gridwidth = 1; res.gridheight = 1; // res.weightx = 0; // res.weighty = 0; res.fill = GridBagConstraints.NONE; noBox.setForeground(Color.WHITE); noBox.setBackground(new Color(0, 0, 130)); gridBagLayout.setConstraints(noBox, res); // itinerari res.gridx = 1; res.gridy = 9; res.gridwidth = 1; res.gridheight = 1; // res.weightx = 0; // res.weighty = 0; res.fill = GridBagConstraints.NONE; iti.setForeground(Color.WHITE); gridBagLayout.setConstraints(iti, res); // Textitinerari res.gridx = 2; res.gridy = 9; res.gridwidth = 1; res.gridheight = 1; // res.weightx = 0;
// res.weighty = 0; res.fill = GridBagConstraints.NONE; gridBagLayout.setConstraints(titi, res); // Crear Desti res.gridx = 1; res.gridy = 10; res.gridwidth = 1; res.gridheight = 1; // res.weightx = 0; // res.weighty = 0; res.fill = GridBagConstraints.NONE; res.anchor = GridBagConstraints.WEST; gridBagLayout.setConstraints(destiCrear, res); } private void crearComponents() { // Container c = this.getContentPane(); // ((JComponent) c).setBorder(BorderFactory.createEmptyBorder(5, 5, 5, // 5)); p1 = new JLabel("ALTA"); p2 = new JLabel("ID"); idT = new JTextField(5); p4 = new JLabel("Continent"); continentT = new JTextField(20); continentTt = new JLabel("Pais"); paisT = new JTextField(20); p5 = new JLabel("Capital"); capitalT = new JTextField(20); p7 = new JLabel("Ciutat"); ciutatT = new JTextField(20); p9 = new JLabel("Poblacio"); poblacioT = new JTextField(20); p11 = new JLabel("CP"); cpT = new JTextField(20); p13 = new JLabel("Te Platja?"); but = new ButtonGroup(); siBox = new JCheckBox("Si"); noBox = new JCheckBox("No"); noBox.setSelected(true); siBox.setActionCommand("Si"); but.add(siBox); but.add(noBox); iti = new JLabel("Itinerari"); titi = new JTextField(20); destiCrear = new JButton("Crear Desti"); destiCrear.setActionCommand("Crear Desti"); destiCrear.addActionListener(this); } private void afegirComponents() { this.add(p1); this.add(p2); this.add(idT); this.add(p4); this.add(p5); this.add(capitalT); this.add(p7); this.add(ciutatT); this.add(p9);