swing图形界面(二)QQ框架界面(GridBagLayout设计实例)

QQ框架:
import javax.swing.*;

import java.awt.*;

public class QQFrame extends JFrame{
    public QQFrame()
    {
        super("QQ2010");
        setSize(340,250);
        setLocation(250,300);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainPanel mp=new MainPanel();
       
        add(mp);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(this);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        setVisible(true);
    }
    public static void main(String[] args)
    {
        QQFrame f=new QQFrame();
       
             
    }

}
class MainPanel extends Panel{
    GridBagLayout gbl=new GridBagLayout();
    public MainPanel()
    {
        super();
        setLayout(gbl);
       
        ImageIcon i1=new ImageIcon("D://my document//photoshops//Icon//QQ.jpg");
        JLabel QQphoto=new JLabel(i1,SwingConstants.CENTER);
        JLabel IDLabel=new JLabel("账号:");
        JTextField ID=new JTextField();
        JLabel register=new JLabel(" 注册新账号");
        JLabel passwordLabel=new JLabel("密码:");
        JPasswordField password=new JPasswordField();
        JLabel re=new JLabel(" 找回密码");
        JCheckBox autoLoad=new JCheckBox("自动登录");
        JCheckBox Remeber=new JCheckBox("记住密码");
        JCheckBox hidingLoad=new JCheckBox("隐身登录");
        JButton set=new JButton("设置");
        JButton load=new JButton("登录");
         addComponent(QQphoto, 0, 0, 11, 3,11,3, GridBagConstraints.NONE,GridBagConstraints.NORTH);
        addComponent(IDLabel,0,4,2,2,10,2, GridBagConstraints.NONE,GridBagConstraints.EAST);
        addComponent(ID, 2, 4, 6, 2,40,2,  GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
        addComponent(register,8,4,3,2,20,2,GridBagConstraints.NONE,GridBagConstraints.WEST);
        addComponent(passwordLabel, 0, 7, 2, 2,10,2, GridBagConstraints.NONE,GridBagConstraints.EAST);
        addComponent(password, 2, 7, 6, 2, 40,2, GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
        addComponent(re, 8, 7, 3, 2, 20,2, GridBagConstraints.NONE,GridBagConstraints.WEST);
        addComponent(autoLoad, 0, 11, 3, 1,3,1,  GridBagConstraints.NONE,GridBagConstraints.CENTER);
        addComponent(Remeber, 3, 11, 5, 1, 3,1, GridBagConstraints.NONE,GridBagConstraints.CENTER);
        addComponent(hidingLoad, 8, 11, 3, 1,3,1,GridBagConstraints.NONE,GridBagConstraints.CENTER);
        addComponent(set, 0, 13, 4, 2, 3,2, GridBagConstraints.NONE,GridBagConstraints.WEST);
        addComponent(load, 7, 13, 4, 2, 3,2, GridBagConstraints.NONE,GridBagConstraints.EAST);
    }
    private void addComponent(Component component,int gridx,int gridy,int gridwidth,int gridheight,int weightx,int weighty,int fill,int anchor)
    {
        GridBagConstraints constraints=new GridBagConstraints();
        constraints.gridx=gridx;
        constraints.gridy=gridy;
        constraints.gridwidth=gridwidth;
        constraints.gridheight=gridheight;
        constraints.weightx=weightx;
        constraints.weighty=weighty;
        constraints.fill=fill;
        constraints.anchor=anchor;
        gbl.setConstraints(component, constraints);
        add(component);
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值