Olá tenho um problema, ja tentei resolve-lo varias vezes e sem sucesso, agora eu queria perguntar como faço para “ligar” uma classe a outra, abaixo vai o codigo:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class PanelInicial extends JPanel implements ActionListener{
	
	JButton novo;
	JButton load;
	Cofrinho c;
	JFileChooser fc;
	String nome;
	String senha;
	PanelLogin jl;
	
	
	public PanelInicial(){
		
		super(new BorderLayout());
		fc = new JFileChooser();
		
        novo = new JButton("Criar nova conta");
        novo.addActionListener(this);
        
        load = new JButton("Carregar conta existente");
        load.addActionListener(this);
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(novo);
        buttonPanel.add(load);
        
        add(buttonPanel, BorderLayout.PAGE_START);
	}
       
	    private static Cofrinho leObjeto (File f){	
	    	Cofrinho o = null;
			try	{
				FileInputStream fos = new FileInputStream (f);
				ObjectInputStream os = new ObjectInputStream (fos);
				o = (Cofrinho) os.readObject ();
				os.close ();
			}catch (IOException e) {
				System.out.println ("Erro ao abrir arquivo.");
			}
			catch (ClassNotFoundException ce) {
				System.out.println ("Objeto não encontrado.");
			}
			return o;
		}
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == novo){
			Cofrinho c = new Cofrinho();
			jl = new PanelLogin(c);
			jl.setCofrinho(c);
			jl.setVisible(true);
	                
		}
		else if (e.getSource() == load) {
           int returnVal = fc.showOpenDialog(PanelInicial.this);
           if (returnVal == JFileChooser.APPROVE_OPTION) {
               File file = fc.getSelectedFile();
               String doFile = file.toString();
               String extensao = doFile.substring(doFile.lastIndexOf("."), doFile.length()); // Verifica se o arquivo é um .cof, se sim ele executa normal, se não ele apresenta uma msg falando sobre isso.
               if(extensao.equals(".cof")){
               c = leObjeto(file);
               jl = new PanelLogin();
               jl.setCofrinho(c);
               this.setVisible(false);
               }
               else{
            	   JOptionPane.showMessageDialog(null, "Tipo de arquivo não aceito!"," FileException!", JOptionPane.ERROR_MESSAGE);
               }
           }
       }
	}
	
	   private void createAndShowGUI() {
	        //Create and set up the window.
	        JFrame frame = new JFrame("Acompanhamento de conta");
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        //Add content to the window.
	        frame.add(new PanelInicial());
	        //Display the window.
	        frame.pack();
	        frame.setVisible(true);
	    }
	    public static void main(String[] args) {
	    	PanelInicial pi = new PanelInicial();
	    	pi.createAndShowGUI();
	    	
	    }
}A outra classe:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class PanelLogin extends JPanel implements ActionListener{
	
	JPasswordField pf;
	JTextField tf;
	JButton ok;
	String nome;
	String senha;
	protected static Cofrinho aux;
	String s;
	boolean statusNome;
	boolean statusSenha;
	PanelPrincipal pp;
	
	public void setCofrinho(Cofrinho c){
		aux = c;
	}
	public PanelLogin(){
		
		tf = new JTextField("Digite o seu login");   
        tf.addActionListener(this);
		
		pf = new JPasswordField("Digite o sua senha");     
        pf.addActionListener(this);
        
        ok = new JButton("Seguir");      
        ok.addActionListener(this);
        
        JPanel buttonPanel = new JPanel(); //use FlowLayout
	        buttonPanel.add(tf);
	        buttonPanel.add(pf);
	        buttonPanel.add(ok);
	        
	        add(buttonPanel, BorderLayout.PAGE_START);
	}
	public PanelLogin(Cofrinho c) {
		aux = c;
		tf = new JTextField("Digite o seu login");   
        tf.addActionListener(this);
		
		pf = new JPasswordField("Digite o sua senha");     
        pf.addActionListener(this);
        
        ok = new JButton("Seguir");      
        ok.addActionListener(this);
        
        JPanel buttonPanel = new JPanel(); //use FlowLayout
	        buttonPanel.add(tf);
	        buttonPanel.add(pf);
	        buttonPanel.add(ok);
	        
	        add(buttonPanel, BorderLayout.PAGE_START);
	}
	public void actionPerformed(ActionEvent e) {
		
		if(aux.getNomeUsuario() != null && aux.getSenhaUsuario() != null){
			
		if(e.getSource() == tf){
			s = new String(tf.getSelectedText());
			
			if(s.equals(aux.getNomeAdm()) || s.equals(aux.getNomeUsuario()))
				statusNome = true;
			else{
				statusNome = false;
			JOptionPane.showMessageDialog(null, "A nome digitado esta incorreto."," Fail", JOptionPane.ERROR_MESSAGE);
			}
			}
		else if( e.getSource() == pf){
				s = new String(pf.getPassword());
				if(s.equals(aux.getSenhaAdm()) || s.equals(aux.getSenhaUsuario()))
					statusSenha = true;
				else{
					statusSenha = false;
				JOptionPane.showMessageDialog(null, "A senha digitada esta incorreta."," Fail", JOptionPane.ERROR_MESSAGE);
		}}
		
		else if(e.getSource() == ok){
			if(statusNome == true && statusSenha == true){
				pp = new PanelPrincipal();
				pp.setCofrinho(aux);
				this.setVisible(false); // Faz "desaparecer" essa janela
			}
				
		}
		}else{
			if(e.getSource() == tf){
				s = new String(tf.getSelectedText());
				aux.setNomeUsuario(s);
				statusNome = true;
				}
			else if( e.getSource() == pf){
					s = new String(pf.getSelectedText());
					aux.setSenhaUsuario(s);
					statusSenha = true;
			}
			
			else if(e.getSource() == ok){
				if(statusNome == true && statusSenha == true){
					pp = new PanelPrincipal();
					pp.setCofrinho(aux);
					this.setVisible(false); // faz "desaparecer" essa janela
				}
		}
		}
	}    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Login");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Add content to the window.
        frame.add(new PanelLogin());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
    	PanelLogin pl = new PanelLogin();
    	pl.createAndShowGUI();
    }
}
Mais uma:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class PanelPrincipal extends JPanel implements ActionListener{
	JButton verSaldo;
	JButton sac;
	JButton dep;
	JButton save;
	JButton historico;
	JFileChooser fc;
	Cofrinho aux;
	PanelHistorico ph;
	
	public void setCofrinho(Cofrinho c){
		aux = c;
	}
	
	public PanelPrincipal(){
		super(new BorderLayout());
		
		fc = new JFileChooser();
	
        verSaldo = new JButton("Ver saldo");
        verSaldo.addActionListener(this);   
        
        sac = new JButton("Sacar");
        sac.addActionListener(this);
        
        dep = new JButton("Depositar");
        dep.addActionListener(this);
         
        save = new JButton("Salvar");
        save.addActionListener(this);
        
        historico = new JButton("Ver historico");
        historico.addActionListener(this);
        
        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(save);
        buttonPanel.add(verSaldo);
        buttonPanel.add(sac);
        buttonPanel.add(dep);
        buttonPanel.add(historico);
        
        add(buttonPanel, BorderLayout.PAGE_START);
	}
	
    private static void gravaObjeto (File f, Cofrinho o){
        try	{
            FileOutputStream fos = new FileOutputStream (f);
            ObjectOutputStream os = new ObjectOutputStream (fos);
            os.writeObject (o);
            os.close ();
        }catch (IOException e)  {
                System.out.println ("Erro ao gravar objeto.");
        }
    }
	
	public void actionPerformed(ActionEvent e){
	     
		   if(e.getSource() == verSaldo){   
			   JOptionPane.showMessageDialog(null, "Seu saldo é R$ "+aux.getSaldo(), "saldo", JOptionPane.INFORMATION_MESSAGE);   
		   }       
		   if(e.getSource() == sac){  
			   double j = Double.parseDouble(JOptionPane.showInputDialog("Digite a quantia que deseja sacar"));
			   try{
				   aux.sacar(j);
		   }catch(IllegalArgumentException ep){
			   JOptionPane.showMessageDialog(null, ""+ep.toString(),"", JOptionPane.INFORMATION_MESSAGE);
		   }
		  }
		   if(e.getSource() == dep){
			   double j = Double.parseDouble(JOptionPane.showInputDialog("Digite a quantia que deseja depositar"));
		   try {
			aux.depositar(j);
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		}
		   }
		   if(e.getSource() == historico){
			  ph.setCofrinho(aux);
			  ph = new PanelHistorico();
		   }
		   if(e.getSource() == save){
	            int returnVal = fc.showSaveDialog(PanelPrincipal.this);
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	                File file = fc.getSelectedFile();
	                File f = new File(file+".cof");
	                gravaObjeto(f, aux);
	                
	            }
	        } 
	}
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Tela Principal");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Add content to the window.
        frame.add(new PanelPrincipal());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
    	PanelPrincipal pp = new PanelPrincipal();
    	pp.createAndShowGUI();
    }
}
E por fim:
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class PanelHistorico extends Label implements ActionListener{
	
	Cofrinho aux;
	JTextArea log;
	
	public void setCofrinho(Cofrinho c){
		aux = c;
	}
	
	public PanelHistorico(){
		
    
    aux = new Cofrinho();
    log = new JTextArea(10,20);
    log.setMargin(new Insets(10,10,10,10));
    log.setEditable(false);
    log.setAutoscrolls(true);
    JScrollPane logScrollPane = new JScrollPane(log);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == log){
			int a = aux.getLast();
			for(int i = 0; i < a; i++){
			log.append(aux.getHistorico(i));
			}
			log.setCaretPosition(log.getDocument().getLength());
		}
		
	}
	
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Historico");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Add content to the window.
        frame.add(new PanelHistorico());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                UIManager.put("swing.boldMetal", Boolean.FALSE); 
                createAndShowGUI();
            }
        });
    }
}
E a classe Cofrinho:
mport java.io.Serializable;
import java.util.GregorianCalendar;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Cofrinho implements Serializable{
private double saldo;
private int last = -1;
private Usuarios usuario;
private Administrador adm;
GregorianCalendar d;
private Vector <String> historico = new Vector <String> (10,1);
public double getSaldo() {
	historico.add("Consultou o saldo "+d.getTime());
	last++;
	return saldo;	
}
public Cofrinho(){
	last++;
	historico.add("Segue abaixo o historico. Foi criado: "+d.getTime());
}
public String getNomeUsuario(){
	return usuario.getNome();
}
public String getSenhaUsuario(){
	return usuario.getSenha();
}
public String getNomeAdm(){
	return adm.getNome();
}
public String getSenhaAdm(){
	return adm.getSenha();
}
public void setNomeUsuario(String nome){
	usuario.setNome(nome);
}
public void setSenhaUsuario(String senha){
	usuario.setSenha(senha);
}
public void depositar(double q) throws IllegalArgumentException{
	if(saldo + q < 0 && saldo + q > Double.MAX_VALUE)
		throw new IllegalArgumentException();
	this.saldo = this.saldo + q ;
	historico.add("Depositou "+q+" "+d.getTime());
	last++;
	
	
}
public void sacar(double q) throws IllegalArgumentException{
	if((saldo - q) < 0){
			throw new IllegalArgumentException();
		}
	else
		saldo = saldo - q;
	String aux = JOptionPane.showInputDialog("Digite o motivo pelo qual resolveu sacar");
	
	if(aux == null || aux.length() < 4){
		aux = "Não informado.";
	}
	
	historico.add("Sacou" +q+" "+d.getTime()+" pelo motivo: "+aux);
	last++;
}
public int getLast(){
	return last;
}
public String getHistorico(int index){
	return historico.get(index);
	}
	
}
Como faço para ligar uma classe a outra, quando abre elas separado funciona, só que quando quero que ligue-as isso não aconteçe, e gera um erro sempre, alguem sabe como faço para que funcionem em conjunto ?
 mais não entendo nada de apps , to fazendo esse projeto para aprender um pouco de interface grafica
 mais não entendo nada de apps , to fazendo esse projeto para aprender um pouco de interface grafica