在 Java 中添加背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3632211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:44:27  来源:igfitidea点击:

Adding background image in Java

javabackground

提问by laurize

How do I add background image on this code? i tried everything but the image just won't show.

如何在此代码上添加背景图片?我尝试了所有方法,但图像无法显示。

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

public class sampleProg extends JFrame {

private Image img;

public void sampleProg(){


ImageIcon icon = new ImageIcon("bg.jpg");

       img=icon.getImage();
}

public void paint (Graphics g){

    g.drawImage(img,0,0,getSize().width,getSize().height,this);
    super.paint(g);

}



JButton button = new JButton ("Result");
JButton button2 = new JButton ("Clear");


JLabel label = new JLabel ("Full Name");
JLabel label2 = new JLabel ("Age");
JLabel label3 = new JLabel ("English");
JLabel label4 = new JLabel ("Mathematics");
JLabel label5 = new JLabel ("Science");
JLabel label6 = new JLabel ("Social Studies");
JLabel label7 = new JLabel ("Height in cm");
JLabel label8 = new JLabel ("Weight in lbs");
JLabel label9 = new JLabel ("Message");
JLabel label10 = new JLabel ("Average");
JLabel label11 = new JLabel ("Remarks");
JLabel label12 = new JLabel ("Laurize Albarracin");



JTextField text = new JTextField ("");
JTextField text2 = new JTextField ("");
JTextField text3 = new JTextField ("");
JTextField text4 = new JTextField ("");
JTextField text5 = new JTextField ("");
JTextField text6 = new JTextField ("");
JTextField text7 = new JTextField ("");
JTextField text8 = new JTextField ("");
JTextField text9 = new JTextField ("");
JTextField text10 = new JTextField ("");
JTextField text11 = new JTextField ("");

int average;

JPanel background = new JPanel();
JFrame frame = new JFrame();

public sampleProg (String str){
    super(str);

    background.setLayout (null);
    background.setBounds (30,50,90,20);

    button.setBounds(350,170,90,20);
    button2.setBounds(450,170,90,20);


    label.setBounds(30,90,90,20);
    label2.setBounds(30,130,90,20);
    label3.setBounds(30,170,90,20);
    label4.setBounds(30,210,90,20);
    label5.setBounds(30,250,90,20);
    label6.setBounds(30,290,90,20);
    label7.setBounds(350,90,90,20);
    label8.setBounds(350,130,90,20);
    label9.setBounds(350,210,90,20);
    label10.setBounds(350,250,90,20);
    label11.setBounds(350,290,90,20);
    label12.setBounds(300,35,150,20);




    text.setBounds(130,90,190,20);
    text2.setBounds(130,130,190,20);
    text3.setBounds(130,170,190,20);
    text4.setBounds(130,210,190,20);
    text5.setBounds(130,250,190,20);
    text6.setBounds(130,290,190,20);
    text7.setBounds(450,90,250,20);
    text8.setBounds(450,130,250,20);
    text9.setBounds(450,210,250,20);
    text10.setBounds(450,250,250,20);
    text11.setBounds(450,290,250,20);

    button.addActionListener (new ActionListener () {
        public void actionPerformed (ActionEvent e){

            text9.setText("Hi! "+text.getText()+"you are"+text2.getText()+"years of age and now you stand"+text7.getText()+"in cm while you're weight is"+text8.getText()+"in lbs. These are your remark and average");
            int English = Integer.parseInt (text3.getText());
            int Mathematics = Integer.parseInt (text4.getText());
            int Science = Integer.parseInt (text5.getText());
            int SocialStudies = Integer.parseInt(text6.getText());

            average = (English+Mathematics+Science+SocialStudies)/4;
            text10.setText(Integer.toString(average)); 

        }   
    }
    );

    button2.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent e){
            text.setText("");
            text2.setText("");
            text3.setText("");
            text4.setText("");
            text5.setText("");
            text6.setText("");
            text7.setText("");
            text8.setText("");
            text9.setText("");
            text10.setText("");
            text11.setText("");

        }
    });

    background.add(button);
    background.add(button2);
    background.add(label);
    background.add(label2);
    background.add(label3);
    background.add(label4);
    background.add(label5);
    background.add(label6);
    background.add(label7);
    background.add(label8);
    background.add(label9);
    background.add(label10);
    background.add(label11);
    background.add(label12);

    background.add(text);       
    background.add(text2);
    background.add(text3);
    background.add(text4);
    background.add(text5);
    background.add(text6);
    background.add(text7);
    background.add(text8);
    background.add(text9);
    background.add(text10);
    background.add(text11);

    getContentPane().add(background);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pack();
     }

    public sampleProg(){

    setLayout(new FlowLayout());
}

     public static void main (String[]args){    

     sampleProg frame = new sampleProg("Swing Application");
     frame.setSize(730,350);
     frame.show();

}


}

采纳答案by Jeff Mercado

Your code never sets the imgfield. Move the code from the void sampleProg()method into your constructor. You should also set the backgroundpanel's backgroundto a transparent color for the image to show through. Also, you should paint the image on the backgroundpanel rather than on the frame, otherwise it will always be covered up. You can create an anonymous panel and move your paint()code into it instead.

您的代码从不设置该img字段。将void sampleProg()方法中的代码移到构造函数中。您还应该将background面板设置background为透明颜色,以便图像显示出来。此外,您应该在background面板上而不是在框架上绘制图像,否则它将始终被掩盖。您可以创建一个匿名面板并将您的paint()代码移入其中。

There are cleaner ways to do this found in the link Mondain provided.

在 Mondain 提供的链接中可以找到更简洁的方法。

public class sampleProg extends JFrame {
    JPanel background = new JPanel() {
        public void paint (Graphics g){
            g.drawImage(img,0,0,getSize().width,getSize().height,this);
            super.paint(g);
        }
    };

    public sampleProg(string str) {
        //...

        ImageIcon icon = new ImageIcon("bg.jpg");
        img=icon.getImage();

        //...

        Color transparent = new Color(0, true);
        background.setBackground(transparent);
    }
}

回答by Paul Gregtheitroade

This may have the answer you are looking for: How to set background image in Java?

这可能有您正在寻找的答案:How to set background image in Java?

回答by camickr

Background Panelgives a couple of solutions depending on your requirements.

背景面板根据您的要求提供了几种解决方案。