在Java中添加背景图像

时间:2010-09-03 00:15:37

标签: java background

如何在此代码上添加背景图片?我尝试了一切,但图像不会显示。

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();

}


}

3 个答案:

答案 0 :(得分:1)

这可能有你想要的答案:How to set background image in Java?

答案 1 :(得分:1)

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

在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);
    }
}

答案 2 :(得分:0)

Background Panel根据您的要求提供了几种解决方案。