How to show a label as the frame background in java? [closed]

主宰稳场 提交于 2019-12-12 01:52:38

问题


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,


回答1:


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.

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.

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




回答2:


1.Just add a label to your JFram form.

2.In the GUI Designer, select the label that you have added to your form.

3.In the Properties window, click the Properties category and scroll to the Icon property.

4.Click the ellipsis (...) button.

5.Then click on external image . So that it will add to your project ,This is with netbeans iDE

If you want more help inform me




回答3:


We have two methods in here

  1. Set Background Image in Netbeans

  2. Otherway of doing :

Override the paintComponent method to draw a background image on each time the JPanel is refreshed.

For example, one would subclass a JPanel, and add a field to hold the background image, and override the paintComponent method:

public class JPanelBackground extends JPanel {

  private Image backgroundImage;

  // Here, we use the constructor to load the image.
  public JPanelBackground(String fileName) throws IOException {
    backgroundImage = ImageIO.read(new File(fileName));
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    // Draw the background image.
    g.drawImage(backgroundImage, 0, 0, this);
  }
}

The following code could be used to add the JPanelBackground into a JFrame:

JFrame f = new JFrame();
f.getContentPane().add(new JPanelBackground("sample.jpeg"));

In this example, the ImageIO.read(File) ( details About ImageIO [Check] )(http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html) method was used to read external JPEG file.



来源:https://stackoverflow.com/questions/29271596/how-to-show-a-label-as-the-frame-background-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!