【问题标题】:Platform independent Image in javajava中的平台无关图像
【发布时间】:2013-10-16 18:43:32
【问题描述】:

我正在尝试将图像绘制到面板上,该面板包含在框架中。

假设我有一张 320 x 480 的图像。 当我尝试创建一个大小为 320x480 的框架并将面板添加到其中时,我遇到了一个问题。

在不同的操作系统中,320x480 的 JFrame 因标题栏大小不同。 因此,我在 Windows XP 中的正确匹配图像将无法在 Windows8 或 Ubuntu 中正确绘制。

由于图像放置不正确,因此可以看到灰色补丁。 我尝试覆盖绘画方法并使用 ImageIcon。

请提供解决方案。

TIA

代码片段

CLASS PA CONTENTS
setPreferredSize(new Dimension(500,500));
.
.
JLabel image= new JLabel();
ImageIcon background = new ImageIcon(getClass().getClassLoader().getResource("Flower.jpg"));
image.setBounds(0, 0, 500, 500);
image.setIcon(background);
this.add(image);    //where "this" is extending from JPanel

CLASS PB CONTENTS
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inserting(frame.getContentPane());

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);

private void inserting(Container pane)
{
cardPanel=new JPanel();
            CardLayout cards=new CardLayout();
cardPanel.setLayout(cards);

PA home= new PA();
cardPanel.add(home,"homeScreen");

    pane.add(cardPanel);
}

【问题讨论】:

  • 为了尽快获得更好的帮助,请提供SSCCE
  • 您是否尝试过在您的 JFrame 上调用 pack() 而不是明确设置其大小?
  • 是的,我使用了 pack,因为我明确指定了包含面板的大小。那是问题吗? (添加代码)
  • 请看一下这个related example。希望它有所帮助:-)
  • @nIcEcOw 我希望你已经发布了这个作为答案。 :) getPreferredSize 的覆盖正是我所缺乏的。非常感谢!

标签: java image swing jframe paint


【解决方案1】:

这似乎是布局问题。最明显的解决方案是将您的图像面板包装到另一个具有适当布局的容器中,这样您的面板将始终具有相同的大小。

【讨论】:

    【解决方案2】:

    根本不要打电话给setSize,打电话给pack(正如VGR在他的评论中所说)。 pack 将根据其中组件的大小以及这些组件之间的间隙来调整您的 JFrame

    现在.. 你会遇到的问题是你的JFrame 在启动时会很小。因此,为您的 JPanel 覆盖 getPreferredSize 方法以返回图像的尺寸:

    public void getPreferredSize() {
        return new Dimension(image.getWidth(), image.getHeight());
    }
    

    现在您的图像将完美契合,您的应用程序将完全独立于操作系统。 而且,不要覆盖paint 方法。相反,覆盖paintComponent。 这是我在像你这样的情况下制作的一个小演示:

    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    
    public class Drawing {
        JFrame frame = new JFrame();
    
        public Drawing() {
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(new Panel());
            frame.pack();
            frame.setVisible(true);
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Drawing();
                }
            });
        }
    
        class Panel extends JPanel {
            BufferedImage image = null;
    
            Panel() {
                try {
                    image = ImageIO.read(new File("path-to-your-image"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, this);
            }
    
            @Override
            public Dimension getPreferredSize() {
                // Panel will be sizes based on dimensions of image
                return new Dimension(image.getWidth(), image.getHeight());
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-02
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2010-09-30
      • 2011-08-28
      • 2016-06-02
      • 2019-04-12
      • 1970-01-01
      相关资源
      最近更新 更多