如何在CardLayout中为所有面板设置单个背景? [英] How to set a single background for all panels in CardLayout?

查看:126
本文介绍了如何在CardLayout中为所有面板设置单个背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CardLayout& ;;在Netbeans中设计一个应用程序.我想对所有JPanel对象使用相同的背景.

I am designing an application in Netbeans using CardLayout & I want to use the same background for all of my JPanel objects.

我已经尝试使用如何向JPanel添加图像?,但是我还没有弄清楚如何在所有面板中使用相同的图像.

I have already tried to accomplish what I want using the method discussed in How to add an image to a JPanel?, however I haven't figured out how to use the same image for all of my panels.

推荐答案

如何为CardLayout中的所有面板设置单个背景.

How to set a single background for all panels in CardLayout..

使它们全部透明.

这张图片是煤矿的一张照片......

This image is a picture of a coal mine ..at midnight.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class BackgroundImageForCardLayout {

    private JComponent ui = null;
    private Image image= new BufferedImage(400,300,BufferedImage.TYPE_INT_RGB);

    BackgroundImageForCardLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            }
        };

        final CardLayout cl = new CardLayout();
        ui.setLayout(cl);

        final String[] names = {"Card 1", "Card 2", "Card 3"};
        for (String name : names) {
            JPanel p = new JPanel(new GridLayout());
            p.setOpaque(false);
            p.add(getBigColoredLabel(name));
            p.setOpaque(false);
            ui.add(p, name);
        }
        ActionListener animationListener = new ActionListener() {

            int i=0;

            @Override
            public void actionPerformed(ActionEvent e) {
                String name = names[i%names.length];
                cl.show(ui, name);
                i++;
            }
        };
        Timer timer = new Timer(750, animationListener);
        timer.start();
    }

    private final JLabel getBigColoredLabel(String text) {
        JLabel l = new JLabel(text, SwingConstants.CENTER);
        l.setForeground(Color.RED);
        Font f = l.getFont();
        l.setFont(f.deriveFont(120f));
        return l;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                BackgroundImageForCardLayout o = 
                        new BackgroundImageForCardLayout();

                JFrame f = new JFrame("Background Image for CardLayout");
                // Hack to terminate the Timer on close.
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

这篇关于如何在CardLayout中为所有面板设置单个背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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