Java Swing parece estar cambiando el diseño de la tarjeta de diseño
-
25-09-2019 - |
Pregunta
Estoy teniendo algunos síntomas bastante extraños con JPanels y CardLayout. En esencia, no tengo tarjetas que actúan como "páginas" ya que sólo puede contener las 12 celdas de la parrilla, y puedo mostrar cada página como una tarjeta, y haciendo clic en -> y <- botones cambian las páginas en consecuencia (o esa es la idea). Debido a estos datos es que representa un cambio de modelo, cada cinco segundos me iba a tener añadirse nuevas tarjetas mostrar información actualizada, y simplemente eliminar las páginas de edad.
Esencialmente, la construcción de las páginas iniciales cuando se inicia el programa:
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() );
}
Cuando refershReleventOrders () simplemente actualiza una lista, y drawPanel () es algo como esto:
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);
}
}
}
La tarea de temporizador creado en el constructor llama refreshRelevantOrders () un drawPanel (). Las miradas de salida de impresión iniciales de esta manera:
Panel type is java.awt.CardLayout
Constructor DOES get called
Panel type is java.awt.CardLayout
Sin embargo, cuando se ejecuta el temporizador, este se muestra:
Panel type is javax.swing.GroupLayout
Y, por supuesto, el código de calidad en drawPanel () falla. Gracias por cualquier idea!
Solución
Reemplaza setLayout () método, e imprimir los nombres de las clases de diseño viejo / nuevo allí (antes o después de llamar a la aplicación súper). De esta forma podrás ver qué parte de su código establece GroupLayout en el panel.