如何在Java中将标签显示为框架背景? [英] How to show a label as the frame background in java?

查看:95
本文介绍了如何在Java中将标签显示为框架背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Java应用程序,我需要取消装饰框架并使用图像作为背景.我使用此代码来做到这一点

i'm developing a java application,i need to undecorate frame and use a image as the background.i used this code to do this

 com.sun.awt.AWTUtilities.setWindowOpaque(this, false)

但是,如果我使用此字母,则显示不正确. 还有另一种方法吗?请帮我解决这个问题,

but if i use this letters are not showing correctly. Is there another way to do this? Please help me to solve this problem,

推荐答案

您的问题是模糊的,但是,除非您使用Java 6,否则我将避免使用com.sun.awt.AWTUtilities.setWindowOpaque(this, false),而是使用新的透明性支持在Java 7+中.有关更多详细信息,请参见如何创建半透明和异形窗口.

Your question is on the vague side, however, unless your using Java 6, I would avoid using com.sun.awt.AWTUtilities.setWindowOpaque(this, false) and instead use the new transparency support which is available in Java 7+. See How to Create Translucent and Shaped Windows for more details.

不要使用JLabel作为内容的背景,它不会根据其子组件来计算其首选大小,而只能根据icontext属性来计算.这意味着它可以小于正确布局其子组件所需的空间.

Don't use a JLabel as a background for content, it doesn't calculate it's preferred size based on it's child components, but solely on the icon and text properties. This means that it can be smaller then space required to properly layout it's child components.

相反,请使用自定义的JPanel并自己绘制图像.

Instead, use a customised JPanel and paint the image yourself.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JLabel label = new JLabel("Boo!");
                label.setForeground(Color.WHITE);
                label.setHorizontalAlignment(JLabel.CENTER);
                label.setFont(label.getFont().deriveFont(Font.BOLD, 128f));

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new BackgroundPane());
                frame.add(label);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class BackgroundPane extends JPanel {

        private BufferedImage background;

        public BackgroundPane() {
            setLayout(new BorderLayout());
            try {
                background = ImageIO.read(new File("Some image"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            Dimension size = super.getPreferredSize();
            if (background != null) {
                size.width = Math.max(size.width, background.getWidth());
                size.height = Math.max(size.height, background.getWidth());
            }
            return size;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g2d.drawImage(background, x, y, this);
                g2d.dispose();
            }
        }

    }

}

请参见读取/加载图像执行自定义绘画以获取更多详细信息

See Reading/Loading an Image and Performing Custom Painting for more details

这篇关于如何在Java中将标签显示为框架背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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