我生待明日,万事成蹉跎

JAVA GUI编程练习《仿QQ2007登录界面》

java gui编程

java gui编程

分析:在窗体内分两个中间容器

窗体类 QqFrame

package cn.xuhongtao.work618;

import java.awt.Color;
import java.awt.Container;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class QqFrame extends JFrame{
private Container contentbody;
private Spanel panels;
private Xpanel panelx;

public QqFrame(){
Toolkit tk = Toolkit.getDefaultToolkit();
//获取屏幕尺寸
int w = (int)tk.getScreenSize().getWidth();
int h = (int)tk.getScreenSize().getHeight();
//设置窗体位置
this.setLocation((w – 330) / 2,(h – 560) / 2);
//设置窗体大小(像素)
this.setSize(330, 560);
//关闭窗体,程序结束
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//窗体标题
this.setTitle(“QQ 2014”);
//窗体图标
this.setIconImage(tk.createImage(“img/logo.gif”));
//内容面板内容放置
this.qqpanel();

//设置窗体可见
this.setVisible(true);
}
public void qqpanel(){
this.contentbody=this.getContentPane();
this.contentbody.setLayout(null);
//设置划分的面板
this.panels=new Spanel();
this.panelx=new Xpanel();
this.contentbody.add(this.panels);
this.contentbody.add(this.panelx);
}

}

上中间容器Spanel

package cn.xuhongtao.work618;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Spanel extends JPanel{
private JLabel qqLabel;//qq banner
private buttonPanel buttonPL;;//按钮面板
private Centerpanel centerPL;//中间面板
public Spanel(){
this.setBackground(new Color(240,250,250));//s面板背景色
this.setLayout(new BorderLayout());
this.setBounds(0, 0, 323, 200);
//banner图片
this.qqLabel=new JLabel();
this.qqLabel.setIcon(new ImageIcon(“img/title.jpg”));
this.add(this.qqLabel,BorderLayout.NORTH);
//中间面板
this.centerPL=new Centerpanel();
this.centerPL.setBackground(new Color(240,250,250));//背景色
this.add(this.centerPL);
//按钮面板,在这个是边界布局的中间容器中默认为流布局
this.buttonPL=new buttonPanel();
this.buttonPL.setBackground(new Color(225,249,255));//按钮面板背景色
this.add(this.buttonPL,BorderLayout.SOUTH);
}
}

下中间容器Xpanel

package cn.xuhongtao.work618;
import java.awt.Color;
import javax.swing.JPanel;
public class Xpanel extends JPanel{
private Onpanel onPL;//上部
private Underpanel unPL;//下部
public Xpanel(){
this.setBackground(new Color(225,249,255));
this.setLayout(null);
this.setBounds(0, 200, 323, 385);
//上部面板
this.onPL=new Onpanel();
this.add(this.onPL);
//下部面板
this.unPL=new Underpanel();
this.add(this.unPL);
}
}

分析:将上中间容器(面板)分为两部分——按钮面板和中间面板,并且用边界布局,把东西方向撑掉。

Spanel的按钮面板类buttonPanel

package cn.xuhongtao.work618;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
//按钮面板
public class buttonPanel extends JPanel{
private JButton safeButton;//安全检查按钮
private JButton setButton;//设置按钮
private JButton loginButton;//登录按钮
private JTextField nullText;//空
public buttonPanel(){
this.safeButton=new JButton(“<>安全检查”);
this.setButton=new JButton(“设置↑”);
this.nullText=new JTextField(” “);
this.nullText.setBorder(null);
this.nullText.setBackground(null);
this.loginButton=new JButton(“登录”);
this.add(this.safeButton);
this.add(this.setButton);
this.add(this.nullText);
this.add(this.loginButton);
}
}

Spanel的Centerpanel面板(边界布局中间部分)

