Java中如何给GUI添加背景图片,要求详细的代码,谢谢啦
importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener...
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Login implements ActionListener {
public static void main(String args[]){
new Login().face();
}
public void face(){
JFrame jf;
JLabel j1,j2;
JTextField jtf;
JPasswordField jd;
JButton jb1,jb2;
JPanel p1,p2,p3;
jf=new JFrame("登陆窗口");
j1=new JLabel("用户名");
j2=new JLabel("密 码");
jtf=new JTextField(15);
jd=new JPasswordField(15);
jb1=new JButton("确定");
jb2=new JButton("取消");
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
jf.setLayout(new GridLayout(3,1));
jf.add(p1);
jf.add(p2);
jf.add(p3);
p1.add(j1);
p1.add(jtf);
p2.add(j2);
p2.add(jd);
p3.add(jb1);
p3.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
jf.setVisible(true);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if(s.equals("确定")){
}
else{
System.exit(0);
}
}
} 展开
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Login implements ActionListener {
public static void main(String args[]){
new Login().face();
}
public void face(){
JFrame jf;
JLabel j1,j2;
JTextField jtf;
JPasswordField jd;
JButton jb1,jb2;
JPanel p1,p2,p3;
jf=new JFrame("登陆窗口");
j1=new JLabel("用户名");
j2=new JLabel("密 码");
jtf=new JTextField(15);
jd=new JPasswordField(15);
jb1=new JButton("确定");
jb2=new JButton("取消");
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
jf.setLayout(new GridLayout(3,1));
jf.add(p1);
jf.add(p2);
jf.add(p3);
p1.add(j1);
p1.add(jtf);
p2.add(j2);
p2.add(jd);
p3.add(jb1);
p3.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
jf.setVisible(true);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if(s.equals("确定")){
}
else{
System.exit(0);
}
}
} 展开
展开全部
package test.array;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login implements ActionListener {
public static void main(String args[]) {
new Login().face();
}
public void face() {
JFrame jf;
JLabel j1, j2;
JTextField jtf;
JPasswordField jd;
JButton jb1, jb2;
//JPanel p1, p2, p3;
jf = new JFrame("登陆窗口");
j1 = new JLabel("用户名");
j2 = new JLabel("密 码");
jtf = new JTextField(15);
jd = new JPasswordField(15);
jb1 = new JButton("确定");
jb2 = new JButton("取消");
// p1 = new JPanel();
// p2 = new JPanel();
// p3 = new JPanel();
JPanel p = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
try {
BufferedImage img = ImageIO.read(new File(this.getClass().getResource("1.jpg").getPath()));
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
} catch (IOException e) {
e.printStackTrace();
}
}
};
p.setLayout(null);
//p.add(p1);
//p.add(p2);
//p.add(p3);
j1.setBounds(50, 10, 60, 20);
p.add(j1);
jtf.setBounds(120, 10, 120, 20);
p.add(jtf);
j2.setBounds(50, 40, 60, 20);
p.add(j2);
jd.setBounds(120, 40, 120, 20);
p.add(jd);
jb1.setBounds(80, 70, 60, 20);
p.add(jb1);
jb2.setBounds(160, 70, 60, 20);
p.add(jb2);
jf.add(p);
jb1.addActionListener(this);
jb2.addActionListener(this);
jf.setBounds(300, 200, 280, 160);
jf.setResizable(false);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("确定")) {
} else {
System.exit(0);
}
}
}
追问
请问 BufferedImage img = ImageIO.read(new File(this.getClass().getResource("1.jpg").getPath()));这句里面的1.jpg应该放在哪里呢
追答
跟这个类同一个目录
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询