如何添加缓冲图像作为 JFrame 的背景,然后在该图

how to add a buffered image as back ground of JFrame then add a panel on this image?(如何添加缓冲图像作为 JFrame 的背景,然后在该图像上添加面板?)

本文介绍了如何添加缓冲图像作为 JFrame 的背景,然后在该图像上添加面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在计算机工程的第二学期.

I'm in the second term of Computer engineering .

我的问题是如何在背景图片上添加JButton等,你知道我已经写了下面的代码,请帮我继续:正如我所说,我的 JBotton 无法在图像上显示,这就是问题所在.

my problem is how to add JButton etc... on the background image ,you know I have written the below code , please help me to continue: as I said my JBotton can't be shown on the image and here is the problem.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

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

public class MyCalcFrame extends JFrame
{
    private BufferedImage myImage;
    private JPanel mypanel;
    private JButton mybtn;

    public MyCalcFrame()
    {
        this.setBounds(410, 110, 600, 450);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setResizable(false);
        this.setAlwaysOnTop(true);



        try
        {
            this.myImage=ImageIO.read(new File("D:\1.jpg"));
        }//end try
        catch(IOException e)
        {
            JOptionPane.showMessageDialog(null, "Image dose not exist.","NO Image found",JOptionPane.ERROR_MESSAGE);
        }//end catch
        this.mypanel=new JPanel();
        this.mypanel.setBackground(Color.black);
        this.setContentPane(new ImagePanel(myImage));
        mybtn=new JButton("hello");
        this.getContentPane().add(mybtn);

        this.setVisible(true);
    }//end MyCalcFrame constructor

    class ImagePanel extends JComponent 
    {
        private Image image;

        public ImagePanel(Image image)
        {
            this.image = image;
        }//end constructor
        @Override
        protected void paintComponent(Graphics g)
        {
            g.drawImage(image, 0, 0, null);
        }//en paintComponent
    }//end ImagePanel
    //################ End constructor ########################
    //public void paint(Graphics g)
    //{
    //  g.drawImage(myImage, 0, 0, this);
    /

本文标题为:如何添加缓冲图像作为 JFrame 的背景,然后在该图

基础教程推荐