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: Insets

Empfohlene Antworten

Veröffentlicht

Hallo zusammen,

ich baue mir ein JFrame und setze ein JPanel als ContentPane. Das ContentPane bekommt eine feste Größe. Das JFrame soll nun so breit und so hoch wie das ContentPane sein, plus Insets des JFrames.

Mein Problem ist: Wenn ich das JFrame erstellt habe, sind all seine Insets angeblich 0. Da es nun genauso groß ist wie sein ContentPane, aber noch Platz für Rahmen und Titelleiste braucht, geht mir die komplette Optik in die Binsen.

Lasse ich mir allerdings zunächst einen MessageDialog anzeigen, kennt es danach seine Insets auf einmal, und alles wird wie gewünscht angezeigt.

Woran liegt das und wie schaffe ich Abhilfe? Ich habe ausgiebigst im Netz gesucht, aber keine Lösung gefunden. Vielleicht hab ich mich auch zu sehr verrannt, und die Lösung ist ganz simpel...? Vielen Dank schonmal für eure Hilfe!

..azett


public class MeinFrame extends JFrame {
public MeinFrame() {
super();
MeinPanel panel = new MeinPanel();
setContentPane(panel);

JOptionPane.showMessageDialog(this, "Zusätzlicher Dialog");

Insets myInsets = this.getInsets();
int myWidth = this.getContentPane().getWidth()
+ myInsets.left
+ myInsets.right;
int myHeight = this.getContentPane().getHeight()
+ myInsets.top
+ myInsets.bottom;
this.setSize(myWidth, myHeight);
this.setVisible(true);
}

public class MeinPanel extends JPanel {
public MeinPanel() {
// GUI-Elemente...
this.setSize(400,200);
this.setVisible(true);
}
}

public static void main(String[] args)
{
MeinFrame neu = new MeinFrame();
}
}
[/PHP]

Da gibt es eine schöne Methode eines jeden JFrames. Schimpft sich pack

Schreib einfach

...

this.pack();

this.setVisible(true);

...

  • Autor

Jau, aber ich verwende ein Null-Layout auf dem ContentPane - pack() staucht mir das gesamte Programmfenster auf die Titelleiste zusammen. Zumal ich mir dann immer noch die Frage stelle, warum das JFrame seine Insets zunächst mit 0 angibt...

Aber trotzdem vielen Dank :)

Zumal ich mir dann immer noch die Frage stelle, warum das JFrame seine Insets zunächst mit 0 angibt...
Weil es genau so in der Doku steht:

The size of the frame includes any area designated for the border. The dimensions of the border area may be obtained using the getInsets method, however, since these dimensions are platform-dependent,
a valid insets value cannot be obtained until the frame is made displayable by either calling pack or show.

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.