2011-10-02 78 views
13

Estoy haciendo un proyecto usando Java y necesito volver a cargar JFrame después de hacer clic en un botón en particular en ese JFrame. ¿Como hacer esto?¿Cómo puedo actualizar o volver a cargar el JFrame?

+3

¿qué quieres decir con 'actualizar'? ¿Es 'devolver todos los componentes al estado en que se encontraban cuando se colocaron primero en el marco', 'cambiar los componentes', alguna otra cosa? –

+0

es usted agrega/elimina JComponent en Runtime, o cambia Tamaño, necesario para volver a diseñar ..., – mKorbel

+0

qué está haciendo exactamente con JFrame. Amablemente elobrate. ? –

Respuesta

23

Trate

SwingUtilities.updateComponentTreeUI(frame); 

Si aún así no funciona, entonces después de completar el paso por encima de tratar

frame.invalidate(); 
frame.validate(); 
frame.repaint(); 
+4

-1 para updateComponentTreeUI: eso es casi ** siempre ** la cosa incorrecta que hay que hacer (es volver a configurar los ui-delegados) cuando el objetivo más probable es simplemente volver a cargar los datos. – kleopatra

+1

_reload frame_ no es un término técnico y sea lo que sea que se suponga que signifique, updateComponentTree es ** NO ** la respuesta. En realidad, la pregunta tal como está es demasiado inespecífica para ser respondida, por lo que votó para cerrar – kleopatra

0

Debe utilizar este código

this.setVisible(false); //this will close frame i.e. NewJFrame 

new NewJFrame().setVisible(true); // Now this will open NewJFrame for you again and will also get refreshed 
+6

mala pregunta atraer malas respuestas (es por eso que sugirió cerrar la pregunta sin éxito ;-) * NO * - cualquiera que sea el problema real en la pregunta, crear un nuevo marco es * NO * la solución. No destruirías tu casa solo porque necesita pintura nueva, ¿o sí? – kleopatra

+0

@kleopatra No, no lo hará. – Ibrahim

0

Prueba este código. También me enfrenté al mismo problema, pero de alguna forma lo resolví.

public class KitchenUserInterface { 

    private JFrame frame; 
    private JPanel main_panel, northpanel , southpanel; 
    private JLabel label; 
    private JButton nextOrder; 
    private JList list; 

    private static KitchenUserInterface kitchenRunner ; 

    public void setList(String[] order){ 
     kitchenRunner.frame.dispose(); 
     kitchenRunner.frame.setVisible(false); 
     kitchenRunner= new KitchenUserInterface(order); 

    } 

    public KitchenUserInterface getInstance() { 
     if(kitchenRunner == null) { 
      synchronized(KitchenUserInterface.class) { 
       if(kitchenRunner == null) { 
        kitchenRunner = new KitchenUserInterface(); 
       } 
      } 
     } 


     return this.kitchenRunner; 
    } 

    private KitchenUserInterface() { 


     frame = new JFrame("Lullaby's Kitchen"); 
     main_panel = new JPanel(); 
     main_panel.setLayout(new BorderLayout()); 
     frame.setContentPane(main_panel); 



     northpanel = new JPanel(); 
     northpanel.setLayout(new FlowLayout()); 


     label = new JLabel("Kitchen"); 

     northpanel.add(label); 


     main_panel.add(northpanel , BorderLayout.NORTH); 


     frame.setSize(500 , 500); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    private KitchenUserInterface (String[] order){ 
     this(); 
     list = new JList<String>(order); 
     main_panel.add(list , BorderLayout.CENTER); 

     southpanel = new JPanel(); 
     southpanel.setLayout(new FlowLayout()); 

     nextOrder = new JButton("Next Order Set"); 
     nextOrder.addActionListener(new OrderUpListener(list)); 
     southpanel.add(nextOrder); 
     main_panel.add(southpanel, BorderLayout.SOUTH); 



    } 

    public static void main(String[] args) { 
     KitchenUserInterface dat = kitchenRunner.getInstance(); 
     try{ 

     Thread.sleep(1500); 
     System.out.println("Ready"); 
     dat.setList(OrderArray.getInstance().getOrders()); 
     } 
     catch(Exception event) { 
      System.out.println("Error sleep"); 
      System.out.println(event); 
     } 


     } 

} 
3

sólo tiene que utilizar frame.setVisible (false); frame.setVisible (true); He tenido este problema con JLabels con imágenes, y esto lo resolvió

Cuestiones relacionadas