1

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

假设我有一张 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);
}
4

2 回答 2

4

Don't call setSize at all, call pack (as VGR stated in his comment). pack will size your JFrame based on size's of component's within it, and gaps between those component's.

Now.. issue you will encounter is that your JFrame will be small at startup. So override getPreferredSize method for your JPanel to return dimensions of your image:

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

Now your image will fit perfectly and your application will be fully OS independent. And also, do not override paint method. Instead, override paintComponent. Here is a small demo I made in cases like yours:

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());
        }
    }
}
于 2013-10-09T12:20:24.847 回答
1

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

于 2013-10-09T12:14:36.380 回答