需求说明:
使用Swing布局管理器和常用控件,实现仿QQ登录界面
实现思路:
创建登录界面的类QQLogin,该类继承父类JFrame,在该类中创建无参数的构造方法,在构造方法中,设置窗体大小为宽355、高265,窗体的布局格式为绝对定位,通过this.setResizable(false)方法设置窗体大小不能改变。
创建初始化方法,在该方法中,创建图10.12中的相关组件,并在该方法中合理设置各组件之间的位置。在构造方法中调用该初始化方法。
素材链接:http://pan-yz.chaoxing.com/share/info/deecb7261a4cc4ea
实现代码:
package com.tencent.qq; import javax.swing.JFrame; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class QQLogin extends JFrame { public QQLogin() { this.setBounds(700, 300, 600, 454); this.setTitle("QQ登录界面"); this.setLayout(null); this.setResizable(false); Icon icon1 = new ImageIcon("img/logo.jpg"); JLabel img1 = new JLabel(icon1); img1.setBounds(0, 0, 600, 154); Icon icon2 = new ImageIcon("img/icon.jpg"); JLabel img2 = new JLabel(icon2); img2.setBounds(80, 170, 110, 100); JTextField userTest = new JTextField(); userTest.setBounds(200, 185, 250, 30); JPasswordField pawTest = new JPasswordField(); pawTest.setBounds(200, 230, 250, 30); JLabel zczh = new JLabel("注册账号"); zczh.setBounds(460, 100, 200, 200); JLabel zhmm = new JLabel("找回密码"); zhmm.setBounds(460, 145, 200, 200); JComboBox ComboBox = new JComboBox(); ComboBox.addItem("在线"); ComboBox.addItem("离开"); ComboBox.addItem("忙碌"); ComboBox.addItem("隐身"); ComboBox.setBounds(95, 280, 85, 25); JCheckBox jzmm = new JCheckBox("记住密码"); jzmm.setBounds(220, 270, 100, 50); JCheckBox zddl = new JCheckBox("自动登录"); zddl.setBounds(350, 270, 100, 50); JButton dzh = new JButton("多账号"); dzh.setBounds(50, 340, 105, 30); JButton sz = new JButton("设置"); sz.setBounds(180, 340, 105, 30); JButton dl = new JButton("登录"); dl.setBounds(420, 340, 105, 30); this.add(img1); this.add(img2); this.add(userTest); this.add(pawTest); this.add(zczh); this.add(zhmm); this.add(ComboBox); this.add(jzmm); this.add(zddl); this.add(dzh); this.add(sz); this.add(dl); } public static void main(String[] args) { QQLogin login = new QQLogin(); login.setVisible(true); } }