嗨,我有两类 Banque 和 Succursale,Bank 有一个 Succursale 列表,每次该列表更改时,我希望 Succursale 获取它,然后使用它来刷新其视图的 JCombobox,问题是 JComboBox 在其项目时不刷新计数正在增加,这里是类
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
public class Banque extends Observable {
private List<Succursale> listSucc = new ArrayList<Succursale>();
private int sommeTotale;
private int nbSuccInit = 4;
public void ajouteSucc(Succursale suc){
}
public Banque(){
initialiserServeur();
}
private void initialiserServeur() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(10118);
}
catch (IOException e)
{
System.err.println("On ne peut pas ecouter au port: 10118.");
System.exit(1);
}
System.out.println ("Le serveur est en marche, Attente de la connexion.....");
int i = 0;
while(i<5){
try {
UtilMultiTh mt = new UtilMultiTh(serverSocket.accept());
Thread t = new Thread(mt);
t.start();
t.join();
Succursale s = mt.getSuc();
listSucc.add(s);
this.addObserver(s);
this.setChanged();
this.notifyObservers(this.listSucc);
//System.out.println(listSucc.size());
/* for(int j =0; j<listSucc.size();j++){
System.out.println("La succursale "+(j+1)+" est:"+listSucc.get(j).getName());
}
i++;
System.out.println("FLAGPOSTban");*/
}
catch (IOException | InterruptedException e)
{
System.err.println("Accept a echoue.");
System.exit(1);
}
}
System.out.println ("connexion reussie");
System.out.println ("Attente de l'entree.....");
}
public static void main (String [] args){
Banque banK = new Banque();
}
}
类Succursale
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ComboBoxModel;
public class Succursale implements Serializable, Observer {
private String coordonnees;
private String name;
private int sommeDepart;
private int sommeRecue;
private int sommeEnvoyee;
private List<Succursale> listSuccAc = new ArrayList<Succursale>();
private GuiSuccursale succView;
public Succursale (){
succView = new GuiSuccursale(Constantes.sommeDepart,1);
this.sommeDepart=Constantes.sommeDepart;
this.name="Succursale: "+(1);
connexionBanque();
}
public void connexionBanque(){
String host = Constantes.adrBanque[0];
int port = Constantes.portBanque[0];
Socket echoSocket = null;
try {
echoSocket = new Socket(host, port);
ObjectOutputStream oos = new ObjectOutputStream(echoSocket.getOutputStream());
oos.writeObject(this);
//System.out.println("FLAGPOSTSUcc");
} catch (UnknownHostException e) {
System.err.println("Hôte inconnu: " + host);
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Ne pas se connecter au serveur: " + host);
System.exit(1);
}
}
public void connexionSuccursales(){
}
public void envoiManuel(){
}
public String getName() {
return name;
}
public void envoiPeriodique(){
}
public static void main (String[] args){
Succursale suc = new Succursale();
}
@Override
public void update(Observable arg0, Object arg1) {
this.listSuccAc= (List)arg1;
this.succView.setListeSucc(listSuccAc);
//this.succView.validate();
//System.out.println("Mes succ connectees sont:"+this.listSuccAc.size());
//System.out.println("Mes succ connectees sont:"+this.getName());
}
}
和 Succursale 的观点
import java.awt.Font;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GuiSuccursale extends JFrame{
private JLabel labelSum = new JLabel("Montant:");
private JIntField champMontant = new JIntField(0, 0, Integer.MAX_VALUE);
private JLabel labelSumDispo = new JLabel ("Somme Disponible");
private JIntField champSumDispo;
private JLabel labelSucc = new JLabel("Vers la Succursale:");
private ComboBoxModel cbm;
private JComboBox listeSucc = new JComboBox<>();
private JPanel pan = new JPanel();
private JButton btnTransferer = new JButton("Transferer");
private JButton btnEffacer = new JButton("Effacer");
public GuiSuccursale(int sumDepart, int num){
init(sumDepart, num);
}
public void init(int sumDepart, int num){
GridLayout gl = new GridLayout();
gl.setColumns(2);
gl.setRows(4);
gl.setHgap(5);
gl.setVgap(5);
pan.setLayout(gl);
labelSumDispo.setHorizontalAlignment(JLabel.CENTER);
labelSumDispo.setFont(new Font("Tahoma", Font.BOLD, 11));
champSumDispo = new JIntField(sumDepart, 0, Integer.MAX_VALUE);
champSumDispo.setEditable(false);
labelSucc.setHorizontalAlignment(JLabel.CENTER);
labelSucc.setFont(new Font("Tahoma", Font.BOLD, 11));
labelSum.setHorizontalAlignment(JLabel.CENTER);
labelSum.setFont(new Font("Tahoma", Font.BOLD, 11));
pan.add(labelSumDispo);
pan.add(champSumDispo);
pan.add(labelSucc);
pan.add(listeSucc);
pan.add(labelSum);
pan.add(champMontant);
pan.add(btnTransferer);
pan.add(btnEffacer);
this.setContentPane(pan);
this.setSize(300, 150);
this.setTitle("Succursale: "+num);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setVisible(true);
}
public JComboBox getListeSucc() {
return listeSucc;
}
public void setListeSucc(List<Succursale> listeSucc) {
this.listeSucc.removeAllItems();
for(int i = 0; i<listeSucc.size();i++){
System.out.println("l<index est:"+i);
this.listeSucc.addItem(listeSucc.get(i).getName());
System.out.println(this.listeSucc.getItemCount());
}
}
}