Как предотвратить увеличение размера JFrame для окна при вызове package()?JAVA

Программисты JAVA общаются здесь
Ответить
Гость
 Как предотвратить увеличение размера JFrame для окна при вызове package()?

Сообщение Гость »


I have a that uses the method to minimize the size of the components within the frame. This works well enough, but occasionally, due to something upon startup, I'll have a lot of components and the frame will be gigantic. Technically, the frame won't be bigger than my screen, but essentially it will be the same size.
I also call a method on my called

Код: Выделить всё

setLocationByPlatform(true)
, which places my frame in the platform-relative default location.
However, when calling these 2 together, they end up with part of my frame being off-screen.
Now, there are a million-and-1 ways to work around this. I could full screen my application, which is most likely the real solution here. I could deactivate either or both options. I could set my size and location explicitly. And there's more solutions.
But is there any way to call both and

Код: Выделить всё

setLocationByPlatform(true)
without ending up with my frame partially off-screen? Excluding above solutions of course. I'll accept no as an answer if it can be backed up (maybe docs say these 2 can contradict each other, or something like that). I just want my window to be fully on-screen after calling both of the mentioned methods.
And to be clear, I am A-OK with the actual content on my frame being cut off because the frame is too small. By all means, if the frame is too small to hold all of the content, that is a different problem that I already have solutions that I will later apply. But for now, to demonstrate the point, I am A-OK with an answer that allows content to be cutoff the window, but the full window is fully on-screen.
And not that this needs a code example, but here is one.

Код: Выделить всё

import javax.swing.*;

public class SOQ_20231003
{

public static void main(final String[] args)
{

SwingUtilities.invokeLater(SOQ_20231003::new);

}

public SOQ_20231003()
{

final JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

final JPanel panel = new JPanel();

for (int i = 0; i < 100; i++)
{

final JButton button = new JButton();

final String text =
"""


Totam qui nihil qui possimus architecto. Quo quia voluptatem possimus. Aliquid praesentium ab assumenda nisi eligendi eum dolore sed. Sint dolorem eaque est vitae ipsam architecto. Consectetur consequatur ipsum assumenda nostrum debitis ab a nobis. Dignissimos nobis rem aut nihil mollitia rerum. Porro quis neque eos. Dolorum eum sit aperiam cumque velit nulla quidem qui. Velit facilis maiores rerum mollitia. Tempore dolor consequatur quis. Similique minima animi et et repellat quam. Hic dolore voluptatem tempora est et dolor atque dolores. Odio est repellendus fugiat neque ut quas sunt quia. Sunt ea ab qui. Placeat pariatur dolores quis earum exercitationem. Nulla aut id nulla pariatur. Officia architecto sunt ipsum. Quo voluptatibus aut qui voluptates velit accusamus fuga. Beatae et ullam adipisci. Quo ducimus et in quam soluta officia ducimus. Placeat molestiae qui quia ut. Sint dolorem porro amet minus. Labore expedita dolorem omnis eveniet et quis.


"""
;

button.setText(text);

panel.add(button);

}

frame.add(panel);

frame.pack();
frame.setLocationByPlatform(true);

frame.setVisible(true); //image is cut-off on the right hand side of the screen!

}

}
I guess I am just more surprised that a method as encouraged as or

Код: Выделить всё

setLocationByPlatform(true)
would default to shoving things off-screen. I felt like that is something where they would either have coded it not to do that, or provided another method that would undo that from happening. Maybe a

Код: Выделить всё

resizeToAvoidClippingScreen
method or something.


Источник: https://stackoverflow.com/questions/772 ... lling-pack
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»