简体   繁体   English

JFrame 不显示来自 JLabel 的图片

[英]JFrame does not show picture from JLabel

my JFrame does not show the image of my JLabel.我的 JFrame 不显示我的 JLabel 的图像。 The JFrame is shown but without the background image.显示了 JFrame,但没有背景图像。 Expected result was: JFrame that shows a background image ("stelle.png").预期结果是:显示背景图像(“stelle.png”)的 JFrame。 I'd greatly appreciate if anyone could help :-) Thanks!如果有人能提供帮助,我将不胜感激:-) 谢谢! Simon西蒙

    public static void main(String[] args) {
new Gui();
 }


 public class Label extends JLabel {   

@Override protected void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g.drawImage(Var.quadro, 0, 0, 800,600, null);
    repaint();
}
}



 public class Var {
static BufferedImage quadro;    
public Var(){
try {
quadro = ImageIO.read(new File("quadri/stelle.png"));
}
catch (IOException e) {
e.printStackTrace();
System.out.println("No picture");
}

}
}



public class Gui {
public Gui(){

JFrame rahmen = new JFrame();
rahmen.setSize(800,600);
rahmen.setLocationRelativeTo(null);
rahmen.setVisible(true);
rahmen.setResizable(false);
rahmen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rahmen.setLayout(null);
rahmen.setTitle("Gioco");
Label label = new Label();

label.setVisible(true);
label.setBounds(0, 0, 800, 600);
rahmen.add(label);

}
}

Hi Have tweaked your solution below:您好 在下面调整了您的解决方案:

keep the package structure intact for the Test.java (can copy all code in it) and your pic stelle.png , refer the attached image below for this example to work seem less.保持Test.java的包结构完整(可以复制其中的所有代码)和你的图片stelle.png ,参考下面的附加图片,这个例子的工作似乎更少。

for incorporating changes in your own structure please keep image in a relative package quadri ( refer the attached image , see how i kept it)要将更改合并到您自己的结构中,请将图像保存在相关的quadri包中( refer the attached image ,看看我是如何保存的)

Please pay attention to my comments.请注意我的评论。

    package com.demo.test.stack;

    import java.awt.Graphics;
    import java.awt.Graphics2D;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;

    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

//Main class for executing test
    public class Test {
        public static void main(String[] args) {
            new Gui();
        }

    }

//this is you extended Label class be careful while importing
    class Label extends JLabel {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g.drawImage(Var.quadro, 0, 0, 800, 600, null);
            repaint();
        }
    }

    class Var {
        static BufferedImage quadro;

//initializing the static variable in static class block (since you are using it directly) 
        static {
            try {
//gettting the absolute path of your image
                URL url = Test.class.getResource("quadri/stelle.png");
                System.out.println(url.getPath());
                quadro = ImageIO.read(new File(url.getPath()));
                System.out.println("quadro: " + quadro);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("No picture");
            }

        }
    }

//your feature class
    class Gui {
        public Gui() {

            JFrame rahmen = new JFrame();
            rahmen.setSize(800, 600);
            rahmen.setLocationRelativeTo(null);
            rahmen.setVisible(true);
            rahmen.setResizable(false);
            rahmen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            rahmen.setLayout(null);
            rahmen.setTitle("Gioco");
            Label label = new Label(); //<<this label is your extended label (i.e com.demo.test.stack.Label)and not awt label 
            label.setVisible(true);
            label.setBounds(0, 0, 800, 600);
            rahmen.add(label);

        }
    }

下面是包结构,也要注意导入中的包

Let me know if you ave any other queries.如果您有任何其他疑问,请告诉我。

Regards and welcome to SO.问候并欢迎来到 SO。 Cheers干杯

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

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