如何添加一个缓冲的形象JFrame的回地面,然后这个图像上添加面板? [英] how to add a buffered image as back ground of JFrame then add a panel on this image?

查看:160
本文介绍了如何添加一个缓冲的形象JFrame的回地面,然后这个图像上添加面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在电脑工程的第二个任期。

我的问题是如何增加一个JButton等...背景图像,你知道我已经写了下面code,请帮我继续说:
就像我说我的JBotton不能在图像上显示,这里是这个问题。

 进口java.awt.Color中;
进口java.awt.Graphics;
进口java.awt.Image中;
进口java.awt.image.BufferedImage中;
进口的java.io.File;
进口java.io.IOException异常;进口javax.imageio.ImageIO中;
进口javax.swing.JButton中;
进口javax.swing.JComponent中;
进口javax.swing.JFrame中;
进口javax.swing.JOptionPane中;
进口javax.swing.JPanel中;公共类MyCalcFrame扩展的JFrame
{
    私人的BufferedImage MYIMAGE;
    私人的JPanel mypanel;
    私人的JButton mybtn;    公共MyCalcFrame()
    {
        this.setBounds(410,110,600,450);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setResizable(假);
        this.setAlwaysOnTop(真);        尝试
        {
            this.myImage = ImageIO.read(新文件(D:\\\\ 1.JPG));
        } //结束试
        赶上(IOException异常E)
        {
            JOptionPane.showMessageDialog(NULL,图像剂量不存在的。,无图像发现,JOptionPane.ERROR_MESSAGE);
        } //抓到底
        this.mypanel =新JPanel();
        this.mypanel.setBackground(Color.black);
        this.setContentPane(新ImagePanel(MYIMAGE));
        mybtn =的新的JButton(你好);
        。this.getContentPane()加(mybtn);        this.setVisible(真);
    } //结束MyCalcFrame构造    类ImagePanel扩展JComponent的
    {
        私人形象画像;        公共ImagePanel(形象画像)
        {
            this.image =图像;
        } //结束构造
        @覆盖
        保护无效paintComponent(图形G)
        {
            g.drawImage(图像,0,0,NULL);
        } // EN的paintComponent
    } //结束ImagePanel
    // ################结束构造########################
    //公共无效漆(图形G)
    // {
    // g.drawImage(MYIMAGE,0,0,这一点);
    //} // end方法油漆    // @@@@@@@@@@@@@@@@@@@@@@@@主要@@@@@@@@@@@@@@@@@@@@@@@ @@@@@
    公共静态无效的主要(字串[] args)
    {
        //JFrame.setDefaultLookAndFeelDecorated(true);
        新MyCalcFrame();
    } // end方法主要
    // @@@@@@@@@@@@@@@@@@@@@@@@主要@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@
} //结束类MyCalcFrame


解决方案

一个JComponent不会当你添加它不会显示该按钮使用布局管理器等等。

尝试使用的FlowLayout 您的组件。

另外,不要对你的框架使用的setBounds()。你应该包(),然后用 setLocationByPlatform(真),所以框显示在其preferred大小。

您需要实现的get preferredSize()为组件所以此工程。

I'm in the second term of Computer engineering .

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

解决方案

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

Try using a FlowLayout on your component.

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.

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

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

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