简体   繁体   English

当我为JFrame使用FlowLayout时,为什么我的图像没有出现?

[英]Why does my image not appear when I use FlowLayout for my JFrame?

There is an issue which I do not understand: Why I am able to paint the image with the drawImage() method if the Layout of the JFrame is a BorderLayout or GridLayout but NOT GridbagLayout , FlowLayout or BoxLayout ? 有一个我不明白的问题:如果JFrame的布局是BorderLayoutGridLayout而不是GridbagLayoutFlowLayoutBoxLayout ,为什么我可以用drawImage()方法绘制图像? Can somebody please explain it to me? 有人可以向我解释吗?

Here is the code: 这是代码:

package footballQuestioner;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;




public class attempter {

    public static void main(String[] args) {
        JFrame frame = new Beispielfenster();

    }

}

class Beispielfenster extends JFrame {

    private class TransparentBG extends JLabel {

        BufferedImage image;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            try {
                image = ImageIO
                        .read(TransparentBG.class
                                .getClassLoader()
                                .getResourceAsStream(
                                        "footballQuestioner/rightAnswerSign.png"));

            } catch (IOException e) {
                e.printStackTrace();
            }

            Graphics2D g2d = (Graphics2D) g;

            g2d.drawImage(image, 0, 0, null);
            g2d.dispose();


        }

    }



    public Beispielfenster() {



             //setLayout(new FlowLayout());
        JPanel panel=new JPanel(new BorderLayout());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        JLabel label=new TransparentBG();

        panel.add(label);

        add(panel);

        pack();

        centeringWindow();
        setVisible(true);

    }

    public void centeringWindow() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x;
        int y;

        x = (int) (dimension.getWidth() - getWidth()) / 2;
        y = (int) (dimension.getHeight() - getHeight()) / 2;

        setLocation(x, y);
    }



}

Because with the first two layouts, the JLabel will fill the container, here the JPanel. 因为使用前两个布局,JLabel将填充容器,这里是JPanel。 With the other layouts, it will size to its preferredSize which is 0. Consider overriding getPreferredSize if you wish to use other layouts. 对于其他布局,它将调整为其preferredSize为0。如果您希望使用其他布局,请考虑覆盖getPreferredSize。

Also note: 另请注意:

  • You should not read in image files without a paintComponent method. 如果没有paintComponent方法,则不应读入图像文件。
  • You shouldn't be re-reading image files if possible (which will happen if you read in the images in a method that gets repeatedly called). 如果可能的话,您不应该重新读取图像文件(如果您使用反复调用的方法读取图像,则会发生这种情况)。 Read them in once, such as in a constructor, and store them in a field. 一次读取它们,例如在构造函数中,然后将它们存储在字段中。
  • You should not dispose of a Graphics object that the JVM gives you, only one that you create yourself (such as from a BufferedImage). 您不应丢弃JVM提供给您的Graphics对象,而只能丢弃您自己创建的Graphics对象(例如,从BufferedImage获取)。

Edit 编辑
For example, something like this: 例如,如下所示:

private class TransparentBG extends JLabel {
  BufferedImage image;

  public TransparentBG() throws IOException {
     image = ImageIO.read(TransparentBG.class.getClassLoader()
           .getResourceAsStream("footballQuestioner/rightAnswerSign.png"));
  }

  @Override
  protected void paintComponent(Graphics g) {
     super.paintComponent(g);
     Graphics2D g2d = (Graphics2D) g;
     if (image != null) {
        g2d.drawImage(image, 0, 0, null);
     }
     // g2d.dispose();
  }

  @Override
  public Dimension getPreferredSize() {
     if (image != null) {
        int w = image.getWidth();
        int h = image.getHeight();
        return new Dimension(w, h);
     }
     return super.getPreferredSize();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM