Añadida funcionalidad checkbox
This commit is contained in:
		
							parent
							
								
									a6afa6adb6
								
							
						
					
					
						commit
						ebbbcfcb10
					
				
							
								
								
									
										1
									
								
								bin/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								bin/.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1,2 +1,3 @@ | ||||
| /VistaControlador/ | ||||
| /PedirDatos/ | ||||
| /Logica/ | ||||
|  | ||||
| @ -12,21 +12,17 @@ public class Gestion{ | ||||
| 		Gestion.total=0; | ||||
| 	} | ||||
| 	 | ||||
| 	public void aniadirGasto(String nombre, int dinero) { | ||||
| 		this.gestiones.add(new Transaccion(nombre, dinero)); | ||||
| 		this.suma+=dinero; | ||||
| 		Gestion.total+=total; | ||||
| 	} | ||||
| 	 | ||||
| 	public void aniadirGasto(Transaccion transaccion) { | ||||
| 		this.gestiones.add(transaccion); | ||||
| 		this.suma+=transaccion.getDinero(); | ||||
| 		Gestion.total+=transaccion.getDinero(); | ||||
| 	} | ||||
| 	 | ||||
| 	public int getSuma() { | ||||
| 		return this.suma; | ||||
| 	} | ||||
| 	 | ||||
| 	public int getTotal() { | ||||
| 	public static int getTotal() { | ||||
| 		return Gestion.total; | ||||
| 	} | ||||
| 	 | ||||
| @ -36,8 +32,15 @@ public class Gestion{ | ||||
| 	 | ||||
| 	public void alterarVisibilidad(boolean visibilidad, int elemento) { | ||||
| 		if(this.gestiones.get(elemento).alterarVisivilidad(visibilidad)) { | ||||
| 			if(this.gestiones.get(elemento).getVisivilidad()) { | ||||
| 				this.suma+=this.gestiones.get(elemento).getDinero(); | ||||
| 				Gestion.total+=this.gestiones.get(elemento).getDinero(); | ||||
| 			}else { | ||||
| 				this.suma-=this.gestiones.get(elemento).getDinero(); | ||||
| 				Gestion.total-=this.gestiones.get(elemento).getDinero(); | ||||
| 			} | ||||
| 			 | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -2,9 +2,16 @@ package VistaControlador; | ||||
| 
 | ||||
| import java.awt.event.ActionEvent; | ||||
| import java.awt.event.ActionListener; | ||||
| import javax.swing.JOptionPane; | ||||
| 
 | ||||
| public class Controlador implements ActionListener{ | ||||
| import javax.swing.JCheckBox; | ||||
| import javax.swing.JOptionPane; | ||||
| import javax.swing.event.ChangeEvent; | ||||
| import javax.swing.event.ChangeListener; | ||||
| 
 | ||||
| import Logica.Gestion; | ||||
| import Logica.Transaccion; | ||||
| 
 | ||||
| public class Controlador implements ActionListener,ChangeListener{ | ||||
| 
 | ||||
| 	private Vista vista; | ||||
| 	public Controlador(Vista vista) { | ||||
| @ -13,6 +20,7 @@ public class Controlador implements ActionListener{ | ||||
| 	} | ||||
| 	@Override | ||||
| 	public void actionPerformed(ActionEvent e) { | ||||
| 		if(e.getActionCommand().equals(this.vista.boton.getActionCommand())) { | ||||
| 			String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso"); | ||||
| 			if(nombre.equals("")) { | ||||
| 				JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE); | ||||
| @ -26,15 +34,44 @@ public class Controlador implements ActionListener{ | ||||
| 			 | ||||
| 			try { | ||||
| 				this.vista.aniadirElemento(nombre, Integer.parseInt(dinero)); | ||||
| 				this.vista.menu.total.setText(String.valueOf(Gestion.getTotal())); | ||||
| 				this.vista.menu.total.revalidate(); | ||||
| 				this.vista.menu.total.repaint(); | ||||
| 				this.aniadirListeners(); | ||||
| 			}catch (NumberFormatException ex) { | ||||
| 				JOptionPane.showMessageDialog(null, "Debe introducir un numero", "error", JOptionPane.WARNING_MESSAGE); | ||||
| 			} | ||||
| 		}else { | ||||
| 			JCheckBox pulsado=(JCheckBox)e.getSource(); | ||||
| 			for(int i=0;i<vista.gestiones.getElementos().size();i++) { | ||||
| 				if(vista.gestiones.getElementos().get(i).toString().equals(pulsado.getText())) { | ||||
| 					vista.gestiones.alterarVisibilidad(!vista.gestiones.getElementos().get(i).getVisivilidad(), i); | ||||
| 					this.vista.menu.total.setText(String.valueOf(Gestion.getTotal())); | ||||
| 					this.vista.menu.total.revalidate(); | ||||
| 					this.vista.menu.total.repaint(); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		 | ||||
| 		 | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void stateChanged(ChangeEvent e) { | ||||
| 		// TODO Auto-generated method stub | ||||
| 		JCheckBox pulsado=(JCheckBox)e.getSource(); | ||||
| 		for(Transaccion transaaccio:vista.gestiones.getElementos()) { | ||||
| 			if(transaaccio.toString().equals(pulsado.getText())) { | ||||
| 				transaaccio.alterarVisivilidad(false); | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	private void aniadirListeners() { | ||||
| 		vista.boton.addActionListener(this); | ||||
| 		for(JCheckBox check:this.vista.transacciones) { | ||||
| 			check.addActionListener(this); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| } | ||||
|  | ||||
| @ -1,27 +1,39 @@ | ||||
| package VistaControlador; | ||||
| 
 | ||||
| import java.awt.Dimension; | ||||
| import java.awt.GridBagConstraints; | ||||
| import java.awt.GridBagLayout; | ||||
| 
 | ||||
| import javax.swing.JButton; | ||||
| import javax.swing.JFrame; | ||||
| import javax.swing.JPanel; | ||||
| import javax.swing.JTabbedPane; | ||||
| import javax.swing.JTextArea; | ||||
| 
 | ||||
| import Logica.Gestion; | ||||
| 
 | ||||
| public class Menu extends JFrame{ | ||||
| 	protected JPanel panelCentral; | ||||
| 	protected JTabbedPane pestania; | ||||
| 	protected Vista ingresos; | ||||
| 	protected Vista gastos; | ||||
| 	 | ||||
| 	protected JTextArea total; | ||||
| 	public Menu() { | ||||
| 		init(); | ||||
| 	} | ||||
| 	 | ||||
| 	private void init() { | ||||
| 		this.setLayout(new GridBagLayout()); | ||||
| 		GridBagConstraints constrain=new GridBagConstraints(); | ||||
| 		this.ingresos=new Vista(this); | ||||
| 		this.gastos=new Vista(this); | ||||
| 		this.panelCentral=new JPanel(); | ||||
| 		this.pestania=new JTabbedPane(); | ||||
| 		this.panelCentral.add(pestania); | ||||
| 		constrain.fill=GridBagConstraints.VERTICAL; | ||||
| 		constrain.gridx=0; | ||||
| 		constrain.gridy=0; | ||||
| 		constrain.weightx=2; | ||||
| 		this.panelCentral.add(pestania,constrain); | ||||
| 		this.pestania.addTab("Ingresos", ingresos); | ||||
| 		this.pestania.addTab("Gastos", gastos); | ||||
| 		getContentPane().add(pestania); | ||||
| @ -32,6 +44,13 @@ public class Menu extends JFrame{ | ||||
| 		setLocationRelativeTo(null); | ||||
| 		Controlador controlador=new Controlador(this.ingresos); | ||||
| 		Controlador controlador2=new Controlador(this.gastos); | ||||
| 		constrain.fill=GridBagConstraints.HORIZONTAL; | ||||
| 		constrain.gridx=1; | ||||
| 		constrain.gridy=0; | ||||
| 		constrain.weightx=1; | ||||
| 		constrain.weighty=2; | ||||
| 		this.total=new JTextArea(String.valueOf(Gestion.getTotal())); | ||||
| 		this.add(this.total,constrain); | ||||
| 	} | ||||
| 	 | ||||
| } | ||||
|  | ||||
| @ -1,6 +1,8 @@ | ||||
| package VistaControlador; | ||||
| 
 | ||||
| import java.awt.Dimension; | ||||
| import java.util.LinkedList; | ||||
| 
 | ||||
| import javax.swing.JButton; | ||||
| import javax.swing.JCheckBox; | ||||
| import javax.swing.JFrame; | ||||
| @ -11,12 +13,15 @@ import Logica.*; | ||||
| 
 | ||||
| public class Vista extends JPanel{ | ||||
| 	private int x,y; | ||||
| 	private Gestion gestiones; | ||||
| 	protected Gestion gestiones; | ||||
| 	protected JButton boton; | ||||
| 	protected JFrame padre; | ||||
| 	protected LinkedList<JCheckBox> transacciones; | ||||
| 	JPanel cuadro; | ||||
| 	JScrollPane panel; | ||||
| 	public Vista(JFrame padre) { | ||||
| 	Menu menu; | ||||
| 	public Vista(Menu menu) { | ||||
| 		this.transacciones=new LinkedList<JCheckBox>(); | ||||
| 		this.menu=menu; | ||||
| 		this.x=100; | ||||
| 		this.boton=new JButton("aniadir"); | ||||
| 		this.gestiones=new Gestion(); | ||||
| @ -30,13 +35,13 @@ public class Vista extends JPanel{ | ||||
| 		panel.setVisible(true); | ||||
| 		this.add(panel); | ||||
| 		this.aniadirElemento("asdfa", 0); | ||||
| 		this.padre=padre; | ||||
| 	} | ||||
| 	 | ||||
| 	public void aniadirElemento(String nombre, int dinero) { | ||||
| 		Transaccion transaccion=new Transaccion(nombre, dinero); | ||||
| 		this.gestiones.aniadirGasto(transaccion); | ||||
| 		JCheckBox check=new JCheckBox(transaccion.toString()); | ||||
| 		this.transacciones.add(check); | ||||
| 		this.cuadro.add(check); | ||||
| 		this.y+=28; | ||||
| 		//System.out.println(this.y); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user