Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JFrame zentrieren

Empfohlene Antworten

Veröffentlicht

Hallo Leute,

wie der Titel schon sagt möchte ich ein JFrame beim Öffnen des Programms in der Mitte des Bildschirmes anzeigen lassen, egal an welchen Bildschirm(beliebige Auflösung). So nun habe ich natürlich auch gegoogelt und einiges gefunden, jedoch klappt es trotzdem nicht.

Hier mein code --->

this.setContentPane(getJPanel2());

this.setTitle("PasswortGenerator");

Dimension framesize = new Dimension(314, 216);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

int top = (screenSize.height - framesize.height) / 2;

int left = (screenSize.width - framesize.height) / 2;

this.setSize(framesize);

this.setLocation(left, top);

this.setVisible(true);

setLocationRelativeTo(null);

Vielleicht könnt ihr mal einen kurz Blick drauf werfen...;)

wie der Titel schon sagt möchte ich ein JFrame beim Öffnen des Programms in der Mitte des Bildschirmes anzeigen lassen
Dann mach es doch einfach so:


JFrame frame = new JFrame();

...

frame.setSize(600, 300); die gewünschte Größe bzw. pack()

frame.setLocationRelativeTo(null);

frame.setVisible(true);

also ich habs jetzt mal in nen neuen project versucht, jedoch klappt es da auch nicht

-->

public class frame extends Frame

{

public static void main(String[]args)

{

JFrame frame = new JFrame();

frame.setSize(300,200);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

Wäre nett von dir wenn du mir kurz ein Stück Quellcode postest bei dem

das Fenster dann in der Mitte ist.

Weil bei meinem anderen Versuch ist es so, dass das Fenster kurz in der Mitte aufblinkt (weniger als 1 Sekunde) und dann aber wieder links oben in der Ecke auftaucht

Genauso, wie du es geschrieben hast:

package test;


import javax.swing.JFrame;


public class FrameTest {


  public static void main(String[] args) {

    JFrame theFrame       = new JFrame();

    theFrame.setSize(640, 480);

    theFrame.setLocationRelativeTo(null);

    theFrame.setVisible(true);

  }


}

Funktioniert hier einwandfrei.

Okay stimmt jetzt gehts.....

Aber wenn ich des in meinen Project mache, dann blinkt das Fenster in

der Mitte weniger als eine Sekunde auf und dann erscheint es wieder links oben in der Ecke wie vorhin beschrieben....also ich habe eine Klasse die von

JFrame abgeleitet ist...dann habe ich einen Konstruktor in der Klasse

public passgenerator()

{

super();

initialize();

}

private void initialize()

{

this.setContentPane(getJPanel2());

this.setTitle("Passwort Generator");

this.setSize(314, 216);

this.setLocationRelativeTo(null);

this.setVisible(true);

}

okay danke für deine Hilfe :)

Das Problem hat sich gelöst

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.