Java Swing sembra stia cambiando il layout dalla scheda di layout
-
25-09-2019 - |
Domanda
Sto avendo alcuni sintomi piuttosto strani con JPanels e CardLayout. In sostanza, ho le carte che agiscono come "pagine" dal momento che può contenere solo 12 celle in griglia, e ho visualizzare ogni pagina come carta, e cliccando -> e <- pulsanti cambiare le pagine di conseguenza (o che è l'idea). Poiché questi dati è che rappresenta un modello di cambiamento, ogni cinque secondi mi stavo per avere essere aggiunte nuove carte la visualizzazione di informazioni aggiornate, e semplicemente rimuovere le vecchie pagine.
In sostanza, ho costruire le pagine iniziali, quando il programma si avvia:
public OrderSimPanel() {
super( new CardLayout() );
// Create the map
frames = new TreeMap<Integer, String>();
// Update and draw
refreshRelevantOrders();
drawPanel();
// Set a timer for future updating
java.util.Timer timer = new java.util.Timer();
timer.schedule(new UpdateTask(), 5000);
System.out.println("Constructor DOES get called");
System.out.println("Panel type is " + this.getLayout().getClass().getName() );
}
Dove refershReleventOrders () solo aggiorna un elenco, e drawPanel () è qualcosa di simile a questo:
private void drawPanel() {
System.out.println("Panel type is " + this.getLayout().getClass().getName() );
// Set all old frames to deprecated
for( Integer k : frames.keySet() ) {
String o = frames.get( k );
frames.remove(k);
k = -k;
frames.put(k, o);
}
// Frame to add to
JPanel frame = new JPanel( new GridLayout(3,4) );
int f = 0;
// Create new cells in groups of 12
for( int i = 0; i < orders.size(); i++ ) {
// Pagination powers activate!
int c = i % 12;
f = i / 12;
// Create a new panel if we've run out of room on this one
if ( c == 0 && i > 0 ) {
// Add old frame
String id = new Double( Math.random() ).toString();
frames.put(f, id);
add( frame, id );
// Create new one
frame = new JPanel( new GridLayout(3,4) );
}
// Add the order cell to the panel
frame.add(new OrderCellPanel( orders.get(i) ));
i++;
}
// Add last frame
String id = new Double( Math.random() ).toString();
frames.put(f, id);
add( frame, id );
// Set active frame to 0'th frame
((CardLayout)(this.getLayout())).show(this, frames.get(0));
// Erase other frames and from map
for( Integer k : frames.keySet() ) {
if( k < 0 ) {
this.remove(0);
frames.remove(k);
}
}
}
Il compito del timer creato nel costruttore chiama refreshRelevantOrders () un drawPanel (). Gli sguardi di uscita stampa iniziali come questo:
Panel type is java.awt.CardLayout
Constructor DOES get called
Panel type is java.awt.CardLayout
Ma quando viene eseguito il timer, questo viene visualizzato:
Panel type is javax.swing.GroupLayout
E, naturalmente, il codice di colata in drawPanel () non riesce. Grazie per tutte le idee!
Soluzione
Override setLayout () metodo, e stampare i nomi di vecchio / nuovo classe di layout lì (prima o dopo aver chiamato il super implementazione). In questo modo potrete vedere quale parte del codice imposta GroupLayout sul pannello.