import java.awt.Color;
import java.awt.Font;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;public class QQ extends JFrame { /**
 * @param args
 */

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

JPanel p = (JPanel)this.getContentPane();
//账号输入框
JTextField tf1 = new JTextField();
tf1.setBounds(80,100,200,30);
p.add(tf1);

//注册账号标签
JLabel lb1 = new JLabel("注册账号");
lb1.setBounds(300,100,70,27);
p.add(lb1);
lb1.setFont(new Font("仿宋",Font.BOLD,14));
lb1.setForeground(Color.BLUE);

JTextField tf2 = new JTextField();
tf2.setBounds(80,140,200,27);
p.add(tf2);

//找回密码标签
JLabel lb2 = new JLabel("找回密码");
lb2.setBounds(300,140,70,27);
p.add(lb2);
lb2.setFont(new Font("仿宋",Font.BOLD,14));
lb2.setForeground(Color.BLUE);

//记住密码,自动登录多选框
JCheckBox ck1 = new JCheckBox("记住密码"); 
JCheckBox ck2 = new JCheckBox("自动登录");
ck1.setBounds(80,180,80,30);
ck2.setBounds(170, 180, 80, 30);
p.add(ck1);
p.add(ck2);

//登录按钮
JButton btn = new JButton("登   录");
btn.setBounds(100, 210, 140, 30);
p.add(btn);
btn.setFont(new Font("仿宋",Font.BOLD,16));

//多账号登录
JButton btn1 = new JButton("多账号");
btn1.setBounds(10, 210, 80, 30);
p.add(btn1);
btn1.setFont(new Font("仿宋",Font.BOLD,14));

//加头像
ImageIcon image = new ImageIcon("英雄头像\\Ahri.png");
JLabel lb3 = new JLabel(image);
p.add(lb3);
lb3.setBounds(5, 100, 70, 70);
//下拉菜单,表状态
JComboBox cbox = new JComboBox();
//ImageIcon cm = new ImageIcon("a.gif");
cbox.addItem("离开");
cbox.addItem("隐身");
cbox.addItem("上线");
cbox.addItem("忙碌");
cbox.setBounds(20, 180, 60, 25);
p.add(cbox);
JMenuBar m = new JMenuBar();
m.setDefaultLocale(null);
m.setLayout(null);
m.setLocation(0,50);
m.setBounds(0, 100, 10, 10);
JMenu mm = new JMenu("图");
JMenuItem mi1= new JMenuItem("在线");
JMenuItem mi2= new JMenuItem("影身");
JMenuItem mi3= new JMenuItem("忙碌");
JMenuItem mi4= new JMenuItem("离开");
this.setJMenuBar(m);
m.add(mm);
mm.add(mi1);
mm.add(mi2);
mm.add(mi3);
mm.add(mi4); this.setResizable(false);//不允许放大,改变窗口大小等
this.setVisible(true);
}
public static void main(String[] args) {
QQ q = new QQ();
}}
这是我写的QQ登录的界面,但是不知道怎么设置背景图片,请各位大神指教QQGUI

解决方案 »

  1.   


    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JTextField;public class QQ extends JFrame { /**
     * @param args
     */ public QQ() {
    super("QQ2013");
    this.setSize(390, 300);
    this.setLayout(null);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// JPanel p = (JPanel) this.getContentPane();
    JPanel p = new ImagePane();
    this.setContentPane(p);

    // 账号输入框
    JTextField tf1 = new JTextField();

    tf1.setBounds(80, 100, 200, 30);
    p.add(tf1); // 注册账号标签
    JLabel lb1 = new JLabel("注册账号");
    lb1.setBounds(300, 100, 70, 27);
    p.add(lb1);
    lb1.setFont(new Font("仿宋", Font.BOLD, 14));
    lb1.setForeground(Color.BLUE); JTextField tf2 = new JTextField();
    tf2.setBounds(80, 140, 200, 27);
    p.add(tf2); // 找回密码标签
    JLabel lb2 = new JLabel("找回密码");
    lb2.setBounds(300, 140, 70, 27);
    p.add(lb2);
    lb2.setFont(new Font("仿宋", Font.BOLD, 14));
    lb2.setForeground(Color.BLUE); // 记住密码,自动登录多选框
    JCheckBox ck1 = new JCheckBox("记住密码");
    JCheckBox ck2 = new JCheckBox("自动登录");
    ck1.setBounds(80, 180, 80, 30);
    ck2.setBounds(170, 180, 80, 30);
    p.add(ck1);
    p.add(ck2); // 登录按钮
    JButton btn = new JButton("登   录");
    btn.setBounds(100, 210, 140, 30);
    p.add(btn);
    btn.setFont(new Font("仿宋", Font.BOLD, 16)); // 多账号登录
    JButton btn1 = new JButton("多账号");
    btn1.setBounds(10, 210, 80, 30);
    p.add(btn1);
    btn1.setFont(new Font("仿宋", Font.BOLD, 14)); // 加头像
    ImageIcon image = new ImageIcon("英雄头像\\Ahri.png");
    JLabel lb3 = new JLabel(image);
    p.add(lb3);
    lb3.setBounds(5, 100, 70, 70);
    // 下拉菜单,表状态
    JComboBox cbox = new JComboBox();
    // ImageIcon cm = new ImageIcon("a.gif");
    cbox.addItem("离开");
    cbox.addItem("隐身");
    cbox.addItem("上线");
    cbox.addItem("忙碌");
    cbox.setBounds(20, 180, 60, 25);
    p.add(cbox);
    JMenuBar m = new JMenuBar();
    m.setDefaultLocale(null);
    m.setLayout(null);
    m.setLocation(0, 50);
    m.setBounds(0, 100, 10, 10);
    JMenu mm = new JMenu("图");
    JMenuItem mi1 = new JMenuItem("在线");
    JMenuItem mi2 = new JMenuItem("影身");
    JMenuItem mi3 = new JMenuItem("忙碌");
    JMenuItem mi4 = new JMenuItem("离开");
    this.setJMenuBar(m);
    m.add(mm);
    mm.add(mi1);
    mm.add(mi2);
    mm.add(mi3);
    mm.add(mi4); 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);
        }
    }