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/ | /VistaControlador/ | ||||||
| /PedirDatos/ | /PedirDatos/ | ||||||
|  | /Logica/ | ||||||
|  | |||||||
| @ -12,21 +12,17 @@ public class Gestion{ | |||||||
| 		Gestion.total=0; | 		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) { | 	public void aniadirGasto(Transaccion transaccion) { | ||||||
| 		this.gestiones.add(transaccion); | 		this.gestiones.add(transaccion); | ||||||
|  | 		this.suma+=transaccion.getDinero(); | ||||||
|  | 		Gestion.total+=transaccion.getDinero(); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	public int getSuma() { | 	public int getSuma() { | ||||||
| 		return this.suma; | 		return this.suma; | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	public int getTotal() { | 	public static int getTotal() { | ||||||
| 		return Gestion.total; | 		return Gestion.total; | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| @ -36,8 +32,15 @@ public class Gestion{ | |||||||
| 	 | 	 | ||||||
| 	public void alterarVisibilidad(boolean visibilidad, int elemento) { | 	public void alterarVisibilidad(boolean visibilidad, int elemento) { | ||||||
| 		if(this.gestiones.get(elemento).alterarVisivilidad(visibilidad)) { | 		if(this.gestiones.get(elemento).alterarVisivilidad(visibilidad)) { | ||||||
| 			this.suma-=this.gestiones.get(elemento).getDinero(); | 			if(this.gestiones.get(elemento).getVisivilidad()) { | ||||||
| 			Gestion.total-=this.gestiones.get(elemento).getDinero(); | 				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.ActionEvent; | ||||||
| import java.awt.event.ActionListener; | 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; | 	private Vista vista; | ||||||
| 	public Controlador(Vista vista) { | 	public Controlador(Vista vista) { | ||||||
| @ -13,28 +20,58 @@ public class Controlador implements ActionListener{ | |||||||
| 	} | 	} | ||||||
| 	@Override | 	@Override | ||||||
| 	public void actionPerformed(ActionEvent e) { | 	public void actionPerformed(ActionEvent e) { | ||||||
| 		String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso"); | 		if(e.getActionCommand().equals(this.vista.boton.getActionCommand())) { | ||||||
| 		if(nombre.equals("")) { | 			String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso"); | ||||||
| 			JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE); | 			if(nombre.equals("")) { | ||||||
| 			return; | 				JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE); | ||||||
| 		} | 				return; | ||||||
| 		String dinero=JOptionPane.showInputDialog("Introduce a cuanto asciende"); | 			} | ||||||
| 		if(dinero.equals("")) { | 			String dinero=JOptionPane.showInputDialog("Introduce a cuanto asciende"); | ||||||
| 			JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE); | 			if(dinero.equals("")) { | ||||||
| 			return; | 				JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE); | ||||||
|  | 				return; | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
|  | 			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(); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		try { |  | ||||||
| 			this.vista.aniadirElemento(nombre, Integer.parseInt(dinero)); |  | ||||||
| 		}catch (NumberFormatException ex) { |  | ||||||
| 			JOptionPane.showMessageDialog(null, "Debe introducir un numero", "error", JOptionPane.WARNING_MESSAGE); |  | ||||||
| 		} |  | ||||||
| 		 | 		 | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
|  | 	@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() { | 	private void aniadirListeners() { | ||||||
| 		vista.boton.addActionListener(this); | 		vista.boton.addActionListener(this); | ||||||
|  | 		for(JCheckBox check:this.vista.transacciones) { | ||||||
|  | 			check.addActionListener(this); | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,27 +1,39 @@ | |||||||
| package VistaControlador; | package VistaControlador; | ||||||
| 
 | 
 | ||||||
| import java.awt.Dimension; | import java.awt.Dimension; | ||||||
|  | import java.awt.GridBagConstraints; | ||||||
|  | import java.awt.GridBagLayout; | ||||||
| 
 | 
 | ||||||
|  | import javax.swing.JButton; | ||||||
| import javax.swing.JFrame; | import javax.swing.JFrame; | ||||||
| import javax.swing.JPanel; | import javax.swing.JPanel; | ||||||
| import javax.swing.JTabbedPane; | import javax.swing.JTabbedPane; | ||||||
|  | import javax.swing.JTextArea; | ||||||
|  | 
 | ||||||
|  | import Logica.Gestion; | ||||||
| 
 | 
 | ||||||
| public class Menu extends JFrame{ | public class Menu extends JFrame{ | ||||||
| 	protected JPanel panelCentral; | 	protected JPanel panelCentral; | ||||||
| 	protected JTabbedPane pestania; | 	protected JTabbedPane pestania; | ||||||
| 	protected Vista ingresos; | 	protected Vista ingresos; | ||||||
| 	protected Vista gastos; | 	protected Vista gastos; | ||||||
| 	 | 	protected JTextArea total; | ||||||
| 	public Menu() { | 	public Menu() { | ||||||
| 		init(); | 		init(); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	private void init() { | 	private void init() { | ||||||
|  | 		this.setLayout(new GridBagLayout()); | ||||||
|  | 		GridBagConstraints constrain=new GridBagConstraints(); | ||||||
| 		this.ingresos=new Vista(this); | 		this.ingresos=new Vista(this); | ||||||
| 		this.gastos=new Vista(this); | 		this.gastos=new Vista(this); | ||||||
| 		this.panelCentral=new JPanel(); | 		this.panelCentral=new JPanel(); | ||||||
| 		this.pestania=new JTabbedPane(); | 		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("Ingresos", ingresos); | ||||||
| 		this.pestania.addTab("Gastos", gastos); | 		this.pestania.addTab("Gastos", gastos); | ||||||
| 		getContentPane().add(pestania); | 		getContentPane().add(pestania); | ||||||
| @ -32,6 +44,13 @@ public class Menu extends JFrame{ | |||||||
| 		setLocationRelativeTo(null); | 		setLocationRelativeTo(null); | ||||||
| 		Controlador controlador=new Controlador(this.ingresos); | 		Controlador controlador=new Controlador(this.ingresos); | ||||||
| 		Controlador controlador2=new Controlador(this.gastos); | 		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; | package VistaControlador; | ||||||
| 
 | 
 | ||||||
| import java.awt.Dimension; | import java.awt.Dimension; | ||||||
|  | import java.util.LinkedList; | ||||||
|  | 
 | ||||||
| import javax.swing.JButton; | import javax.swing.JButton; | ||||||
| import javax.swing.JCheckBox; | import javax.swing.JCheckBox; | ||||||
| import javax.swing.JFrame; | import javax.swing.JFrame; | ||||||
| @ -11,12 +13,15 @@ import Logica.*; | |||||||
| 
 | 
 | ||||||
| public class Vista extends JPanel{ | public class Vista extends JPanel{ | ||||||
| 	private int x,y; | 	private int x,y; | ||||||
| 	private Gestion gestiones; | 	protected Gestion gestiones; | ||||||
| 	protected JButton boton; | 	protected JButton boton; | ||||||
| 	protected JFrame padre; | 	protected LinkedList<JCheckBox> transacciones; | ||||||
| 	JPanel cuadro; | 	JPanel cuadro; | ||||||
| 	JScrollPane panel; | 	JScrollPane panel; | ||||||
| 	public Vista(JFrame padre) { | 	Menu menu; | ||||||
|  | 	public Vista(Menu menu) { | ||||||
|  | 		this.transacciones=new LinkedList<JCheckBox>(); | ||||||
|  | 		this.menu=menu; | ||||||
| 		this.x=100; | 		this.x=100; | ||||||
| 		this.boton=new JButton("aniadir"); | 		this.boton=new JButton("aniadir"); | ||||||
| 		this.gestiones=new Gestion(); | 		this.gestiones=new Gestion(); | ||||||
| @ -30,13 +35,13 @@ public class Vista extends JPanel{ | |||||||
| 		panel.setVisible(true); | 		panel.setVisible(true); | ||||||
| 		this.add(panel); | 		this.add(panel); | ||||||
| 		this.aniadirElemento("asdfa", 0); | 		this.aniadirElemento("asdfa", 0); | ||||||
| 		this.padre=padre; |  | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	public void aniadirElemento(String nombre, int dinero) { | 	public void aniadirElemento(String nombre, int dinero) { | ||||||
| 		Transaccion transaccion=new Transaccion(nombre, dinero); | 		Transaccion transaccion=new Transaccion(nombre, dinero); | ||||||
| 		this.gestiones.aniadirGasto(transaccion); | 		this.gestiones.aniadirGasto(transaccion); | ||||||
| 		JCheckBox check=new JCheckBox(transaccion.toString()); | 		JCheckBox check=new JCheckBox(transaccion.toString()); | ||||||
|  | 		this.transacciones.add(check); | ||||||
| 		this.cuadro.add(check); | 		this.cuadro.add(check); | ||||||
| 		this.y+=28; | 		this.y+=28; | ||||||
| 		//System.out.println(this.y); | 		//System.out.println(this.y); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user