Java JFrame pack() 问题

发布于 2024-08-24 07:23:10 字数 1651 浏览 10 评论 0原文

我正在尝试使用标准 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>

替代文本 http://img186.imageshack.us/img186/1082/image2ll .jpg

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2024-08-31 07:23:10

我想把窗户打开
最初与背景图像
占据整个窗口
空间。

然后只需创建一个带有图标的 JLabel 并使用该标签作为框架的内容窗格。当您打包框架时,它将假定图像的大小。

然后我想要第二张图片
显示在顶部的位置
我指定。

对上述标签使用空布局。现在,您可以使用图标创建其他 JLabel,并将它们添加到内容窗格并使用 setBounds 放置它们。

I'd like to have the window open
initially with the background image
taking up the entirety of the window's
space.

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.

I'd then like to have the second image
displayed on top, at the location that
I specify.

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.

薄凉少年不暖心 2024-08-31 07:23:10

您可以将背景面板的首选大小设置为图像的大小:

backgroundPanel.setPreferredSize(new Dimension(
    backgroundImage.getIconWidth(), backgroundImage.getIconHeight()));

我建议遵循@camickr 的方法。我自己之前没有尝试过 setBounds(),这里有一个简单的例子来看看效果:

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

public class ImageTest {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ElliptIcon(380, 260, Color.red));
        label.setLayout(new GridLayout(2, 2));
        frame.setContentPane(label);

        for (int i = 0; i < 4; i++) {
            label.add(new JLabel(new ElliptIcon(100, 60, Color.blue)));
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private static class ElliptIcon implements Icon {

        private int w, h;
        private Color color;

        public ElliptIcon(int w, int h, Color color) {
            this.w = w;
            this.h = h;
            this.color = color;
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(color);
            g.fillOval(x, y, w, h);
        }

        @Override
        public int getIconWidth() { return w; }

        @Override
        public int getIconHeight() { return h; }
    }
}

You can set the preferred size of the background panel to the image's size:

backgroundPanel.setPreferredSize(new Dimension(
    backgroundImage.getIconWidth(), backgroundImage.getIconHeight()));

I would advocate following @camickr's approach. Not having previously experimented with setBounds() myself, here is a simple example to see the effect:

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

public class ImageTest {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ElliptIcon(380, 260, Color.red));
        label.setLayout(new GridLayout(2, 2));
        frame.setContentPane(label);

        for (int i = 0; i < 4; i++) {
            label.add(new JLabel(new ElliptIcon(100, 60, Color.blue)));
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private static class ElliptIcon implements Icon {

        private int w, h;
        private Color color;

        public ElliptIcon(int w, int h, Color color) {
            this.w = w;
            this.h = h;
            this.color = color;
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(color);
            g.fillOval(x, y, w, h);
        }

        @Override
        public int getIconWidth() { return w; }

        @Override
        public int getIconHeight() { return h; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文