gpt4 book ai didi

java - 在Java中添加背景图片

转载 作者:行者123 更新时间:2023-11-30 05:10:20 28 4
gpt4 key购买 nike

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

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

}


}

最佳答案

您的代码永远不会设置 img 字段。将代码从 void SampleProg() 方法移动到构造函数中。您还应该将 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);
}
}

关于java - 在Java中添加背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3632211/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com