当前位置:编程学习 > JAVA >>

基础,添加背景问题

package QQ;

import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class land {
JFrame qq = new JFrame("QQ");
JTextField name = new JTextField(10);
JTextField password = new JTextField(16);
JButton login = new JButton("登    录");
JLabel zhuce = new JLabel("还没注册?");
JLabel wangji = new JLabel("忘记密码?");
ImageIcon background = new ImageIcon("F:/JAVA/picture/beijing.jpg");

private JPanel imagePanel;

public void init()
{
// 把背景图片加到label
JLabel label = new JLabel(background);
//label的大小为jframe的大小
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
    //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel = (JPanel)qq.getContentPane();
imagePanel.setOpaque(false);
//
imagePanel.setLayout(new FlowLayout());
//添加按钮,文本框到JPANEL中
imagePanel.add(login);
imagePanel.add(name);
imagePanel.add(password);
imagePanel.add(wangji);
imagePanel.add(zhuce);
qq.getLayeredPane().setLayout(null);
//把背景图片添加到分层窗格的最底层作为背景
qq.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));  
//设置Jframe的大小
qq.setSize(400,300);
//设置父类容器的Llayout为null,绝对布局
qq.setLayout(null);
//设置按钮,文本框的大小
login.setBounds(150, 200, 100, 50);
name.setBounds(150, 100, 150, 30);
password.setBounds(150, 150, 150, 30);
zhuce.setBounds(220, 100, 50, 30);
wangji.setBounds(220, 150, 50, 30);
//qq.add(zhuce);
//qq.add(wangji);
//JRAME居中
qq.setLocationRelativeTo(null);
//JRAME不可改变大小
qq.setResizable(false); 
//关闭事件
qq.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//窗口显示
qq.setVisible(true);
}



public static void main(String[] args) {
new land().init();
}

}
这个文本框不被覆盖,但是文字怎么被覆盖了呀?怎么解决?有其他的方法添加背景吗?(常用)
小弟自学JAVA一月有余,跪求大神!
基础  背景  QQ --------------------编程问答-------------------- 参考下:

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

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

public class QQ extends JFrame {

/**
 * @param args
 */

public QQ() {
this.setSize(390, 300);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel p = new ImagePane();
this.setContentPane(p);

this.setResizable(false);// 不允许放大,改变窗口大小等
this.setVisible(true);
}

public static void main(String[] args) {
QQ q = new QQ();
}

}

class ImagePane extends JPanel {

private Image image;

public ImagePane() {
try {
image = ImageIO.read(new File("/Users/user/Desktop/1.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}


--------------------编程问答-------------------- 版主大人驾到?哇,好激动,第一次发帖子版主就给我回复。天降祥瑞呀!感谢 --------------------编程问答-------------------- 直接使用SwingX,JXPanel setBackgroundPainter(new ImagePainter(...))
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,