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

查看:26
本文介绍了如何添加缓冲图像作为 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);
    //}//end method paint

    //@@@@@@@@@@@@@@@@@@@@@@@@ main @@@@@@@@@@@@@@@@@@@@@@@@@@@@
    public static void main(String[] args)
    {
        //JFrame.setDefaultLookAndFeelDecorated(true);
        new MyCalcFrame();
    }//end method main
    //@@@@@@@@@@@@@@@@@@@@@@@@ main @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
}//end class MyCalcFrame

推荐答案

JComponent 不使用布局管理器,因此当您添加按钮时它不会显示.

A JComponent doesn't use a Layout Manager so when you add the button it doesn't display.

尝试在您的组件上使用 FlowLayout.

Try using a FlowLayout on your component.

另外,不要将 setBounds() 用于您的框架.您应该 pack() 然后使用 setLocationByPlatform(true),以便框架以其首选大小显示.

Also, don't use setBounds() for your frame. You should pack() and then use setLocationByPlatform(true), so the frame is displayed at its preferred size.

您需要为您的组件实现 getPreferredSize() 才能正常工作.

You would need to implement getPreferredSize() for your component so this works.

这篇关于如何添加缓冲图像作为 JFrame 的背景,然后在该图像上添加面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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