package cn.xuhongtao.work618;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Centerpanel extends JPanel{
private JComboBox qqBox;//QQ号码下拉列表
private JComboBox inputqqBox;//QQ号码输入框下拉列表
private JLabel getqqBox;//申请号码标签
private JLabel getpwdBox;//QQ密码标签
private JTextField qqpwdField;//QQ密码文本框
private JLabel nullpwdBox;//忘了密码标签
private JRadioButton automaticButton;//自动登陆选框
private JRadioButton StealthButton;//隐身登录选框
public Centerpanel(){
this.setLayout(null);
//QQ号码下拉列表
this.qqBox=new JComboBox(new String [] {“QQ号码 “,”手机号码”,”小灵通号码”});
this.qqBox.setEditable(false);
this.qqBox.setBounds(9, 5, 78, 25);
//QQ号码的输入框下拉列表
this.inputqqBox=new JComboBox();
this.inputqqBox.setBounds(93, 5, 130, 25);
this.inputqqBox.setEditable(true);
//申请号码、QQ密码
this.getqqBox=new JLabel(” 申请号码 “);
this.getqqBox.setBounds(228, 5, 70, 25);
this.getpwdBox=new JLabel(“QQ密码 “);
this.getpwdBox.setBounds(9, 35, 70, 25);
//文本框
this.qqpwdField=new JTextField(“”);
this.qqpwdField.setBounds(93, 35, 135, 20);
//忘了密码
this.nullpwdBox=new JLabel(“忘了密码?”);
this.nullpwdBox.setBounds(232, 30, 70, 25);
//单选框
this.automaticButton=new JRadioButton(“自动登陆”);
this.automaticButton.setBounds(78, 60, 85, 25);
this.automaticButton.setBackground(new Color(240,250,250));
this.StealthButton=new JRadioButton(“隐身登录”);
this.StealthButton.setBackground(new Color(240,250,250));
this.StealthButton.setBounds(173, 60, 85, 25);

this.add(this.qqBox);
this.add(this.inputqqBox);
this.add(this.getqqBox);
this.add(this.getpwdBox);
this.add(this.qqpwdField);
this.add(this.nullpwdBox);
this.add(this.automaticButton);
this.add(this.StealthButton);
}
}

分析:接着将Xpanel容器分为上下两部分,用null布局

Xpanel的Onpanel面板(上面部分)

package cn.xuhongtao.work618;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
//Xpanel的Onpanel
public class Onpanel extends JPanel {
private JLabel loginModeLabel;//登陆模式
private JComboBox loginBox;//登陆模式下拉列表
private JTextField nullText;//空
private JButton cancelButton;//清除记录
private JButton takeButton;//取回密码
private JButton safeButton;//申请密码保护
public Onpanel(){
this.setBackground(new Color(225,249,255));
this.setBounds(0, 0, 323, 100);
this.setBorder(BorderFactory.createTitledBorder(“其它选项”));
//登陆模式标签
this.loginModeLabel=new JLabel(“登陆模式:”);
this.add(this.loginModeLabel);
//登陆模式下拉列表
this.loginBox=new JComboBox(new String [] {“普通模式”,”网吧模式”,”消息保护模式”});
this.add(this.loginBox);
//空
this.nullText=new JTextField(” “);
this.nullText.setBorder(null);
this.nullText.setBackground(null);
this.add(this.nullText);
//按钮
this.cancelButton=new JButton(“清除记录”);
this.takeButton=new JButton(“取回密码”);
this.safeButton=new JButton(“申请密码保护”);
this.add(this.cancelButton);
this.add(this.takeButton);
this.add(this.safeButton);
}
}

Xpanel的Underpanel面板(下面部分)

