Duvida quanto a armazenamento de dados (sem banco de dados)

1 resposta
W

Tenho uma classe [b]TelaDeposito[\b] e uma TelaSaque que estão ligadas uma terceira chamada UserSettings;

A TelaDeposito aplica um valor que é armazenado em UserSettings.
A TelaSaque retira um menor ou igual do que foi depositado em UserSettings.
Ambos amostram o valor atual da conta corrente;

No entanto, depois de armazenado um valor em UserSettings, quando troco de Frame para a TelaSaque, o valor depositado desaparece.
Simplifiquei o código, já que faz parte de um programa que estou desenvolvendo com muito mais linhas de código.

UserSettings

import javax.swing.*;

public class UserSettings

{
  double credito_disponivel;
  
  public void CreditoDisponivel()
  {
     ; 
    }
    
  public void DepositoCredito(double adição)
  {
      credito_disponivel += adição;
    }
    
    public void DecrescimoCredito(double diminuição)
  {
      credito_disponivel -= diminuição;
    }
    
    public double getCreditoDisponivel()
    {
        return credito_disponivel;
    }
    
 }

TelaDeposito

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane.*;
import java.io.*;
import java.util.*;
import java.text.*;
 
public class TelaDeposito extends JFrame implements ActionListener
{
   
  ImageIcon logo_contacorrente = new ImageIcon("logo_contacorrente.gif");
  JLabel apli_logo = new JLabel(logo_contacorrente);
  JLabel boas_vindas = new JLabel("Olá Breno Leite!");
  JLabel boas_vindas2 = new JLabel("Seja Bem-Vindo ao Banco Real");
  
  TelaSaque telasaque;
  UserSettings settings = new UserSettings();
  
  JTextArea saida_movimentação = new JTextArea();
  JButton saque = new JButton("Saque");
  JButton deposito = new JButton("Depósito");
  JButton carga_celular = new JButton("Carga de Celular");
  JButton emprestimos = new JButton("Cartão de Crédit");
  JButton logoff = new JButton("Logoff");
  JButton menu_principal = new JButton("Menu Principal");
  
  JMenuBar menuBarPrincipal = new JMenuBar();
  JButton Pagamentos = new JButton("Pagamentos");
  JButton ContaCorrente = new JButton("Conta Corrente");
  JButton Investimentos = new JButton("Investimentos");
  JButton Empréstimos = new JButton("Empréstimos");
  JButton cartao = new JButton("Cartão de Crédito");
  
  
  JLabel txt_deposito = new JLabel("Depósito");
  JLabel exib_total_atual = new JLabel("Total Atual: " + settings.getCreditoDisponivel()); 
  JLabel exib_valor_deposito = new JLabel("Valor do Depósito");
  JTextField valor_deposito = new JTextField();
  JButton confirmar = new JButton("Confirmar");
  
  double adição;
  
  
  
 
public TelaDeposito()
{
  setTitle("Banco Real - Conta Corrente");
  setSize(800,650);
  getContentPane().setBackground(new Color(204,204,204));
  setResizable(false);
  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  setLocationRelativeTo(null);
  getContentPane().setLayout(null);
  
  getContentPane().add(apli_logo);
  apli_logo.setBounds(new Rectangle(0,0,800,100));
  
  //------------------- menuBarPrincipal ----------------------------------
  getContentPane().add(menuBarPrincipal);
  menuBarPrincipal.setFont(new Font("Arial", Font.BOLD, 16));
  menuBarPrincipal.setSize(800,50);
  menuBarPrincipal.setBackground(new Color(0,102,102));
  menuBarPrincipal.setForeground(new Color(255,255,255));
  menuBarPrincipal.setBounds(new Rectangle(0,101,800,30));
  
  
  menuBarPrincipal.add(Pagamentos);
  Pagamentos.addActionListener(this);
  Pagamentos.setBackground(new Color(0,102,102));
  Pagamentos.setFont(new Font("Arial", Font.BOLD, 14));
  Pagamentos.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  menuBarPrincipal.add(ContaCorrente);
  ContaCorrente.addActionListener(this);
  ContaCorrente.setBackground(new Color(0,102,102));
  ContaCorrente.setFont(new Font("Arial", Font.BOLD, 14));
  ContaCorrente.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  menuBarPrincipal.add(Investimentos);
  Investimentos.addActionListener(this);
  Investimentos.setBackground(new Color(0,102,102));
  Investimentos.setFont(new Font("Arial", Font.BOLD, 14));
  Investimentos.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  menuBarPrincipal.add(cartao);
  cartao.addActionListener(this);
  cartao.setBackground(new Color(0,102,102));
  cartao.setFont(new Font("Arial", Font.BOLD, 14));
  cartao.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  menuBarPrincipal.add(Empréstimos);
  Empréstimos.addActionListener(this);
  Empréstimos.setBackground(new Color(0,102,102));
  Empréstimos.setFont(new Font("Arial", Font.BOLD, 14));
  Empréstimos.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  //-------------------------------------------------------------------------
  
  
  //--------------------------- Menu Esquerdo -------------------------------
  getContentPane().add(boas_vindas);
  boas_vindas.setFont( new Font ("Arial Black", Font.BOLD, 14));
  boas_vindas.setVisible(true);
  boas_vindas.setForeground(new Color(0,77,74));
  boas_vindas.setBounds(new Rectangle(10,140,150,20));
  
  getContentPane().add(boas_vindas2);
  boas_vindas2.setFont( new Font ("Arial", Font.BOLD, 12));
  boas_vindas2.setVisible(true);
  boas_vindas2.setForeground(new Color(0,77,74));
  boas_vindas2.setSize(250,20);
  boas_vindas2.setBounds(new Rectangle(10,160,250,20));
  
  
  
  getContentPane().add(saque);
  saque.setFont(new Font("Arial", Font.BOLD, 16));
  saque.setSize(200,30);
  saque.addActionListener(this);
  saque.setToolTipText("Manipule seu dinheiro");
  saque.setBackground(new Color(0,77,74));
  saque.setForeground(new Color(255,255,255));
  saque.setBounds(new Rectangle(10,200,200,30));
  
  getContentPane().add(deposito);
  deposito.setFont(new Font("Arial", Font.BOLD, 16));
  deposito.addActionListener(this);
  deposito.setToolTipText("Manipule seu dinheiro");
  deposito.setBackground(new Color(0,77,74));
  deposito.setForeground(new Color(255,255,255));
  deposito.setBounds(new Rectangle(10,250,200,30));
  
 
  
   
  getContentPane().add(menu_principal);
  menu_principal.setFont(new Font("Arial", Font.BOLD, 16));
  menu_principal.addActionListener(this);
  menu_principal.setToolTipText("Voltar ao Menu Principal");
  menu_principal.setBackground(new Color(0,102,102));
  menu_principal.setForeground(new Color(255,255,255));
  menu_principal.setBounds(new Rectangle(10,500,200,30));
  
  getContentPane().add(logoff);
  logoff.setFont(new Font("Arial", Font.BOLD, 16));
  logoff.addActionListener(this);
  logoff.setToolTipText("Voltar ao Menu Principal");
  logoff.setBackground(new Color(0,102,102));
  logoff.setForeground(new Color(255,255,255));
  logoff.setBounds(new Rectangle(10,450,200,30));
  
  //--------------------------------------------------------------------------
  getContentPane().add(menuBarPrincipal);
  menuBarPrincipal.setFont(new Font("Arial", Font.BOLD, 16));
  menuBarPrincipal.setSize(800,50);
  menuBarPrincipal.setBackground(new Color(0,102,102));
  menuBarPrincipal.setForeground(new Color(255,255,255));
  menuBarPrincipal.setBounds(new Rectangle(0,101,800,30));
  
  //--------------------- Campos para Depósito ---------------------------
  getContentPane().add(txt_deposito);
  txt_deposito.setFont( new Font ("Arial Black", Font.BOLD, 20));
  txt_deposito.setSize(200,30);
  txt_deposito.setForeground(new Color(102,102,102));
  txt_deposito.setVisible(true);
  txt_deposito.setBounds(new Rectangle(350,140,200,30));
  
  getContentPane().add(exib_total_atual);
  exib_total_atual.setFont( new Font ("Arial", Font.PLAIN, 16));
  exib_total_atual.setSize(300,30);
  exib_total_atual.setForeground(new Color(102,102,102));
  exib_total_atual.setVisible(true);
  exib_total_atual.setBounds(new Rectangle(350,190,300,30));
  
  getContentPane().add(exib_valor_deposito);
  exib_valor_deposito.setFont( new Font ("Arial", Font.PLAIN, 16));
  exib_valor_deposito.setSize(300,20);
  exib_valor_deposito.setForeground(new Color(102,102,102));
  exib_valor_deposito.setVisible(true);
  exib_valor_deposito.setBounds(new Rectangle(350,240,300,30));
  
  getContentPane().add(valor_deposito);
  valor_deposito.setSize(200,25);
  valor_deposito.setFont( new Font ("Verdana", Font.PLAIN, 15));
  valor_deposito.setForeground(new Color(102,102,102));
  valor_deposito.setVisible(true);
  valor_deposito.setBounds(new Rectangle(500,240,200,25));
  
  getContentPane().add(confirmar);
  confirmar.setFont(new Font("Arial", Font.BOLD, 16));
  confirmar.setSize(160,25);
  confirmar.addActionListener(this);
  confirmar.setToolTipText("Confirmar Depósito");
  confirmar.setBackground(new Color(1,123,103));
  confirmar.setForeground(new Color(255,255,255));
  confirmar.setBounds(new Rectangle(540,300,160,25));
  }

public static void main (String args[])
{
  JFrame TelaDeposito = new TelaDeposito();
  TelaDeposito.setVisible(true);
  }
  
public void actionPerformed(ActionEvent arg0)
{
    if(arg0.getSource()==saque)
     {
         telasaque = new TelaSaque();
         telasaque.setVisible(true);
         setVisible(false);
      }
     
     if(arg0.getSource()==confirmar)
     {
         adição = Double.parseDouble(valor_deposito.getText());
         settings.DepositoCredito(adição);
         valor_deposito.setText("");
         exib_total_atual.setText("Total Atual: " + settings.getCreditoDisponivel());
         
         
     }
 
     
     
}
  }

TelaSaque

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane.*;
import java.io.*;
import java.util.*;
import java.text.*;
 
public class TelaSaque extends JFrame implements ActionListener
{
   
  ImageIcon logo_contacorrente = new ImageIcon("logo_contacorrente.gif");
  JLabel apli_logo = new JLabel(logo_contacorrente);
  JLabel boas_vindas = new JLabel("Olá Breno Leite!");
  JLabel boas_vindas2 = new JLabel("Seja Bem-Vindo ao Banco Real");
  
  TelaDeposito teladeposito;
  
  JTextArea saida_movimentação = new JTextArea();
  JButton saque = new JButton("Saque");
  JButton deposito = new JButton("Depósito");
  JButton carga_celular = new JButton("Carga de Celular");
  JButton emprestimos = new JButton("Cartão de Crédit");
  JButton logoff = new JButton("Logoff");
  JButton menu_principal = new JButton("Menu Principal");
  
  JMenuBar menuBarPrincipal = new JMenuBar();
  
  JButton Pagamentos = new JButton("Pagamentos");
  JButton ContaCorrente = new JButton("Conta Corrente");
  JButton Investimentos = new JButton("Investimentos");
  JButton Empréstimos = new JButton("Empréstimos");
  JButton cartao = new JButton("Cartão de Crédito");
  
