java gui图像问题: doesn't display in the background

标签 java image user-interface swing

我有一个疑问,为什么我的图像没有显示在我的程序背景中。我的意思是我做了所有必要的步骤,但它仍然不会显示。代码运行完美,但没有显示图像。该目录写在图像的好位置。我正在使用 java 和 gui。如果有人可以帮助我解决我的问题,我将不胜感激:) 这是下面的代码:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*;

public class hehe extends JPanel{

    public hehe(){

            setOpaque(false);
            setLayout(new FlowLayout()); 
          }

    public static void main (String args[]){
           JFrame win = new JFrame("yooooo"); // it is automaticcally hidden

           JPanel mainPanel = new JPanel(new BorderLayout()); 
                win.add(mainPanel);

     JLabel titleLabel = new JLabel("title boss"); 
           titleLabel.setFont(new Font("Arial",Font.BOLD,18)); 
           titleLabel.setForeground(Color.blue);
           mainPanel.add(titleLabel,BorderLayout.NORTH);

           win.setSize(382,269); // the dimensions of the image
           win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           win.setVisible(true);
        }


            public void paint(Graphics g) {

                      Image a = Toolkit.getDefaultToolkit().getImage("C:\\Users\\andrea\\Desktop\\Gui\\car"); // car is the name of the image file and is in JPEG
                g.drawImage(a,0,0,getSize().width,getSize().height,this);
                super.paint(g);
            }
            }

最佳答案

在您提供的代码中,main() 方法中没有引用 hehe 类。我猜您会想要创建该对象并将其添加到您的窗口中。

您还需要在文件名中包含 .jpg 扩展名

关于java gui图像问题: doesn't display in the background,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4603948/

相关文章:

java - 如何在非 Activity 类中使用 Activity 类中的函数

java - cvtColor 在 OpenCV for Java 中无法正常工作

html - 签名中的嵌入图像未显示

user-interface - Wix UI 中密码文本框的奇怪双框

JavaFx 和套接字监听器

java - 如何让 JAXB 选择枚举?

java - 在 Netbeans 8 上配置 JBoss EAP 的问题

Swift - 旋转图像,然后放大并裁剪以保持宽度/高度纵横比不变

image - 将图像插入标签

java - 为什么我的启动画面没有显示?