Foros del Web » Programación para mayores de 30 ;) » Java »

¿Adaptar GUI a diferentes resoluciones de pantalla?

Estas en el tema de ¿Adaptar GUI a diferentes resoluciones de pantalla? en el foro de Java en Foros del Web. Tengo un JFrame, con varios JPanels dentro. ¿Como puedo hacer para que los JPanels se adapten a la resolución de la pantalla? Ahora mismo estoy ...
  #1 (permalink)  
Antiguo 02/05/2016, 13:46
 
Fecha de Ingreso: noviembre-2014
Mensajes: 17
Antigüedad: 9 años, 5 meses
Puntos: 0
¿Adaptar GUI a diferentes resoluciones de pantalla?

Tengo un JFrame, con varios JPanels dentro. ¿Como puedo hacer para que los JPanels se adapten a la resolución de la pantalla? Ahora mismo estoy con una pantalla 1440x900 y una relación 4:3 creo. El programa necesito que se adapte a pantallas 1000x768 minimo.
Intenté hacerlo usando :
Código:
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
private static Rectangle bounds = env.getMaximumWindowBounds();
int relacionY = bounds.getHeight()/768;
int relacionX = bounds.getWidth()/1000;
donde relacion es Px = LFx/LOx.
Px es la longitudX del panel, LDx longitudX de la pantalla final, LOx longitudX original de la pantalla.
Entonces multiplicaba cada propiedad (posición y tamaño) por esa relación, pero no funcionó.
  #2 (permalink)  
Antiguo 03/05/2016, 17:27
 
Fecha de Ingreso: noviembre-2014
Mensajes: 17
Antigüedad: 9 años, 5 meses
Puntos: 0
Respuesta: ¿Adaptar GUI a diferentes resoluciones de pantalla?

Solucionado, sería aproximadamente así:
Código:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Dimension;

public class TestResolution extends JFrame{

		private JPanel contentPane;
		private static Rectangle bounds;
		private JPanel firstPanel;
		private JPanel secondPanel;
		private JPanel cardPanel;
		private JPanel thirdPanel;
		private static float anchuraContentPane = 1024;
		private static float alturaContentPane = 900;
		private static float relacionX;
		private static float relacionY;
		
		public static void main(String[] args) {
			EventQueue.invokeLater(new Runnable() {

				public void run() {
					try {
						
						Toolkit.getDefaultToolkit().getScreenSize();

						GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
						
				        bounds = env.getMaximumWindowBounds();
				        relacionX = ((int)bounds.getWidth()/anchuraContentPane);
				    	relacionY = ((int)bounds.getHeight()/alturaContentPane);
				    	
						TestResolution frame = new TestResolution();
						frame.pack();
						frame.setVisible(true);

						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}

		
		public TestResolution() {
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setResizable(false);
			contentPane = new JPanel();
			setContentPane(contentPane);
			contentPane.setPreferredSize(new Dimension((int) bounds.getWidth(), (int) bounds.getHeight()));
			contentPane.setLayout(null);
			
			
					
			firstPanel = new JPanel();
			firstPanel.setBounds(5, 5,Math.round(400*relacionX),Math.round(250*relacionY));
			firstPanel.setBackground(Color.blue);
			firstPanel.setLayout(null);
			contentPane.add(firstPanel);
			
			cardPanel = new JPanel();
			cardPanel.setBounds(
					Math.round(firstPanel.getWidth()+10),
					Math.round(5*relacionY),
					Math.round(580*relacionX)+5,
					Math.round(700*relacionY));
			cardPanel.setBackground(Color.black);
			cardPanel.setLayout(null);
			contentPane.add(cardPanel);


			secondPanel = new JPanel();
			secondPanel.setBounds(
					5,
					Math.round((firstPanel.getHeight()+10)),
					Math.round(200*relacionX), 
					Math.round(450*relacionY));
			secondPanel.setBackground(new Color(0, 0, 0));
			secondPanel.setLayout(null);
			contentPane.add(secondPanel);
			
			thirdPanel = new JPanel();
			thirdPanel.setBounds(
					Math.round((secondPanel.getWidth()+10)),
					Math.round((firstPanel.getHeight()+10)),
					Math.round(195*relacionX), 
					Math.round(450*relacionY));
			thirdPanel.setBackground(new Color(0, 0, 0));
			contentPane.add(thirdPanel);
		}
}
Lo que pasa , es que en Windows no me reconoce taskbar, en linux sí.
Intenté usar GraphicsConfiguration pero no supe usarla...

Última edición por tec01; 04/05/2016 a las 03:20

Etiquetas: gui, layouts, resolución, tamaño
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:24.