java實(shí)現(xiàn)簡(jiǎn)單QQ登陸界面:
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),硯山企業(yè)網(wǎng)站建設(shè),硯山品牌網(wǎng)站建設(shè),網(wǎng)站定制,硯山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,硯山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
1.生成界面的java代碼
package?QQ2014;
import?javax.swing.*;
import?java.awt.*;
import?java.awt.event.*;
public?class?QQ2014?{
//創(chuàng)建登陸界面類
public?void?showLoginFrame(){
//創(chuàng)建船體對(duì)象
JFrame?loginFrame=new?JFrame();
//設(shè)置大小,位置,標(biāo)題
loginFrame.setSize(300,200);
loginFrame.setTitle("QQ2014");
loginFrame.setLocationRelativeTo(null);
//創(chuàng)建流式分布對(duì)象
FlowLayout?layout=new?FlowLayout();
loginFrame.setLayout(layout);
//創(chuàng)建賬戶名,密碼和輸入框
JLabel?user_name=new?JLabel("賬號(hào):");
JLabel?user_password=new?JLabel("密碼:");
JTextField?field_name=new?JTextField(20);
JPasswordField?field_password=new?JPasswordField(20);
//創(chuàng)建登陸,重置按鈕
JButton?button_reset=new?JButton("重置");
JButton?button_login=new?JButton("登陸");
//設(shè)置窗體可見(jiàn)
loginFrame.setVisible(true);
//創(chuàng)建事件監(jiān)聽(tīng)對(duì)象
ActionListener?action_listener1=new?ActionListener(){
public?void?actionPerformed(ActionEvent?e){
String?name=field_name.getText();
String?password=field_password.getText();
if("zhaoxin".equals(name)"123".equals(password))
{
showIndexFrame();
loginFrame.setDefaultCloseOperation(3);
loginFrame.setVisible(false);
}
else{
System.out.println("密碼錯(cuò)誤,重新輸入!");
}
}
};
ActionListener?action_listener2=new?ActionListener(){
public?void?actionPerformed(ActionEvent?e){
field_name.setText("");
field_password.setText("");
}
};
//將文本輸入框,按鈕,事件監(jiān)聽(tīng)對(duì)象添加
loginFrame.add(user_name);
loginFrame.add(field_name);
loginFrame.add(user_password);
loginFrame.add(field_password);
loginFrame.add(button_reset);
loginFrame.add(button_login);
button_reset.addActionListener(action_listener2);
button_login.addActionListener(action_listener1);
}
public?void?showIndexFrame(){
//創(chuàng)建窗體對(duì)象
JFrame?indexFrame=new?JFrame();
indexFrame.setSize(200,500);
indexFrame.setTitle("QQ好友列表");
indexFrame.setLocationRelativeTo(null);
//設(shè)置流式分布對(duì)象
FlowLayout?layout=new?FlowLayout(FlowLayout.CENTER,100,10);
indexFrame.setLayout(layout);
//創(chuàng)建好友按鈕
for(int?i=0;i10;i++)
{
JButton?button_friend=new?JButton("friend"+i);
//創(chuàng)建動(dòng)作事件監(jiān)聽(tīng)對(duì)象
ActionListener?action_listener=new?ActionListener()
{
public?void?actionPerformed(ActionEvent?e)
{
showChatFrame();
indexFrame.setVisible(false);
indexFrame.setDefaultCloseOperation(3);
}
};
button_friend.addActionListener(action_listener);
indexFrame.add(button_friend);
}
//設(shè)置窗體可見(jiàn)
indexFrame.setVisible(true);
}
public?void?showChatFrame(){
//創(chuàng)建窗體,大小,位置,標(biāo)題
JFrame?chatFrame=new?JFrame();
chatFrame.setSize(400,400);
chatFrame.setTitle("正在聊天中...");
chatFrame.setLocationRelativeTo(null);
//創(chuàng)建聊天記錄,輸入域
JTextArea?area_input=new?JTextArea(10,30);
JTextArea?area_record=new?JTextArea(5,30);
//創(chuàng)建流式分布對(duì)象
FlowLayout?layout=new?FlowLayout(FlowLayout.CENTER,0,10);
chatFrame.setLayout(layout);
//創(chuàng)建發(fā)送,關(guān)閉按扭
JButton?button_send=new?JButton("發(fā)送");
JButton?button_close=new?JButton("關(guān)閉");
//創(chuàng)建動(dòng)作事件監(jiān)聽(tīng)對(duì)象
ActionListener?action_listener1=new?ActionListener()
{
public?void?actionPerformed(ActionEvent?e){
area_record.setText(area_record.getText()+"\n"+area_input.getText());
area_input.setText("");
}
};
ActionListener?action_listener2=new?ActionListener()
{
public?void?actionPerformed(ActionEvent?e){
chatFrame.setVisible(false);
chatFrame.setDefaultCloseOperation(3);
}
};
//設(shè)置窗體可見(jiàn)
chatFrame.setVisible(true);
//添加按鈕,事件監(jiān)聽(tīng)對(duì)象
chatFrame.add(area_record);
chatFrame.add(area_input);
chatFrame.add(button_send);
chatFrame.add(button_close);
button_send.addActionListener(action_listener1);
button_close.addActionListener(action_listener2);
}
}
復(fù)制代碼
2.java?main方法調(diào)用
package?QQ2014;
public?class?Test?{
public?static?void?main(String[]?args){
QQ2014?qq=new?QQ2014();
qq.showLoginFrame();
}
}
package ch10;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//定義該類繼承自JFrame,實(shí)現(xiàn)ActionListener接口
public class LoginTest extends JFrame implements ActionListener
{
//創(chuàng)建JPanel對(duì)象
private JPanel jp=new JPanel();
//創(chuàng)建3個(gè)標(biāo)并加入數(shù)組
JLabel name = new JLabel("請(qǐng)輸入用戶名");
JLabel password = new JLabel("請(qǐng)輸入密碼");
JLabel show = new JLabel("");
private JLabel[] jl={name,password,show};
//創(chuàng)建登陸和重置按扭并加入數(shù)組
JButton login = new JButton("登陸");
JButton reset = new JButton("重置");
private JButton[] jb={login,reset};
//創(chuàng)建文本框以及密碼框
private JTextField jName=new JTextField();
private JPasswordField jPassword =new JPasswordField();
public LoginTest()
{
//設(shè)置布局管理器為空布局,這里自己擺放按鈕、標(biāo)簽和文本框
jp.setLayout(null);
for(int i=0;i2;i++)
{
//設(shè)置標(biāo)簽和按扭的位置與大小
jl[i].setBounds(30,20+40*i,180,20);
jb[i].setBounds(30+110*i,100,80,20);
//添加標(biāo)簽和按扭到JPanel容器中
jp.add(jl[i]);
jp.add(jb[i]);
//為2個(gè)按鈕注冊(cè)動(dòng)作事件監(jiān)聽(tīng)器
jb[i].addActionListener(this);
}
//設(shè)置文本框的位置和大小,注意滿足美觀并足夠用戶名的長(zhǎng)度
jName.setBounds(130,15,100,20);
//添加文本框到JPanel容器中
jp.add(jName);
//為文本框注冊(cè)動(dòng)作事件監(jiān)聽(tīng)器
jName.addActionListener(this);
//設(shè)置密碼框的位置和大小,注意滿足美觀和足夠密碼的長(zhǎng)度
jPassword.setBounds(130,60,100,20);
//添加密碼框到JPanel容器中
jp.add(jPassword);
//設(shè)置密碼框中的回顯字符,這里設(shè)置美元符號(hào)
jPassword.setEchoChar('$');
//為密碼框注冊(cè)動(dòng)作事件監(jiān)聽(tīng)器
jPassword.addActionListener(this);
//設(shè)置用于顯示登陸狀態(tài)的標(biāo)簽大小位置,并將其添加進(jìn)JPanel容器
jl[2].setBounds(10,180,270,20);
jp.add(jl[2]);
//添加JPanel容器到窗體中
this.add(jp);
//設(shè)置窗體的標(biāo)題、位置、大小、可見(jiàn)性及關(guān)閉動(dòng)作
this.setTitle("登陸窗口");
this.setBounds(200,200,270,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//實(shí)現(xiàn)動(dòng)作監(jiān)聽(tīng)器接口中的方法actionPerformed
public void actionPerformed(ActionEvent e)
{
//如果事件源為文本框
if(e.getSource()==jName)
{
//切換輸入焦點(diǎn)到密碼框
jPassword.requestFocus();
}
//如果事件源為重置按扭
else if(e.getSource()==jb[1])
{
//清空姓名文本框、密碼框和show標(biāo)簽中的所有信息
jl[2].setText("");
jName.setText("");
jPassword.setText("");
//讓輸入焦點(diǎn)回到文本框
jName.requestFocus();
}
//如果事件源為登陸按鈕,則判斷登錄名和密碼是否正確
else
{
//判斷用戶名和密碼是否匹配
if(jName.getText().equals("lixiangguo")
String.valueOf(jPassword.getPassword()).equals("19801001"))
{
jl[2].setText("登陸成功,歡迎您的到來(lái)!");
}
else
{
jl[2].setText("對(duì)不起,您的用戶名或密碼錯(cuò)誤!");
}
}
}
public static void main(String[] args)
{
//創(chuàng)建LoginTest窗體對(duì)象
new LoginTest();
}
}
這個(gè)簡(jiǎn)單點(diǎn)的
package cn.myself.myproject.FrameProject;import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;import javax.swing.Icon;
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.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;import cn.myself.myproject.employeepj.view.common.CenterWindow;
/**
* 程序功能:QQ登陸面板
* 學(xué)習(xí)內(nèi)容:GridBagLayout布局方式的學(xué)習(xí)
* 以GridBagLayout方式布局的容器,其容器中的每個(gè)組件必須由一個(gè)GridBagConstrains類的實(shí)例對(duì)象進(jìn)行大小,位置等約束。
* @author huliu 2009-06-26
* 難題:a.帳號(hào)后面是什么框?
* b.圖片的相對(duì)路徑怎么設(shè)置?
*/
public class QQRegistBoard extends JFrame{
JPanel p1;
GridBagLayout gb1;
GridBagConstraints gbc1;
JButton btn1,btn2;
JLabel label0,label1,label2,label3,label4,label5;
JTextField text1,text2;
JComboBox box1,box2;
JCheckBox check1,check2;
JList list1;
/**
* 構(gòu)造方法
*/
public QQRegistBoard(){
super("2009正式版(huliu)");
p1=new JPanel();
gb1=new GridBagLayout();
gbc1=new GridBagConstraints();
p1.setLayout(gb1);//GridBagLayout布局。網(wǎng)袋布局
getContentPane().add(p1); //取得當(dāng)前容器對(duì)象
this.setSize(350,250);
CenterWindow.centerW(this);
Icon icon1 = new ImageIcon("./QQ2.jpg");
// Icon icon1 = new ImageIcon("./QQ.jpg"); //加載圖片,當(dāng)前目錄下的QQ.jpg
// Icon icon1 = new ImageIcon("src/cn/mysef/images/QQ1.jpg");
label0=new JLabel(icon1);
label1=new JLabel("帳號(hào):");
label2=new JLabel("注冊(cè)新帳號(hào)");
label3=new JLabel("密碼:");
label4=new JLabel("取回密碼");
label5=new JLabel("狀態(tài):");
text1=new JTextField(10);
text2=new JTextField(10);
String[] str1={"313558851","313857401","690442763"};
box1=new JComboBox(str1);
box1.setEditable(true);//設(shè)置ComboBox字段值是否為可編輯
box2=new JComboBox();
check1=new JCheckBox("記住密碼",true);
check2=new JCheckBox("自動(dòng)登錄");
btn1=new JButton("設(shè)置");
btn2=new JButton("登錄");
p1.add(label0,GBC(0,0,3,1,new Insets(5,2,2,4)));//圖片
p1.add(label1,GBC(1,0,1,1,new Insets(4,2,2,4)));
p1.add(box1, GBC(1,1,1,1,new Insets(4,2,2,0)));
//p1.add(text2,GBC(1,1,1,1));
p1.add(label2,GBC(1,2,1,1,new Insets(4,2,2,3)));
p1.add(label3,GBC(2,0,1,1,new Insets(4,2,2,3)));
p1.add(text1, GBC(2,1,1,1,new Insets(5,2,2,3)));
p1.add(label4,GBC(2,2,1,1,new Insets(4,2,2,3)));
p1.add(label5,GBC(3,0,1,1,new Insets(4,2,2,3)));
p1.add(check1,GBC(3,1,1,1,new Insets(4,2,2,3)));
p1.add(check2,GBC(3,2,1,1,new Insets(4,2,2,3)));
p1.add(btn1 ,GBC(4,0,1,1,new Insets(4,2,2,3)));
p1.add(btn2 ,GBC(4,2,1,1,new Insets(4,2,2,3)));
}
/**
* GBC方法:功能是設(shè)計(jì)以GridBagLayout方式布局的容器(如Panel容器對(duì)象)內(nèi)的組件的位置,大小等約束的。
* @param gridy
* @param gridx
* @param gridwidth
* @param gridheight
* @return GridBagStraints實(shí)對(duì)象
* Insets(int top, int left, int bottom, int right),與其它組件之間距離(上,左,下,右)
*/
public GridBagConstraints GBC(int gridy,int gridx,int gridwidth,int gridheight,Insets insets){
GridBagConstraints gbc1=new GridBagConstraints();
gbc1.gridx=gridx; //列
gbc1.gridy=gridy; //行
gbc1.gridwidth=gridwidth;//寬度
gbc1.gridheight=gridheight; //高度
//insets=new Insets(1,1,1,1);
gbc1.insets=insets;
return gbc1;
}
public static void main(String[] args){
new QQRegistBoard().setVisible(true);
}}