Мне поручено реализовать максимизируемый диалог в Swing. Многие из наших диалогов «толстые»: они содержат таблицы, формы и т. д. Поэтому пользователям нужна возможность максимизировать диалоги (и они не хотят изменять их размер вручную). Я знаю, что Swing не предназначен для работы на собственном уровне (поскольку библиотека принадлежит Java, кроссплатформенному языку). Тем не менее, это работа, которую мне нужно закончить. Я как бы застрял, потому что, когда диалог развернут, его расположение оказывается в странном месте, с заметными пробелами по бокам. Я подозреваю, что это из-за странного [-8,-8] местоположения его предка окна, которое установлено изначально (в sun.awt.windows.WComponentPeer#pShow). Это как если бы -8 для фрейма на самом деле означает 0, а для диалога оно означает -8
Java 8. Windows 10
MRE:
Код: Выделить всё
package demos.panel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
public class FrameDemo {
private static JFrame frame;
public static void main(String[] args) {
Container mainPanel = createMainPanel();
frame = new JFrame("Frame Demo");
frame.setContentPane(mainPanel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
private static JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.add(createDebugButton());
panel.add(createDialogButton());
return panel;
}
private static JButton createDebugButton() {
JButton button = new JButton("Print frame location");
button.addActionListener(e -> {
System.out.println("Frame's location: " + frame.getLocation());
});
return button;
}
private static JButton createDialogButton() {
JButton button = new JButton("Show dialog");
button.addActionListener(e -> {
createDialog().setVisible(true);
});
return button;
}
private static JDialog createDialog() {
JDialog dialog = new JDialog();
dialog.setContentPane(createDialogPane(dialog));
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.pack();
dialog.setLocationRelativeTo(null);
return dialog;
}
private static JPanel createDialogPane(JDialog dialog) {
JPanel dialogPane = new JPanel();
dialogPane.add(createMaximizeButton(dialog));
return dialogPane;
}
private static JButton createMaximizeButton(JDialog dialog) {
JButton button = new JButton("Maximize dialog");
button.addActionListener(e -> {
maximizeAndDisableResizing(dialog);
// I won't make it a toggle, for the sake of simplicity,
// so you'll have to rerun, sorry
button.setEnabled(false);
});
return button;
}
private static void maximizeAndDisableResizing(Window window) {
maximize(window);
disableResizing(window);
}
private static void maximize(Window window) {
Rectangle maximumPossibleBounds = getMaximumPossibleBounds();
maximumPossibleBounds.setLocation(getTopmostContainerLocation(window));
window.setBounds(maximumPossibleBounds);
}
private static Rectangle getMaximumPossibleBounds() {
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
return graphicsEnvironment.getMaximumWindowBounds();
}
private static Point getTopmostContainerLocation(Window window) {
Window topmostContainer = SwingUtilities.getWindowAncestor(window);
Point location = (topmostContainer == null) ? new Point(0, 0) : topmostContainer.getLocation();
return location;
}
private static void disableResizing(Window window) {
((Dialog) window).setResizable(false);
}
}

Подробнее здесь: https://stackoverflow.com/questions/790 ... t-be-fixed
Мобильная версия