Java - ¿Cómo adaptar programa a la resolución de la pantalla?

 
Vista:
sin imagen de perfil
Val: 87
Ha aumentado su posición en 2 puestos en Java (en relación al último mes)
Gráfica de Java

¿Cómo adaptar programa a la resolución de la pantalla?

Publicado por Tomas (76 intervenciones) el 29/04/2016 01:12:25
Pongamos que voi a trabajar con una pantalla de 15 o más pulgadas, con una resolución minima de 1024x768
Tengo un JFrame, con varios JPanels, lo que hice fue usar :
1
2
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
private static Rectangle bounds = env.getMaximumWindowBounds();
y crear una relación ax = Lpx/Lox, ay = Lpy/Loy
donde ax es anchura del panel, Lpx ( anchura pantalla ) , Lox (anchura original o minima , en este caso la resolución de la pantalla). ay seria la altura etc.
Entonces pensé en definir un tamaño y posición dentro de esa resolución minima(ej: si tengo 1024 px de anchura, panel1 le doi 400px, panel 2 otros 400, me sobran 224) a cada panel, y multiplicarla por la relación. Pero no funciona. Cuando uso resoluciones mas grandes o mas pequeñas no se ven, otras veces se descuadra (sale fuera del JFrame).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class GUI extends JFrame {
 
	private JPanel contentPane;
	private static Rectangle bounds;
	private JPanel panel1;
	private JPanel panel2;
	private JPanel panel3;
	private int anchuraContentPane = 1024;
	private int alturaContentPane = 768;
	private int relacionX = ((int)bounds.getWidth()/anchuraContentPane);
	private int relacionY = ((int)bounds.getHeight()/alturaContentPane);
 
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
 
			public void run() {
				try {
 
					Toolkit.getDefaultToolkit().getScreenSize();
 
					//reolucion teniendo en cuenta el entorno grafico.
					GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
			        bounds = env.getMaximumWindowBounds();
					GUI frame = new GUI();
					frame.setVisible(true);
 
 
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 
 
	public GUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
		//this.setExtendedState(MAXIMIZED_BOTH);
		setBounds(0,0,(int)bounds.getWidth(),(int)bounds.getHeight());
 
		contentPane = new JPanel();
		contentPane.setBounds(new Rectangle(0, 0,(int) bounds.getWidth(), (int) bounds.getHeight()));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
 
 
		panel1 = new JPanel();
		//panel1.setBounds(12,12,600, 300);
		panel1.setBounds(5*relacionX,5*relacionY,400*relacionX, 300*relacionY);
		panel1.setBackground(Color.LIGHT_GRAY);
		contentPane.add(panel1);
 
		panel2= new JPanel();
		panel2.setBackground(Color.LIGHT_GRAY);
		panel2.setBounds(610*relacionX,5*relacionY,400*relacionX, 50*relacionY);
		contentPane.add(panel2);
 
		panel3= new JPanel();
		panel3.setBounds(5*relacionX,310*relacionY,250*relacionX, 300*relacionY);
		panel3.setBackground(Color.LIGHT_GRAY);
		contentPane.add(panel3);
     }
}

¿Alguna idea mejor?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder