2013-02-14 12 views
5

Ho quattro telai interni e 3 bottoni in esso .Quando ho colpito il pulsante di ingrandimento, massimizza ma si sovrappone tutto il frames.But il mio punto è che dovrebbe mostrare le cornici minimizzate. si prega di trovare il codice qui sottocome mantenere JInternalFrame minimizzato in cima

package Project; 

import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.HeadlessException; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.beans.PropertyVetoException; 
import javax.swing.JButton; 
import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.plaf.basic.BasicInternalFrameTitlePane; 
import javax.swing.plaf.basic.BasicInternalFrameUI; 

public class Test { 

    public Test() throws HeadlessException, PropertyVetoException { 
     createAndShowGUI(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        new Test(); 
       } catch (HeadlessException ex) { 
        //Logger.getLogger(MinPanel1.class.getName()).log(Level.SEVERE, null, ex); 
       } catch (PropertyVetoException ex) { 
        // Logger.getLogger(MinPanel1.class.getName()).log(Level.SEVERE, null, ex); 
       } 

      } 
     }); 
    } 

    private void createAndShowGUI() throws HeadlessException, PropertyVetoException { 
     JFrame frame = new JFrame(); 
     frame.setResizable(true); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     final JDesktopPane jdp = new JDesktopPane() { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(400, 400); 
      } 
     }; 

     frame.setContentPane(jdp); 
     frame.pack(); 

     createAndAddInternalFrame(jdp, 0, 0); 
     createAndAddInternalFrame(jdp, 200, 0); 
     createAndAddInternalFrame(jdp, 400, 0); 
     createAndAddInternalFrame(jdp, 600, 0); 


     frame.setVisible(true); 
    } 

    private void createAndAddInternalFrame(final JDesktopPane jdp, int x, int y) throws PropertyVetoException { 
     final JInternalFrame jInternalFrame = new JInternalFrame("Test1", true, true, true, true); 
     jInternalFrame.setLocation(x, y); 
       final JInternalFrame jInternalFrame1 = new JInternalFrame("Test2", true, true, true, true); 
JPanel jp= new JPanel(); 
     JButton jb1 = new JButton("min"); 
     JButton jb2 = new JButton("[]"); 
     JButton jb3 = new JButton("X"); 

     jInternalFrame.setLayout(new GridLayout(2, 2,2,2)); 
     jInternalFrame1.add(jb1); 
     jInternalFrame.setSize(200, 200);//testing 
     jInternalFrame.setLayout(new GridLayout(2,2)); 

     JButton jb= new JButton("min"); 
     // jInternalFrame.add(jb); 
     // jInternalFrame.add(jb3); 
     //jInternalFrame.add(jb2); 
     jp.add(jb); 
     jp.add(jb2); 
     jp.add(jb3); 

     jInternalFrame.add(jp); 
     jb.addActionListener(new ActionListener() 
       { 


      @Override 
      public void actionPerformed(ActionEvent ae) { 
         try { 
          jInternalFrame.setIcon(true); 
         } catch (PropertyVetoException ex) { 
         } 

      } 


     }); 
     jb1.addActionListener(new ActionListener() 
       { 


      @Override 
      public void actionPerformed(ActionEvent ae) { 
         try { 
          jInternalFrame.setIcon(true); 
         } catch (PropertyVetoException ex) { 
         } 

      } 


     }); 
     jb2.addActionListener(new ActionListener() 
     { 


    @Override 
    public void actionPerformed(ActionEvent ae) { 
     try { 
       jInternalFrame.setMaximum(true); 

      } 
     catch(Exception e) 
     { 

     } 

    } 


});jb3.addActionListener(new ActionListener() 
{ 


@Override 
public void actionPerformed(ActionEvent ae) { 
     try { 
      jInternalFrame.dispose(); 
     } catch (Exception ex) { 
     } 

} 


}); 

     BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane(); 
     jInternalFrame.remove(titlePane); 


     jInternalFrame.setVisible(true); 
     jInternalFrame1.setVisible(true); 

     jdp.add(jInternalFrame); 
       //jdp.add(jInternalFrame1); 

    } 
} 
+1

Non ingoiare eccezioni e utilizzare lo spazio bianco con più attenzione. – trashgod

+0

@trashgod è questa la tua risposta? –

+0

Una volta risolto il problema, funziona come previsto. – trashgod

risposta

5

Si potrebbe provare: JDesktopPane#setComponentZOrder(Component com, int i). As per docs:

sposta il componente specificato all'indice ordine z specificato nel contenitore . L'ordine z determina l'ordine in cui i componenti sono verniciati ; il componente con le vernici a z più alte per primo e il componente con le ultime vernici a ordine z per ultime. In cui i componenti sovrapposizione, il componente con le vernici inferiore z-ordine oltre il componente con il più alto ordine z.

...

Nota:

Non tutte le piattaforme supportano cambiare l'ordine z di componenti pesanti da un contenitore ad un altro senza chiamata a removeNotify. Non c'è modo di rilevare se una piattaforma supporta questo, quindi gli sviluppatori non dovrebbero fare alcuna ipotesi.

Questo vi permetterà di impostare l'ordine dei JInternalFrame s contenute all'interno JDesktopPane.

UPDATE:

Come per il mio commento:

Da quello che posso vedere il suo il comportamento predefinito e doesnt sembrano essere over com-in grado da JDesktopPane#setComponentZOrder(Component com, int i) quando il JInternalFrame è iconificato. funziona bene quando la sua in normale stato

Soluzione:

Suggerisco di regolazione del livello su cui massimizzata JInternalFrame è mostrato:

jb2.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      try { 
       if (jInternalFrame.isMaximum()) {//restore 
        jInternalFrame.pack(); 
       } else {//maximize 
        jInternalFrame.setMaximum(true); 
       } 
       jdp.remove(jInternalFrame); 
       jdp.add(jInternalFrame, JDesktopPane.FRAME_CONTENT_LAYER); 
       jdp.revalidate(); 
       jdp.repaint(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 
    }); 

Dobbiamo anche non dimenticare di aggiungerlo nuovamente al DEFAULT_LAYER quando è ridotto al minimo:

jb.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      try { 
       if (jInternalFrame.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) { 
        jdp.remove(jInternalFrame); 
        jdp.add(jInternalFrame, JDesktopPane.DEFAULT_LAYER); 
        jdp.revalidate(); 
        jdp.repaint(); 
       } 
       jInternalFrame.pack(); 
       jInternalFrame.setIcon(true); 
      } catch (PropertyVetoException ex) { 
      } 

     } 
    }); 

Ecco il codice completo:

enter image description here

import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.HeadlessException; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.beans.PropertyVetoException; 
import javax.swing.JButton; 
import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.plaf.basic.BasicInternalFrameTitlePane; 
import javax.swing.plaf.basic.BasicInternalFrameUI; 

public class Test { 

    public Test() throws HeadlessException, PropertyVetoException { 
     createAndShowGUI(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        new Test(); 
       } catch (HeadlessException ex) { 
        ex.printStackTrace(); 
       } catch (PropertyVetoException ex) { 
        ex.printStackTrace(); 
       } 

      } 
     }); 
    } 

    private void createAndShowGUI() throws HeadlessException, PropertyVetoException { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     final JDesktopPane jdp = new JDesktopPane() { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(600, 400); 
      } 
     }; 

     frame.setContentPane(jdp); 
     frame.pack(); 

     createAndAddInternalFrame(jdp, 0, 0); 
     createAndAddInternalFrame(jdp, 300, 0); 
     createAndAddInternalFrame(jdp, 0, 200); 

     frame.setVisible(true); 
    } 

    private void createAndAddInternalFrame(final JDesktopPane jdp, int x, int y) throws PropertyVetoException { 
     final JInternalFrame jInternalFrame = new JInternalFrame("Test1", true, true, true, true); 
     jInternalFrame.setLocation(x, y); 

     JPanel jp = new JPanel(); 

     JButton jb = new JButton("min"); 
     JButton jb2 = new JButton("max/restore"); 
     JButton jb3 = new JButton("close"); 

     jInternalFrame.setLayout(new GridLayout(2, 2)); 

     jp.add(jb); 
     jp.add(jb2); 
     jp.add(jb3); 

     jInternalFrame.add(jp); 

     jb.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       try { 
        if (jInternalFrame.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) { 
         jdp.remove(jInternalFrame); 
         jdp.add(jInternalFrame, JDesktopPane.DEFAULT_LAYER); 
         jdp.revalidate(); 
         jdp.repaint(); 
        } 
        jInternalFrame.pack(); 
        jInternalFrame.setIcon(true); 
       } catch (PropertyVetoException ex) { 
        ex.printStackTrace(); 
       } 

      } 
     }); 
     jb2.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       try { 
        if (jInternalFrame.isMaximum()) {//restore 
         jInternalFrame.pack(); 
        } else {//maximize 
         jInternalFrame.setMaximum(true); 
        } 
        jdp.remove(jInternalFrame); 
        jdp.add(jInternalFrame, JDesktopPane.FRAME_CONTENT_LAYER); 
        jdp.revalidate(); 
        jdp.repaint(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

      } 
     }); 
     jb3.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       try { 
        jInternalFrame.dispose(); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 

      } 
     }); 

     BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane(); 
     jInternalFrame.remove(titlePane); 

     jInternalFrame.pack(); 
     jInternalFrame.setVisible(true); 

     jdp.add(jInternalFrame); 
    } 
} 
+0

quale sarebbe il valore di io? –

+0

@sukant l'indice nel contenitore cioè qualsiasi cosa meno del maximzed dovrebbe dipingere dopo esso –

+0

se inserisco il valore di indice come zero quindi funziona solo .it Fornisce l'errore di runtime per altri valori.Il codice viene eseguito per il valore zero ma l'output desiderato non è in arrivo. Quindi ho chiesto quale sarebbe il valore di i –

4

Ecco il risultato della chiamata moveToBack() nel gestore pulsante di ingrandimento. Ricordarsi inoltre di chiamare pack() sulla cornice interna.

Addendum: Ho aggiornato l'esempio per includere max, min e icona pulsanti. I pulsanti utilizzano Action per test più semplici e i frame interni hanno nomi distinti. Vedere createToolBar() per modificare dinamicamente L & F, ad es.

frame.add(createToolBar(frame), BorderLayout.NORTH); 

image

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.beans.PropertyVetoException; 
import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

//* @see https://stackoverflow.com/a/14874924/230513 */ 
public class Test { 

    public Test() { 
     createAndShowGUI(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Test(); 
      } 
     }); 
    } 

    private void createAndShowGUI() { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     final JDesktopPane jdp = new JDesktopPane() { 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(600, 400); 
      } 
     }; 
     for (int i = 0; i < 4; i++) { 
      createInternalFrame(jdp, 100 * i, 100 * i); 
     } 
     frame.add(jdp); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private void createInternalFrame(final JDesktopPane jdp, int x, int y) { 
     final JInternalFrame jif = new JInternalFrame("Test" + x, true, true, true, true); 
     jif.setLocation(x, y); 
     JPanel jp = new JPanel(); 
     jp.add(new JButton(new AbstractAction("max") { 

      @Override 
      public void actionPerformed(ActionEvent ae) { 
       try { 
        jif.setMaximum(true); 
        jif.moveToBack(); 
       } catch (PropertyVetoException e) { 
        e.printStackTrace(); 
       } 

      } 
     })); 
     jp.add(new JButton(new AbstractAction("min") { 

      @Override 
      public void actionPerformed(ActionEvent ae) { 
       try { 
        jif.setMaximum(false); 
       } catch (PropertyVetoException e) { 
        e.printStackTrace(); 
       } 

      } 
     })); 
     jp.add(new JButton(new AbstractAction("icon") { 

      @Override 
      public void actionPerformed(ActionEvent ae) { 
       try { 
        jif.setIcon(true); 
       } catch (PropertyVetoException e) { 
        e.printStackTrace(); 
       } 
      } 
     })); 
     jif.add(jp); 
     jif.pack(); 
     jif.setVisible(true); 
     jdp.add(jif); 
    } 
} 
+0

Ho ri-fattorizzato il tuo [sscce] (http://sscce.org/) per concentrarmi sulla mia comprensione del problema e facilitare la sperimentazione. – trashgod

+0

+1 bello e molto più facile del mio –

+0

@trashgod grazie per aver dato questa bellissima risposta –

Problemi correlati