java添加背景图片

这是我现在的代码,我想在JPANEL中添加该类同路径的ALL.JPG图片作为背景,请高手指点该如何更改可以实现.importjava.awt.*;importjavax.... 这是我现在的代码,我想在JPANEL中添加该类同路径的ALL.JPG图片作为背景,请高手指点该如何更改可以实现.
import java.awt.*;
import javax.swing.*;
public class 登陆界面 extends JPanel {
private static final long serialVersionUID = 1L;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField jTextField = null;
private JPasswordField jPasswordField = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(140, 30, 160, 30));
}
return jTextField;
}
private JPasswordField getJPasswordField() {
if (jPasswordField == null) {
jPasswordField = new JPasswordField();
jPasswordField.setBounds(new Rectangle(140, 80, 160, 30));
}
return jPasswordField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(330, 30, 70, 30));
jButton.setText("登陆");
}
return jButton;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(330, 80, 70, 30));
jButton1.setText("注册");
}
return jButton1;
}
public static void main(String[] args) {
}
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("all.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(),this);
}
}
public 登陆界面() {
super();
initialize();
}
private void initialize() {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(45, 80, 70, 30));
jLabel1.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("密码");
jLabel = new JLabel();
jLabel.setText("用户名");
jLabel.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel.setBounds(new Rectangle(45, 30, 70, 30));
this.setSize(601, 410);
this.setLayout(null);
this.add(jLabel, null);
this.add(jLabel1, null);
this.add(getJTextField(), null);
this.add(getJPasswordField(), null);
this.add(getJButton(), null);
this.add(getJButton1(), null);
}
}
展开
 我来答
可达鸭爱编程
2010-05-09
知道答主
回答量:13
采纳率:0%
帮助的人:20.7万
展开全部
overwrite的paintComponent可以这样写
public void paintComponent(Graphics g) {
super.paintComponent(g);
Image image=(new ImageIcon("all.jpg")).getImage();
g.drawImage(image, 0, 0, this);
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式