?? loginframe.java
字號:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* Description:
* <br/>Copyright (C), 2008-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
//登陸用的對話框
public class LoginFrame extends JDialog
{
public JLabel tip;
public JTextField userField = new JTextField("李剛" , 20);
public JComboBox iconList = new JComboBox(
new Integer[]{1, 2, 3, 4, 5 , 6, 7, 8 ,9 ,10});
private JButton loginBn = new JButton("登陸");
//聊天的主界面
private LanChat chatFrame;
//聊天通信的工具實例
public static ComUtil comUtil;
//構(gòu)造器,用于初始化的登陸對話框
public LoginFrame(LanChat parent , String msg)
{
super(parent , "輸入名字后登陸" , true);
this.chatFrame = parent;
setLayout(new GridLayout(5, 1));
JPanel jp = new JPanel();
tip = new JLabel(msg);
tip.setFont(new Font("Serif" , Font.BOLD , 16));
jp.add(tip);
add(jp);
add(getPanel("用戶名" , userField));
iconList.setPreferredSize(new Dimension(224, 20));
add(getPanel("圖 標(biāo)" , iconList));
JPanel bp = new JPanel();
loginBn.addActionListener(new MyActionListener(this));
bp.add(loginBn);
add(bp);
pack();
setVisible(true);
}
//工具方法,該方法將一個字符串和組件組合成JPanel對象
private JPanel getPanel(String name , JComponent jf)
{
JPanel jp = new JPanel();
jp.add(new JLabel(name + ":"));
jp.add(jf);
return jp;
}
//該方法用于改變登陸窗口最上面的提示信息
public void setTipMsg(String tip)
{
this.tip.setText(tip);
}
//定義一個事件監(jiān)聽器
class MyActionListener implements ActionListener
{
private LoginFrame loginFrame;
public MyActionListener(LoginFrame loginFrame)
{
this.loginFrame = loginFrame;
}
//當(dāng)鼠標(biāo)單擊事件發(fā)生時
public void actionPerformed(ActionEvent evt)
{
try
{
//初始化聊天通信類
comUtil = new ComUtil(chatFrame);
final String loginMsg = YeekuProtocol.PRESENCE + userField.getText()
+ YeekuProtocol.SPLITTER + iconList.getSelectedObjects()[0]
+ YeekuProtocol.PRESENCE;
comUtil.broadCast(loginMsg);
//啟動定時器每20秒廣播一次在線信息
javax.swing.Timer timer = new javax.swing.Timer(1000 * 10,
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
comUtil.broadCast(loginMsg);
}
});
timer.start();
loginFrame.setVisible(false);
chatFrame.setVisible(true);
}
catch (Exception ex)
{
loginFrame.setTipMsg("確認(rèn)30001端口空閑,且網(wǎng)絡(luò)正常!");
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -