【问题标题】:Scaling JFrame content缩放 JFrame 内容
【发布时间】:2013-06-12 20:05:15
【问题描述】:

我得到了一个 JLayeredPane,其中包含一个包含图像的 JLabel。 这看起来像这样:

JLayeredPane panel = new JLayeredPane();
JLabel label1 = new JLabel();
label1.setIcon(new ImageIcon(image));
label1.setBounds(0, 0, 1300, 900);
panel.add(label1, 0);
frame.add(panel);
frame.setSize(1320,900);
frame.setVisible(true);

以及 JLayeredPane 中的一些其他图像。

一切正常。但是当我在 Windows 中缩放框架时,图像不会缩放。 (图像保持相同的大小,窗口只是随着灰色空间变大。)现在我的问题:我该怎么做才能使图像始终填满框架,无论我如何缩放它?

【问题讨论】:

标签: java image swing jframe


【解决方案1】:

不确定您为什么为此使用 JLayeredPane。

也许你可以使用 Darryl 的 Stretch Icon 类。

【讨论】:

    【解决方案2】:

    您真正需要的是带有图像的 JComponent,并像这样覆盖paintComponent:

    public class FillImage extends JComponent {
        final Image image;
    
        public FillImage(final Image image) {
            this.image = image;
        }
    
        @Override
        public paintComponent(Graphics g) {
           // Additionally, you may set extra hints for better scaling
    
           // Draw image stretched to fill entire component
           g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
        }
    }
    

    然后,确保将 FillImage 组件添加到容器中,其布局使其填充所有可用空间(如 CENTER 中的 BorderLayout)。

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多