jpanel添加背景图片问题

我想在jpanel内添加背景图片!添加成功后在下面小窗口能显示图片!可是运行出错!请高手指点指点!这是原代码!packageshitu;importjava.awt.Bo... 我想在jpanel内添加背景图片!添加成功后在下面小窗口能显示图片!可是运行出错!请高手指点指点!这是原代码!
package shitu;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import lei.db;
import java.awt.GridBagLayout;
import java.awt.Color;
import java.awt.SystemColor;
public class denglu extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JLabel jLabel1 = null;
private JPasswordField jPasswordField = null;
private JButton jButton = null;
private JLabel jLabel2 = null;
private JPanel jPanel = null;
private JLabel jLabel3 = null;
public denglu() {
super();
initialize();
}
private void initialize() {
this.setSize(513, 342);
this.setContentPane(getJContentPane());
this.setTitle("管理员登陆");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(143, 3, 129, 37));
JLabel lb = new JLabel(new ImageIcon(".\\123.jpg"));
lb.setBounds(new Rectangle(-1, 0, 505, 309));
jPanel.add(lb);
jLabel3.setText("");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(48, 247, 446, 26));
jLabel2.setText("");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(333, 149, 66, 24));
jLabel1.setText("密码:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(333, 98, 68, 28));
jLabel.setText("用户名:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJPasswordField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(getJPanel(), null);
jContentPane.add(jLabel3, null);
jContentPane.add(lb, null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(333, 125, 160, 24));
}
return jTextField;
}

}
} catch (SQLException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}

});
}
return jButton;
}

/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
}
return jPanel;
}

}
请高手说的详细点!我是个初学者不会用Jpanel谢谢!
展开
 我来答
百度网友8b8b50e
2010-04-03 · TA获得超过166个赞
知道答主
回答量:38
采纳率:0%
帮助的人:42.2万
展开全部
代码太乱了,我给你个例子:
package com.huaxia.ch6.action;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
*Apr 3, 2010 12:15:25 PM
*作者:kriss
*说明:
*/
public class JPanelWithBg extends JPanel {

private JFrame jf;
public JPanelWithBg(){
jf=new JFrame("有背景的panel");
jf.setSize(500, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.add(this);

jf.setVisible(true);
}

@Override//这个方法是用来画Panel组件的
protected void paintComponent(Graphics g) {
try {
//BufferedImage img=ImageIO.read(new File("c:\\1.jpg"));
BufferedImage img=ImageIO.read(new File(this.getClass().getResource("Winter.jpg").getPath()));
g.drawImage(img, 0, 0, 500, 400,null);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new JPanelWithBg();
}
}
ltolt
2010-04-03 · TA获得超过106个赞
知道答主
回答量:56
采纳率:0%
帮助的人:45.2万
展开全部
你粘上来的代码少太多东西了,没法给你改,return jButton 的函数在哪?catch干吗用 try呢
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式