简体   繁体   English

如何在Eclipse或Windows Builder Pro中的Java应用程序中将图像设置为背景

[英]How to set an image as background in Java application in eclipse or windows builder pro

I was wondering how to set an image as the background to an application in Java. 我想知道如何将图像设置为Java中应用程序的背景。 I know in android it is very straight forward, and windows builder pro has a lot of amazing tools for building the Java gui so was wondering whether there was a way I could do this? 我知道在android中这是非常简单的,并且Windows Builder pro有很多很棒的工具来构建Java gui,所以想知道是否有办法可以做到这一点? Thanks in advance! 提前致谢! MY application looks pretty bad as grey... 我的应用看起来像灰色一样糟糕...

You can't set the background to an image exactly. 您无法将背景准确设置为图像。 What you have to do is draw the image on the graphics during painting. 您要做的是在绘画过程中在图形上绘制图像。 So you'll need to subclass JPanel and override the paintComponent() method, and draw the image there. 因此,您需要子类化JPanel并重写paintComponent()方法,并在那里绘制图像。

 public class ImagePanel extends JPanel {
     private Image bgImage;

     public Image getBackgroundImage() {
        return this.bgImage;
     }

     public void setBackgroundImage(Image image) {
        this.bgImage = image;
     }

     protected paintComponent(Graphics g) {
         super.paintComponent(g);
         g.drawImage( bgImage, 0, 0, bgImage.getWidth(null), bgImage.getHeight(null), null );
     }
 }

You can set your component's color by calling: 您可以通过调用以下命令设置组件的颜色:

.setBackground(myColor);

Some components such as JLabels require you to call this upon it for the color change to take effect: 某些组件(例如JLabels)要求您对其调用以使颜色更改生效:

.setOpaque(true);

Hope this helped. 希望这会有所帮助。

Technically you could add a label on the whole screen and then in the icon option chane to specific background: 从技术上讲,您可以在整个屏幕上添加标签,然后在图标选项中添加特定背景:

JLabel lblNewLabel = new JLabel("n");
lblNewLabel.setIcon(new ImageIcon("gene.jpg"));
lblNewLabel.setBounds(0, 0, 434, 261);
frame.getContentPane().add(lblNewLabel);

For example, in the Source 例如,在来源中

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("Let's start!");
    btnNewButton.setFont(new Font("David", Font.ITALIC, 12));
    btnNewButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e) 
        {
            JOptionPane.showMessageDialog(null, "Now we will have to add some code here right =]?");
        }
    }
                                                        );
    btnNewButton.setBounds(158, 120, 89, 23);
    frame.getContentPane().add(btnNewButton);


    JLabel lblNewLabel = new JLabel("n");
    lblNewLabel.setIcon(new ImageIcon("gene.jpg"));
    lblNewLabel.setBounds(0, 0, 434, 261);
    frame.getContentPane().add(lblNewLabel);
}

http://imgur.com/rSTXZLP.jpg http://imgur.com/rSTXZLP.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM