這是我第一次在這里尋求幫助,對不起,如果我做錯了什么,請告訴我。我的母語是西班牙語,抱歉有些文字是西班牙語。我剛剛開始使用 Java,所以如果您在我的代碼中看到問題(除了我要求的內容),請告訴我,謝謝。
解釋代碼:我有一個表單,它通過“JtextFields”獲取四個字串,并在您按下按鈕(通過 btAgregarRegistro 偵聽器)后將它們放置在來自類“Cliente”的物件“cliente1”的屬性上。然后我將物件添加到 arrayList
//Arraylist declaration
private List<Cliente> arregloClientes = new ArrayList<>();
//Add the object to the ArrayList
arregloClientes.add(cliente1);
然后我更新 Jlist“listClientes”(在函式 actualizarLista() 上),這樣您就可以在添加記錄后在表單上看到它。該更新呼叫類 (Cliente) getDatosClientes 的方法,該方法將所有屬性帶到 DefaultListModel,然后將其顯示在 Jlist 我的問題:當我添加新記錄時,我添加的最后一個,替換整個串列等等。所以我的方法“getDatosClientes()”的第一個列印是好的,但第二個,替換串列中的第一個。我會說問題出在更新上,但我已經嘗試了很多東西,但似乎沒有任何效果。我希望我正確地解釋了自己。非常感謝!
這是所有的代碼:
Main.java(主)
package nickoos;
public class Main
{
public static void main(String[] args)
{
Formulario1();
}
public static void Formulario1()
{
Formulario frame1 = new Formulario(); //instanciar
frame1.setContentPane(new Formulario().getPanel()); //mostrar el panel
frame1.show();
frame1.setSize(800,600);
frame1.setLocation(600,230);
}
}
Cliente.java(類)
package nickoos;
public class Cliente
{
private String nombre;
private String apellido;
private String email;
private String telefono;
public Cliente(String nombre, String apellido, String email, String telefono)
{
this.nombre = nombre;
this.apellido = apellido;
this.email = email;
this.telefono = telefono;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellido() {
return apellido;
}
public void setApellido(String apellido) {
this.apellido = apellido;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelefono() {
return telefono;
}
public void setTelefono(String telefono) {
this.telefono = telefono;
}
public String getDatosClientes()
{
return "Nombre: " getNombre() " Apellido: " getApellido() " Email: " getEmail() " Teléfono: " getTelefono();
}
}
Formulario.java(表單)
package nickoos;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class Formulario extends JFrame {
private JButton btAgregarRegistro;
private JPanel panel;
private JLabel lbTitulo;
private JLabel lbNombre;
private JTextField txNombre;
private JLabel lbApellido;
private JLabel lbInstrucciones;
private JLabel lbSimetria;
private JLabel lbEmail;
private JLabel lbTelefono;
private JTextField txApellido;
private JTextField txEmail;
private JTextField txTelefono;
private JList listClientes;
private JLabel lbSubtituloLista;
private JButton btEliminarRegistro;
private JButton btModificarRegistro;
private List<Cliente> arregloClientes = new ArrayList<>();
//Instance the class
Cliente cliente1 = new Cliente("","","","");
public JPanel getPanel() {
return panel;
}
public Formulario()
{
//Listener to add a record
btAgregarRegistro.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//Set the attributes to the object
cliente1.setNombre(txNombre.getText());
cliente1.setApellido(txApellido.getText());
cliente1.setEmail(txEmail.getText());
cliente1.setTelefono(txTelefono.getText());
//Add the object to the ArrayList
arregloClientes.add(cliente1);
//Update the list
actualizarLista();
//Clear textFields after adding the record
blanquearCampos();
}
});
//Listener "delete"
btEliminarRegistro.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
int indice = listClientes.getSelectedIndex();
arregloClientes.remove(indice);
actualizarLista();
}
});
}
//Clear textFields
private void blanquearCampos()
{
txNombre.setText("");
txApellido.setText("");
txEmail.setText("");
txTelefono.setText("");
}
//Update list
private void actualizarLista()
{
DefaultListModel datos = new DefaultListModel();
for (int i = 0; i < arregloClientes.size(); i )
{
Cliente index = arregloClientes.get(i);
datos.addElement(index.getDatosClientes());
}
listClientes.setModel(datos);
}
}
uj5u.com熱心網友回復:
移動這條線
//Instance the class
Cliente cliente1 = new Cliente("","","","");
進入這個方法
btAgregarRegistro.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//move it HERE
//Set the attributes to the object
cliente1.setNombre(txNombre.getText());
cliente1.setApellido(txApellido.getText());
cliente1.setEmail(txEmail.getText());
cliente1.setTelefono(txTelefono.getText());
//Add the object to the ArrayList
arregloClientes.add(cliente1);
//Update the list
actualizarLista();
//Clear textFields after adding the record
blanquearCampos();
}
});
正在發生的事情是您正在參考記憶體中的同一個物件并只是更改其值。您要做的是每次要添加客戶端時都創建一個新物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/336804.html
