在按钮操作上向JPanel添加背景图像 [英] Adding background image to JPanel on button action

查看:154
本文介绍了在按钮操作上向JPanel添加背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用JButton时,将背景图像添加到JPanel / JLabel的最佳方法是什么?我知道如何获得JButton动作等。当按下该按钮时,我无法弄清楚或找到一种方法来改变背景图像。

What is the best way to add a background image to a JPanel/JLabel when a JButton is called? I know how to get the JButton action and such. I just can't figure out or find a way to get the background image to change when that button is pressed.

推荐答案

这里是一个例子:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class ModifiableBackgroundFrame extends JFrame implements ActionListener
{
    private static final long serialVersionUID = 1L;
    private ImageIcon image;
    private JPanel pan;
    private JButton btn;
    private int count = 0;
    private static final String[] images = 
        {"http://www.dvd-ppt-slideshow.com/images/ppt-background/background-3.jpg",
        "http://www.psdgraphics.com/wp-content/uploads/2009/02/abstract-background.jpg",
        "http://hdwallpaperpics.com/wallpaper/picture/image/background.jpg",
        "http://www.highresolutionpics.info/wp-content/uploads/images/beautiful-on-green-backgrounds-for-powerpoint.jpg"};

    public ModifiableBackgroundFrame()
    {
        super("The title");

        image = new ImageIcon();

        btn = new JButton("Change background");
        btn.setFocusPainted(false);
        btn.addActionListener(this);
        pan = new JPanel()
        {
            private static final long serialVersionUID = 1L;

            @Override
            public void paintComponent(Graphics g)
            {
                g.drawImage(image.getImage(), 0, 0, null);
            }
        };
        pan.setPreferredSize(new Dimension(400, 400));

        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(pan, BorderLayout.CENTER);
        contentPane.add(btn, BorderLayout.SOUTH);

        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ModifiableBackgroundFrame();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        btn.setEnabled(false);
        btn.setText("Loading...");
        new SwingWorker<Image, Void>()
        {
            @Override
            protected Image doInBackground() throws Exception
            {
                return ImageIO.read(new URL(images[count++ % 4]));
            }

            @Override
            protected void done()
            {
                try
                {
                    image.setImage(get());
                    pan.repaint();
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                catch(ExecutionException e)
                {
                    e.printStackTrace();
                }
                btn.setText("Change background");
                btn.setEnabled(true);
            }
        }.execute();
    }
}

这篇关于在按钮操作上向JPanel添加背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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