  UserSettings settings = new UserSettings();
  JLabel txt_saque = new JLabel("Saque");
  JLabel exib_total_disp = new JLabel("Total Disponível: " + settings.getCreditoDisponivel());
  JLabel exib_valor_saque = new JLabel("Valor do Saque: ");
  JTextField valor_saque = new JTextField();
  JButton confirmar = new JButton("Confirmar");
  
  
 public TelaSaque()
{
  setTitle("Banco Real - Conta Corrente");
  setSize(800,650);
  getContentPane().setBackground(new Color(204,204,204));
  setResizable(false);

  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  setLocationRelativeTo(null);
  getContentPane().setLayout(null);
  
  getContentPane().add(apli_logo);
  apli_logo.setBounds(new Rectangle(0,0,800,100));
  
  //------------------- menuBarPrincipal ----------------------------------
  getContentPane().add(menuBarPrincipal);
  menuBarPrincipal.setFont(new Font("Arial", Font.BOLD, 16));
  menuBarPrincipal.setSize(800,50);
  menuBarPrincipal.setBackground(new Color(0,102,102));
  menuBarPrincipal.setForeground(new Color(255,255,255));
  menuBarPrincipal.setBounds(new Rectangle(0,101,800,30));
  
  
  menuBarPrincipal.add(Pagamentos);
  Pagamentos.addActionListener(this);
  Pagamentos.setBackground(new Color(0,102,102));
  Pagamentos.setFont(new Font("Arial", Font.BOLD, 14));
  Pagamentos.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  menuBarPrincipal.add(ContaCorrente);
  ContaCorrente.addActionListener(this);
  ContaCorrente.setBackground(new Color(0,102,102));
  ContaCorrente.setFont(new Font("Arial", Font.BOLD, 14));
  ContaCorrente.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  menuBarPrincipal.add(Investimentos);
  Investimentos.addActionListener(this);
  Investimentos.setBackground(new Color(0,102,102));
  Investimentos.setFont(new Font("Arial", Font.BOLD, 14));
  Investimentos.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  menuBarPrincipal.add(cartao);
  cartao.addActionListener(this);
  cartao.setBackground(new Color(0,102,102));
  cartao.setFont(new Font("Arial", Font.BOLD, 14));
  cartao.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  menuBarPrincipal.add(Empréstimos);
  Empréstimos.addActionListener(this);
  Empréstimos.setBackground(new Color(0,102,102));
  Empréstimos.setFont(new Font("Arial", Font.BOLD, 14));
  Empréstimos.setForeground(new Color(253,186,57));
  menuBarPrincipal.setBounds(new Rectangle(10,200,350,150));
  
  //-------------------------------------------------------------------------
  
  
  //--------------------------- Menu Esquerdo -------------------------------
  getContentPane().add(boas_vindas);
  boas_vindas.setFont( new Font ("Arial Black", Font.BOLD, 14));
  boas_vindas.setVisible(true);
  boas_vindas.setForeground(new Color(0,77,74));
  boas_vindas.setBounds(new Rectangle(10,140,150,20));
  
  getContentPane().add(boas_vindas2);
  boas_vindas2.setFont( new Font ("Arial", Font.BOLD, 12));
  boas_vindas2.setVisible(true);
  boas_vindas2.setForeground(new Color(0,77,74));
  boas_vindas2.setSize(250,20);
  boas_vindas2.setBounds(new Rectangle(10,160,250,20));
  
  
  
  getContentPane().add(saque);
  saque.setFont(new Font("Arial", Font.BOLD, 16));
  saque.setSize(200,30);
  saque.addActionListener(this);
  saque.setToolTipText("Manipule seu dinheiro");
  saque.setBackground(new Color(0,77,74));
  saque.setForeground(new Color(255,255,255));
  saque.setBounds(new Rectangle(10,200,200,30));
  
  getContentPane().add(deposito);
  deposito.setFont(new Font("Arial", Font.BOLD, 16));
  deposito.addActionListener(this);
  deposito.setToolTipText("Manipule seu dinheiro");
  deposito.setBackground(new Color(0,77,74));
  deposito.setForeground(new Color(255,255,255));
  deposito.setBounds(new Rectangle(10,250,200,30));
  
 
  
   
  getContentPane().add(menu_principal);
  menu_principal.setFont(new Font("Arial", Font.BOLD, 16));
  menu_principal.addActionListener(this);
  menu_principal.setToolTipText("Voltar ao Menu Principal");
  menu_principal.setBackground(new Color(0,102,102));
  menu_principal.setForeground(new Color(255,255,255));
  menu_principal.setBounds(new Rectangle(10,500,200,30));
  
  getContentPane().add(logoff);
  logoff.setFont(new Font("Arial", Font.BOLD, 16));
  logoff.addActionListener(this);
  logoff.setToolTipText("Voltar ao Menu Principal");
  logoff.setBackground(new Color(0,102,102));
  logoff.setForeground(new Color(255,255,255));
  logoff.setBounds(new Rectangle(10,450,200,30));
  
  //--------------------------------------------------------------------------
  getContentPane().add(menuBarPrincipal);
  menuBarPrincipal.setFont(new Font("Arial", Font.BOLD, 16));
  menuBarPrincipal.setSize(800,50);
  menuBarPrincipal.setBackground(new Color(0,102,102));
  menuBarPrincipal.setForeground(new Color(255,255,255));
  menuBarPrincipal.setBounds(new Rectangle(0,101,800,30));
  
  // -------------------- Campos para Saque ----------------------------------
  getContentPane().add(txt_saque);
  txt_saque.setFont( new Font ("Arial Black", Font.BOLD, 20));
  txt_saque.setSize(200,30);
  txt_saque.setForeground(new Color(102,102,102));
  txt_saque.setVisible(true);
  txt_saque.setBounds(new Rectangle(350,140,200,30));
  
  getContentPane().add(exib_total_disp);
  exib_total_disp.setFont( new Font ("Arial", Font.PLAIN, 16));
  exib_total_disp.setSize(300,30);
  exib_total_disp.setForeground(new Color(102,102,102));
  exib_total_disp.setVisible(true);
  exib_total_disp.setBounds(new Rectangle(350,190,300,30));
  
  getContentPane().add(exib_valor_saque);
  exib_valor_saque.setFont( new Font ("Arial", Font.PLAIN, 16));
  exib_valor_saque.setSize(300,20);
  exib_valor_saque.setForeground(new Color(102,102,102));
  exib_valor_saque.setVisible(true);
  exib_valor_saque.setBounds(new Rectangle(350,240,300,30));
  
  getContentPane().add(valor_saque);
  valor_saque.setSize(200,25);
  valor_saque.setFont( new Font ("Verdana", Font.PLAIN, 15));
  valor_saque.setForeground(new Color(102,102,102));
  valor_saque.setVisible(true);
  valor_saque.setBounds(new Rectangle(500,240,200,25));
  
  getContentPane().add(confirmar);
  confirmar.setFont(new Font("Arial", Font.BOLD, 16));
  confirmar.setSize(160,25);
  confirmar.addActionListener(this);
  confirmar.setToolTipText("Confirmar Depósito");
  confirmar.setBackground(new Color(1,123,103));
  confirmar.setForeground(new Color(255,255,255));
  confirmar.setBounds(new Rectangle(540,300,160,25));
  }

public static void main (String args[])
{
  JFrame TelaSaque = new TelaSaque();
  TelaSaque.setVisible(true);
  }
  
public void actionPerformed(ActionEvent arg0)
{
  
     if(arg0.getSource()==deposito)
     {
         teladeposito = new TelaDeposito();
         teladeposito.setVisible(true);
         setVisible(false);
     }
 
}
  }

Assim que terminar o programa por inteiro, postarei no PortalJava.com
para servir como objeto de estudo para iniciantes.

Agradeço desde já a atenção.

[/b]

1 Resposta

W

Esse meu exemplo de cima está em frames divididos. Seria melhor eu coloca-los para JDesktopFrame?

Criado 23 de junho de 2006
Ultima resposta 25 de jun. de 2006
Respostas 1
Participantes 1