Java背景JFrame与Jpanel在网格中排列图像 [英] Java background JFrame with a Jpanel arranging images in a grid

查看:72
本文介绍了Java背景JFrame与Jpanel在网格中排列图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在研究程序,但是我很难用网络图的背景图像来设置JFrame,然后使用JFrame顶部的MigLayout设置JPanel 这将允许我根据网络地图放置图像.我已经看到了几种不同的方法来设置背景图像,并尝试使用在此站点上其他地方找到的示例,但效果不佳.另外,我对此进行了更多研究,发现在JFrame的顶部具有JPanel可能会阻止看到JFrame背景图像.我希望有人可以看一下我的代码,然后让我知道是否是这种情况.

so I've been working on my program and I am having trouble getting setting up a JFrame with a background image of a network map and then setting a JPanel using a MigLayout on top of the JFrame which will allow me to place images in accordance to the network map. I have seen several different ways on how to set a background image and have tried using an example I found elsewhere on this site but it's not quite working. Also, I researched more on this and found that having a JPanel ontop of a JFrame may prevent the JFrame background image from being seen. I was hoping that someone could take a look at my code and let me know if that is the case.

这是我班的背景:

public class bwMonBackground extends JFrame {
    Image img;
    public bwMonBackground() {
        try {
            img = ImageIO.read(new File("/Users/pwopperer/workspace/BWmon/src/customer_vlans.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
public void paintComponent(Graphics g)
{
    super.paintComponents(g);
    g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}

}

这是我的主要爱好:

public class bwMonBackgroundMain {

    public static void main( String[] args )
        {
            bwMonBackground frame = new bwMonBackground();
            frame.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
            migLayout testing = new migLayout();
            frame.add(testing.createLayout());
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();

        }
}

当我像这样运行它时,我只会获得一个JPanel,其中包含我添加的6个JLabel

When I run it like this, I only get a JPanel with the 6 JLabels I added into it

推荐答案

有点挑剔,但是除非有充分的理由,否则应该始终致电super.paintComponent.

It's been a little nit picky, but you should always call super.paintComponent unless you have a REALLY, REALLY good reason not to.

此外,您应在可能的情况下为Graphics.drawImage提供一个ImageObserver.基本上,这意味着如果图像处理是在另一个线程中进行的,则ImageObserver不会通知更改并自动更新;)

Also, you should provide Graphics.drawImage with a ImageObserver where possible. Basically this means if the image processing is taking place in another thread, the ImageObserver will not notified of changes and updated automatically ;)

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g.drawImage(img, 0, 0, this);
}

将组件放置到地图上时,需要确保它们是透明的(JComponent.setOpaque(false)).这样可以使背景显示出来.

When placing components onto your map, you need to make sure they a transparent (JComponent.setOpaque(false)). This will allow the background to show through.

很明显,您可以将某些组件选择为不透明的,以使它们更易于阅读或提供AlphaComposite来提供半透明效果.

Obviously, you could choose some components to be opaque so they are more easily readable or provide AlphaComposites to provide translucent effects.

其他想法

我不会以这种方式覆盖JFrame.框架具有内容窗格,这些窗格负责核心内容.

I wouldn't override JFrame in this manner. Frames have content panes, which are responsible for the core content.

此外,JFrame从内存中没有paintComponent方法:P

Besides, from memory, JFrame doesn't have a paintComponent method :P

我会改写JPanel(或JComponent)并使用paintComponent方法,然后将该面板添加到框架内容窗格或将其设置为内容窗格本身(frame.setContentPane(...)-请当心,您可以在向框架添加任何其他组件之前进行此操作,因为这将有效地将它们从用户界面中删除;))

I would instead override JPanel (or JComponent) and user there paintComponent method instead, then either add this panel to the frames content pane or set it as the content pane itself (frame.setContentPane(...) - just beware, you do this BEFORE adding any other components to the frame, as this will effectively remove them from the UI ;))

这篇关于Java背景JFrame与Jpanel在网格中排列图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