2012-03-17 33 views
6

Necesito colocar JFrame en mi pantalla. Pero no puedo hacer que aparezcan en el lado derecho de la pantalla inferior.Posición en la parte inferior derecha de la pantalla

Por favor alguien me puede explicar cómo colocarlos, si puede describir cómo hacerlo, sería genial.

Aquí está el código hasta ahora.

//Gets the screen size and positions the frame left bottom of the screen 
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    GraphicsDevice defaultScreen = ge.getDefaultScreenDevice(); 
    Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds(); 
    int x = (int)rect.getMinX(); 
    int y = (int)rect.getMaxY()- frame.getHeight(); 
    frame.setLocation(x ,y - 45); 
+0

Algunas plataformas limitan esto, como se menciona [aquí] (http://stackoverflow.com/a/2188981/230513). – trashgod

Respuesta

13

Pruebe el siguiente ejemplo. Tenga en cuenta cómo pack() "Causa que Window tenga el tamaño adecuado para el tamaño preferido y los diseños de sus subcomponentes".

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Rectangle; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

/** @see http://stackoverflow.com/q/9753722/230513 */ 
public class LowerRightFrame { 

    private void display() { 
     JFrame f = new JFrame("LowerRightFrame"); 
     f.add(new JPanel() { 

      @Override // placeholder for actual content 
      public Dimension getPreferredSize() { 
       return new Dimension(320, 240); 
      } 

     }); 
     f.pack(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice defaultScreen = ge.getDefaultScreenDevice(); 
     Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds(); 
     int x = (int) rect.getMaxX() - f.getWidth(); 
     int y = (int) rect.getMaxY() - f.getHeight(); 
     f.setLocation(x, y); 
     f.setVisible(true); 
    } 

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

      @Override 
      public void run() { 
       new LowerRightFrame().display(); 
      } 
     }); 
    } 
} 
3

La manera más fácil que conozco es anidar JPanels cada uno usando su propio administrador de disposición.

  • Los principales JPanel utilizarían un BorderLayout
  • Otro JPanel que se añade a la principal en la posición BorderLayout.SOUTH también utiliza BorderLayout.
  • El componente que debe ir en la esquina SE se agrega al JPanel anterior en la posición BorderLayout.EAST.
  • En general, casi siempre es mejor usar gestores de disposición que tratar de establecer la posición absoluta de los componentes.
+0

sí! Quiero configurar JFrame ¡Perdone mi falta de comprensión! ¡Pero gracias por intentarlo! – Isuru

+0

Solo un consejo para referirse a 'BorderLayout.SOUTH' y' BorderLayout.EAST', cuando debemos usar 'BorderLayout.PAGE_END' y' BorderLayout.LINE_START' respectivamente, para jdk 1.4+, como se menciona en [BorderLayout Tutorials ] (http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) :-) –

Cuestiones relacionadas