gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:07:24 26 4
gpt4 key购买 nike

我有一个疑问,为什么我的图像没有显示在我的程序背景中。我的意思是我做了所有必要的步骤,但它仍然不会显示。代码运行完美,但没有显示图像。该目录写在图像的好位置。我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com