package cn.xuhongtao.work618;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
//Xpanel的Underpanel
public class Underpanel extends JPanel {
private JLabel tyleLabel;//类型:
private JLabel addressLabel;//地址:
private JLabel kouLabel;//端口:
private JLabel userLabel;//用户:
private JLabel pwdLabel;//密码:
private JLabel areaLabel;//域(可不填):
private JLabel etstyleLabel;//设置你要登录到的服务器类型
private JLabel etaddressLabel;//需要登陆的服务器地址
private JComboBox styleBox;//类型的下拉列表
private JComboBox loginstyleBox;//登录到的服务器类型的下拉列表
private JComboBox addressBox;//登陆的服务器地址的下拉列表

private JTextField addressText;//地址文本框
private JTextField kouText;//端口文本框
private JTextField userText;//用户文本框
private JTextField pwdText;//密码文本框
private JTextField areaText;//域文本框

private JButton testButton;//测试按钮
private JRadioButton qqServer;//选择QQ登录服务器

public Underpanel(){
this.setLayout(null);
this.setBackground(new Color(225,249,255));
this.setBounds(0, 100, 323, 257);
this.setBorder(BorderFactory.createTitledBorder(“网络设置”));
//JLabel标签
this.tyleLabel=new JLabel(“类型:”);
this.tyleLabel.setBounds(10, 20, 50, 30);
this.addressLabel=new JLabel(“地址:”);
this.addressLabel.setBounds(10, 52, 40, 30);
this.kouLabel=new JLabel(“端口:”);
this.kouLabel.setBounds(195, 52,40, 30);
this.userLabel=new JLabel(“用户”);
this.userLabel.setBounds(10, 77, 40, 30);
this.pwdLabel=new JLabel(“密码:”);
this.pwdLabel.setBounds(195, 77, 40, 30);
this.areaLabel=new JLabel(“域(可不填):”);
this.areaLabel.setBounds(10, 110, 90, 30);
this.etstyleLabel=new JLabel(“设置你要登录到的服务器类型”);
this.etstyleLabel.setBounds(10, 165, 190, 20);
this.etaddressLabel=new JLabel(“需要登陆的服务器地址”);
this.etaddressLabel.setBounds(10, 190, 190, 20);
//JComboBox标签
this.styleBox=new JComboBox(new String [] {“不使用代理”,”SOCKS5代理”,”HTTP代理”,”使用浏览器设置”});
this.styleBox.setBounds(55, 20, 130, 25);
this.loginstyleBox=new JComboBox(new String [] {“UDP类型”});
this.loginstyleBox.setBounds(180, 160, 130, 25);
this.addressBox=new JComboBox(new String [] {“xuhongtao.cn”});
this.addressBox.setBounds(150, 190, 130, 25);
//JTextField标签
this.addressText=new JTextField();
this.addressText.setBounds(55, 55, 135, 23);
this.kouText=new JTextField();
this.kouText.setBounds(235, 55, 83, 23);
this.userText=new JTextField();
this.userText.setBounds(55, 80, 135, 23);
this.pwdText=new JTextField();
this.pwdText.setBounds(235, 80, 83, 23);
this.areaText=new JTextField();
this.areaText.setBounds(90, 113, 98, 23);
//按钮和单选
this.testButton=new JButton(“测试”);
this.testButton.setBounds(255, 110, 60, 30);
this.qqServer=new JRadioButton(“选择QQ登录服务器”);
this.qqServer.setBackground(null);
this.qqServer.setBounds(10, 140, 150,20);
//添加组件
this.add(this.tyleLabel);
this.add(this.addressLabel);
this.add(this.kouLabel);
this.add(this.userLabel);
this.add(this.pwdLabel);
this.add(this.areaLabel);
this.add(this.etstyleLabel);
this.add(this.etaddressLabel);
this.add(this.styleBox);
this.add(this.loginstyleBox);
this.add(this.addressBox);
this.add(this.addressText);
this.add(this.kouText);
this.add(this.userText);
this.add(this.pwdText);
this.add(this.areaText);
this.add(this.testButton);
this.add(this.qqServer);
}
}

注:主方法main略

未经允许不得转载:徐宏涛博客 » JAVA GUI编程练习《仿QQ2007登录界面》

分享到:更多 ()

评论 抢沙发

评论前必须登录!