Ajuda em JFrame

3 respostas
T

Como faço para, ao inicializar meu programa usando JFrame, ele abrir centralizado na tela

Valeu!!!

:smiley:

3 Respostas

F

tenta por este codigo no construtor do JFrame

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int alturaTela = d.height;
int larguraTela = d.width;
setLocation(larguraTela/4, alturaTela/4);
A

Basta vc usar o metodo setLocation(int, int)
Abaixo vai um exemplo

import javax.swing.*;
import java.awt.*;

public class Abdon extends JFrame{
    public Abdon(){    
	setTitle("Usando o setLocation(int, int)");
	//Este setLocation é o metodo e funciona como o setSize!
	setLocation(100, 100);
	setSize(30, 40);
	setVisible(true);
    }

    public static void main(String args[]){
	new Abdon();	    

    }
}
L

surgiro esta implementação:

public class Aplicacao  {
  public Aplicacao() {
    Frame frame = new seuFrame();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch(Exception e) {
      e.printStackTrace();
    }
    new Aplicacao();
  }
}
Criado 22 de outubro de 2003
Ultima resposta 22 de out. de 2003
Respostas 3
Participantes 4