Java:在调整窗口大小之前图像不会显示
我正在使用 java 进行编程,并且正在研究 GUI 和图形。在我的程序中,我将图像绘制到 JPanel 上并将 JPanel 添加到主窗口。我遇到的问题是,当我运行程序时,图像不会显示,直到我手动调整窗口大小。以下是相关代码:
绘制图像的位置:
public class painting extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.drawImage(Toolkit.getDefaultToolkit().getImage("image.png"), 0, 0, null);
}
}
将 JPanel 添加到 JFrame 的位置(c 是 GridBagConstraints):
public class GUI extends JFrame{
public GUI(){
painting Pnt = new painting();
c.gridx = 1; c.gridy = 0;
c.ipadx = 540; c.ipady = 395;
add(Pnt, c);
}
}
设置窗口的位置:
public class MainC{
public static void main (String args[]){
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);
gui.setTitle("Title");
}
}
谢谢, Bennett
编辑:我注意到它有时会正确显示图像,但如果我关闭程序并重试,它就无法工作,直到我调整它的大小。
I am programming in java and I'm getting into GUIs and Graphics. In my program I paint an image onto a JPanel and add the JPanel to the main window. The Problem I'm having is when I run the program the image doesn't show until I manually resize the window. Here is the relevant code:
Where the image is drawn:
public class painting extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.drawImage(Toolkit.getDefaultToolkit().getImage("image.png"), 0, 0, null);
}
}
Where JPanel is added to JFrame (c is GridBagConstraints):
public class GUI extends JFrame{
public GUI(){
painting Pnt = new painting();
c.gridx = 1; c.gridy = 0;
c.ipadx = 540; c.ipady = 395;
add(Pnt, c);
}
}
Where The Window is set up:
public class MainC{
public static void main (String args[]){
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);
gui.setTitle("Title");
}
}
Thanks,
Bennett
EDIT: I noiced that it sometimes displays the image correctly but then if I close the program and try again and it doesn't work until I resize it.
EDIT2: Here are the files GUI class, MainC class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Toolkit.getImage()
异步工作。使用ImageIO.read()
或添加MediaTracker
。Toolkit.getImage()
works asynchronously. Either useImageIO.read()
or add aMediaTracker
.在
GUI
构造函数中,您应该先调用 super() 。同时移动:
在 GUI 构造函数中。
In
GUI
constructor you should call super() before anything else.Also move:
in GUI constructor.
您的问题一定出在代码的其他地方。我将您的三个类复制并粘贴到我的 IDE 中。我唯一改变的是在 GUI.java 中我添加了
setLayout(new GridBagLayout());
和GridBagConstraints c = new GridBagConstraints();
并且我更改了图像为我自己的一张。该窗口按预期工作,并立即显示图像。我假设您还有此处未显示的其他代码,例如我添加的 c 的初始化。检查您的其他代码以确保您最初没有重绘图像。另外,如果您发布其他代码,我可以帮助您找出问题
Your problem must be somewhere else in your code. I copied and pasted your three classes into my IDE. The only thing I changed was in GUI.java I added
setLayout(new GridBagLayout());
andGridBagConstraints c = new GridBagConstraints();
and I changed the location of the image to one of my own. The window works as expected with the image displayed right away.I assume you have additional code not displayed here, such as the initialization of c that I added. Check your other code to make sure you are not redrawing over your image initially. Also, if you post the other code I could help you identify the problem
吗
会给你带来图片 ?
我尝试过:
它有效。
does
brings you the picture?
I tried it with:
and it works.