Mainframe zentrieren

Status
Nicht offen für weitere Antworten.

Kaladial

Bekanntes Mitglied
moin

ich hab da nen problem:
ich möchte mein hauptfester, welches 1/2 bildschirmgröße groß ist, in der mitte zentrieren ...
nur irgendwie funktioniert das net ...

kann mir da wer helfen ?

hier mein code:

Code:
package AVS5Host;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Rectangle;

import javax.swing.JPanel;
import javax.swing.JFrame;

public class AVS5Host extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	public int mainframe_x=0;
	public int mainframe_y=0;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AVS5Host thisClass = new AVS5Host();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public AVS5Host() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.mainframe_x=this.getGraphicsConfiguration().getBounds().width;
		this.mainframe_y=this.getGraphicsConfiguration().getBounds().height;
		int x = ((this.mainframe_x/2)-(this.mainframe_x/4));
		int y = ((this.mainframe_y/2)-(this.mainframe_y/4));
		this.setBounds(new Rectangle(x, y, (this.mainframe_x/2), (this.mainframe_y/2)));
		this.setUndecorated(true);
		this.setTitle("AVS 5 - Host");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

}
 
Das geht auch einfacher:

Code:
myFrame.setLocationrelativeTo(null);

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Window.html#setLocationRelativeTo(java.awt.Component)
 
sorry aber das hab ich auch gelesen nur hab ich das net hin bekommen ... kann mir das wer in dem zusammenhang von meinem beispiel ma schreiben ?
 
ok hat sich erledigt ich habs ... *grml* man sollte net immer noch andere positionierungssachen einbauen ... die heben sich irgendwie gegenseitig auf 😉
 
Code:
   private void initialize() {
//      this.mainframe_x=this.getGraphicsConfiguration().getBounds().width;
//      this.mainframe_y=this.getGraphicsConfiguration().getBounds().height;
//      int x = ((this.mainframe_x/2)-(this.mainframe_x/4));
//      int y = ((this.mainframe_y/2)-(this.mainframe_y/4));
//      this.setBounds(new Rectangle(x, y, (this.mainframe_x/2), (this.mainframe_y/2)));
	   this.setSize(getGraphicsConfiguration().getBounds().width/2, 
			   this.getGraphicsConfiguration().getBounds().height/2);
	   this.setLocationRelativeTo(null);
      this.setUndecorated(true);
      this.setTitle("AVS 5 - Host");
      this.setContentPane(getJContentPane());
   }
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben