从jframe接收数据时出现问题

时间:2020-05-01 15:48:09

标签: java

我一直在尝试将值添加到来自单独jFrame的类中的列表中。我进行了测试,知道该信息可以正确发送,并且接收该信息的功能也可以正确接收它。问题是我不确定是否将信息添加到列表中,因为当我尝试打印信息时,什么也没发生。


import java.util.ArrayList;

public class Conta {
    private int NumConta;
    private String Nome;
    private String RG;
    private String CPF;
    private double Saldo;
    private String Password;
    ArrayList<Conta> ListaUser;


    public Conta(){
        ListaUser = new ArrayList();
    }

    public Conta(int NumConta, String Nome, String RG, String CPF, double Saldo, String Password) {
        this.NumConta = NumConta;
        this.Nome = Nome;
        this.RG = RG;
        this.CPF = CPF;
        this.Saldo = Saldo;
        this.Password = Password;
        ListaUser = new ArrayList();
    }

    public int getNumConta() {
        return NumConta;
    }

    public void setNumConta(int NumConta) {
        this.NumConta = NumConta;
    }

    public String getNome() {
        return Nome;
    }

    public void setNome(String Nome) {
        this.Nome = Nome;
    }

    public String getRG() {
        return RG;
    }

    public void setRG(String RG) {
        this.RG = RG;
    }

    public String getCPF() {
        return CPF;
    }

    public void setCPF(String CPF) {
        this.CPF = CPF;
    }

    public double getSaldo() {
        return Saldo;
    }

    public void setSaldo(double Saldo) {
        this.Saldo = Saldo;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String Password) {
        this.Password = Password;
    }

    public ArrayList<Conta> getListaUser() {
        return ListaUser;
    }

    public void setListaUser(ArrayList<Conta> ListaUser) {
        this.ListaUser = ListaUser;
    }
    public void AddFunc(Conta C){
        ListaUser.add(C);
    }

}

这是接收信息的班级


import java.util.Random;
import javax.swing.JOptionPane;

public class Cadastrar extends javax.swing.JFrame {
    public Cadastrar() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanelinicio = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldnomecadastro = new javax.swing.JTextField();
        jTextFieldRG = new javax.swing.JTextField();
        jTextFieldCPF = new javax.swing.JTextField();
        jTextFieldpasswordcadastro = new javax.swing.JTextField();
        jButtonconfirmarcadastro = new javax.swing.JButton();
        jButtonvoltarCadastrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanelinicio.setBackground(new java.awt.Color(102, 153, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("RG:");

        jLabel2.setText("CPF:");

        jLabel3.setText("Nome:");

        jLabel4.setText("Password:");

        jButtonconfirmarcadastro.setText("Confirmar");
        jButtonconfirmarcadastro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonconfirmarcadastroActionPerformed(evt);
            }
        });

        jButtonvoltarCadastrar.setText("Voltar");
        jButtonvoltarCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonvoltarCadastrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addGap(58, 58, 58)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldpasswordcadastro, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldRG, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldnomecadastro, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(77, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButtonconfirmarcadastro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonvoltarCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(173, 173, 173))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldnomecadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldRG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldpasswordcadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(68, 68, 68)
                .addComponent(jButtonconfirmarcadastro)
                .addGap(18, 18, 18)
                .addComponent(jButtonvoltarCadastrar)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanelinicioLayout = new javax.swing.GroupLayout(jPanelinicio);
        jPanelinicio.setLayout(jPanelinicioLayout);
        jPanelinicioLayout.setHorizontalGroup(
            jPanelinicioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanelinicioLayout.setVerticalGroup(
            jPanelinicioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelinicioLayout.createSequentialGroup()
                .addGap(0, 117, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelinicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelinicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void jButtonvoltarCadastrarActionPerformed(java.awt.event.ActionEvent evt) {                                                       
        Principal principal = new Principal();
        principal.setVisible(true);           
        dispose();
    }                                                      

    private void jButtonconfirmarcadastroActionPerformed(java.awt.event.ActionEvent evt) {                                                         
        Random random = new Random();
        int numero = random.nextInt(9999999);
        Conta C = new Conta(numero,jTextFieldnomecadastro.getText(),jTextFieldRG.getText(),jTextFieldCPF.getText(),0,jTextFieldpasswordcadastro.getText());
        C.AddFunc(C);
        JOptionPane.showMessageDialog(this, "Conta criada com sucesso!\n"+"Numero da conta: "+numero);
        Principal p = new Principal();
        p.setVisible(true);
        dispose();
    }                                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Cadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Cadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Cadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Cadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Cadastrar().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonconfirmarcadastro;
    private javax.swing.JButton jButtonvoltarCadastrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelinicio;
    private javax.swing.JTextField jTextFieldCPF;
    private javax.swing.JTextField jTextFieldRG;
    private javax.swing.JTextField jTextFieldnomecadastro;
    private javax.swing.JTextField jTextFieldpasswordcadastro;
    // End of variables declaration                   
}

此jFrame发送信息。

如何使列表接收信息,以及如何查看所有添加的信息?

预先感谢

1 个答案:

答案 0 :(得分:1)

每个Conta都有它自己的ListaUser副本,所以当您这样做时

Conta C = new Conta(...);
C.AddFunc(C);

您正在将C添加到 C自己的ListaUser副本中,并且每个Conta C将包含一个列表ListaUser,该列表中只有一个元素; C本身。换句话说,每个Conta都将有一个列表,仅其自身为列表内容。没有包含您创建的Conta的所有所有的列表。

帐户(Conta)不应是维护程序所保留帐户列表的地方。程序本身与帐户有关,因此该程序具有一个帐户列表。

public class Cadastrar extends javax.swing.JFrame {
    private List<Conta> listaUser = new ArrayList<>();
    private Cadastrar program;

    public Cadastrar() {
        initComponents();
        program = this;
    }
    // etc.

以后在jButtonconfirmarcadastroActionPerformed中的程序中创建Conta时,会将其添加到程序列表中

Conta c = new Conta(...);
program.listaUser.add(c);
// or possibly: program.storeConta(c);

在程序的某个点上,您可能希望显示列表,也许是响应另一个按钮,如果您只是在控制台上打印的话,可能看起来像这样(我不会编写代码)在这里显示在JPanel或其他任何东西中

// other methods that are in class Cadastrar
List<Conta> getContaList()
{
    return program.listaUser;
}

void listContas()
{
    for (Conta c : program.getContaList()) {
        System.out.println(c);
    }
}

通常您可以使用this而不是保留单独的变量program

还请注意,我已更改了变量名的大小写……这不是硬性规定,但Java中的约定是,类名应以大写字母开头,例如Conta,而变量名应以小写字母开头,例如{ {1}},而不是listaUser

我已经很长时间没有进行Swing编程了,但是(通常曾经是)将 Application 对象与您的主要ListaUser分开,所以您可能会有这种安排...

JFrame