Swing - 如何将我的应用程序限制为屏幕大小
Swing - How to limit my app to screen size
在我的应用中,我需要 window 来显示图像查看器,具有以下大小限制:
- 如果图片较小,window会根据图片调整大小,不显示滚动条
- 如果图像很大,window 也会进行调整,但是 整个应用程序 应该在屏幕上保持可见,如果需要,图像周围会出现滚动条
目标是使图像上方和下方的 UI 保持可见。
我为演示编写了以下代码,但是对于大图像,应用程序总是比屏幕大...
知道正确的方法是什么吗?
import javax.swing.*;
import java.awt.*;
public class ScrollTest {
private static final int IMG_WIDTH = 2000; // or 500
private static final int IMG_HEIGHT = 2000; // or 500
// Unsuccessful attempts:
private static void limitAppSize(JFrame frame) {
frame.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());
// frame.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.pack();
}
public static void addComponentsToPane(Container container) {
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
JPanel imagePanel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
// Image replaced by a shape for demonstration purpose
g2d.drawOval(0,0, IMG_WIDTH, IMG_HEIGHT);
}
public Dimension getPreferredSize() {
return new Dimension(IMG_WIDTH, IMG_HEIGHT);
}
public Dimension getMaximumSize() {
return new Dimension(IMG_WIDTH, IMG_HEIGHT);
}
};
JScrollPane scrollableImagePanel = new JScrollPane(imagePanel);
container.add(new JLabel("This would be a toolbar menu with multiple lines"));
container.add(scrollableImagePanel);
container.add(new JLabel("This would be a status bar"));
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("ScrollTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
limitAppSize(frame);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGUI());
}
}
框架将调整为屏幕大小,忽略任务栏的大小。
要考虑任务栏,您可以使用:
frame.pack();
Dimension preferred = frame.getSize();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle bounds = env.getMaximumWindowBounds();
preferred.width = preferred.width > bounds.width ? bounds.width : preferred.width;
preferred.height = preferred.height > bounds.height ? bounds.height : preferred.height;
frame.setSize( preferred );
在我的应用中,我需要 window 来显示图像查看器,具有以下大小限制:
- 如果图片较小,window会根据图片调整大小,不显示滚动条
- 如果图像很大,window 也会进行调整,但是 整个应用程序 应该在屏幕上保持可见,如果需要,图像周围会出现滚动条
目标是使图像上方和下方的 UI 保持可见。
我为演示编写了以下代码,但是对于大图像,应用程序总是比屏幕大...
知道正确的方法是什么吗?
import javax.swing.*;
import java.awt.*;
public class ScrollTest {
private static final int IMG_WIDTH = 2000; // or 500
private static final int IMG_HEIGHT = 2000; // or 500
// Unsuccessful attempts:
private static void limitAppSize(JFrame frame) {
frame.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());
// frame.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.pack();
}
public static void addComponentsToPane(Container container) {
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
JPanel imagePanel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
// Image replaced by a shape for demonstration purpose
g2d.drawOval(0,0, IMG_WIDTH, IMG_HEIGHT);
}
public Dimension getPreferredSize() {
return new Dimension(IMG_WIDTH, IMG_HEIGHT);
}
public Dimension getMaximumSize() {
return new Dimension(IMG_WIDTH, IMG_HEIGHT);
}
};
JScrollPane scrollableImagePanel = new JScrollPane(imagePanel);
container.add(new JLabel("This would be a toolbar menu with multiple lines"));
container.add(scrollableImagePanel);
container.add(new JLabel("This would be a status bar"));
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("ScrollTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
limitAppSize(frame);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGUI());
}
}
框架将调整为屏幕大小,忽略任务栏的大小。
要考虑任务栏,您可以使用:
frame.pack();
Dimension preferred = frame.getSize();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle bounds = env.getMaximumWindowBounds();
preferred.width = preferred.width > bounds.width ? bounds.width : preferred.width;
preferred.height = preferred.height > bounds.height ? bounds.height : preferred.height;
frame.setSize( preferred );