Java JFrame pack() 问题
我正在尝试使用标准 java 实用程序将图像覆盖在背景图像之上。请参阅下面的图片...
我有似乎可以创建背景图像的代码(您能验证它是否真的有效吗?) 我创建了 JPanel 的扩展,用于显示图像(该类称为 ImagePanel)。
但是,当程序启动时,JFrame 仅显示第二个图像,然后随着窗口大小的调整而移动。
我想让窗口最初打开,背景图像占据窗口的整个空间。然后,我希望将第二张图像显示在顶部我指定的位置。
import javax.swing.*;
import java.awt.*;
public class ImageTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
JPanel backgroundPanel = new JPanel();
ImageIcon backgroundImage = new ImageIcon("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\TableAndChairs.png");
JLabel background = new JLabel(backgroundImage);
background.setBounds(0, 0, backgroundImage.getIconWidth(), backgroundImage.getIconHeight());
frame.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
backgroundPanel.setOpaque(false);
frame.setContentPane(backgroundPanel);
ImagePanel button = new ImagePanel("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\button.png");
JPanel cardContainer = new JPanel(new FlowLayout());
frame.getContentPane().add(cardContainer);
cardContainer.add(button);
cardContainer.setBounds(100, 600, 200, 200);
frame.pack();
frame.setVisible(true);
}
}
替代文本 http://img189.imageshack.us/img189/9739/image1qi.jpg< /a>
I am attempting to overlay images on top of a background image using the standard java utilities. See the pictures below...
I have code that seems to create the background image (can you verify that it really works?)
And I have created an extension of JPanel that I use to display images (the class is called ImagePanel)
However, when the program launches, the JFrame is only showing the second image, which then gets moved around as the window is resized.
I'd like to have the window open initially with the background image taking up the entirety of the window's space. I'd then like to have the second image displayed on top, at the location that I specify.
import javax.swing.*;
import java.awt.*;
public class ImageTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
JPanel backgroundPanel = new JPanel();
ImageIcon backgroundImage = new ImageIcon("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\TableAndChairs.png");
JLabel background = new JLabel(backgroundImage);
background.setBounds(0, 0, backgroundImage.getIconWidth(), backgroundImage.getIconHeight());
frame.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
backgroundPanel.setOpaque(false);
frame.setContentPane(backgroundPanel);
ImagePanel button = new ImagePanel("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\button.png");
JPanel cardContainer = new JPanel(new FlowLayout());
frame.getContentPane().add(cardContainer);
cardContainer.add(button);
cardContainer.setBounds(100, 600, 200, 200);
frame.pack();
frame.setVisible(true);
}
}
alt text http://img189.imageshack.us/img189/9739/image1qi.jpg
alt text http://img186.imageshack.us/img186/1082/image2ll.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然后只需创建一个带有图标的 JLabel 并使用该标签作为框架的内容窗格。当您打包框架时,它将假定图像的大小。
对上述标签使用空布局。现在,您可以使用图标创建其他 JLabel,并将它们添加到内容窗格并使用 setBounds 放置它们。
Then simply create a JLabel with an Icon and use the label as the content pane of the frame. When you pack the frame it will be assume the size of the image.
Use a null layout for the above label. Now you can create additional JLabels with Icons and add them to the content pane and postion them using setBounds.
您可以将背景面板的首选大小设置为图像的大小:
我建议遵循@camickr 的方法。我自己之前没有尝试过 setBounds(),这里有一个简单的例子来看看效果:
You can set the preferred size of the background panel to the image's size:
I would advocate following @camickr's approach. Not having previously experimented with
setBounds()
myself, here is a simple example to see the effect: