简体   繁体   English

JButton在背景图片后面

[英]JButton behind background image

public class main extends JFrame {
    JPanel panel = new JPanel();
    JButton playGame = new JButton("PLAY GAME");
    public void paint(java.awt.Graphics g) {
        super.paint(g);
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File("./src/Images/menu.jpg"));
        } catch (IOException e) {

            e.printStackTrace();
        }
        g.drawImage(image, 0, 0, 1000, 600, this);
    }

    public main(){
        super();

        playGame.setBounds(390, 250, 220, 30);
        //panel.setBounds(80, 800, 200, 100);
        panel.setLayout(null);
        panel.add(playGame);
        add(panel);

        setTitle("MENU");
        setSize(1000,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        //setLayout(new FlowLayout());
        setVisible(true);
    }

    public static void main(String[] args) {
        new main();
}
}

I am trying to add my JButton over the image but it is coming behind the image. 我试图在图像上添加我的JButton ,但是它在图像后面。

The problem is I don't know how to add a background picture so that the buttons appear at the top of the picture. 问题是我不知道如何添加背景图片,以使按钮出现在图片的顶部。 Is there any for me set up a background picture so that the other panels show up on top as well? 我是否需要设置背景图片,以便其他面板也显示在顶部?

Try to add you background image to your panel, and don't load your image from src folder because after compiling your classes will be created in bin folder, so use class.getResource() which gives you relative url to your image; 尝试将背景图片添加到面板中,不要从src文件夹中加载图片,因为编译后,您的类将在bin文件夹中创建,因此请使用class.getResource(),它会为您提供图片的相对网址; another thing you should never override JFrame paint method because painting on JFrame is applied also over frame border, so try always to paint on a panel inside your frame by overriding paintComponent method of JPanel class: 另一件事是您永远不应覆盖JFrame绘制方法,因为在JFrame上进行绘制也将应用在框架边框上,因此,请始终尝试通过重写JPanel类的paintComponent方法在框架内的面板上进行绘制:

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class main extends JFrame
{
    JButton playGame = new JButton("PLAY GAME");
    JPanel panel = new JPanel()
    {
        public void paintComponent(java.awt.Graphics g)
        {
            super.paintComponent(g);
            BufferedImage image = null;
            try
            {
                image = ImageIO.read(main.class.getResource("/Images/menu.jpg"));
            }
            catch (IOException e)
            {

                e.printStackTrace();
            }
            g.drawImage(image, 0, 0, 1000, 600, this);
        }
    };

    public main()
    {
        super();

        playGame.setBounds(390, 250, 220, 30);
        // panel.setBounds(80, 800, 200, 100);
        panel.setLayout(null);
        panel.add(playGame);
        add(panel);

        setTitle("MENU");
        setSize(1000, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        // setLayout(new FlowLayout());
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new main();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM