?? chat.java
字號(hào):
import java.io.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class Chat extends JFrame implements ItemListener,ListSelectionListener, ActionListener {
JPanel pnlMain;
JMenuBar mbMain;
JMenu mnuSystem,mnuHelp;
JPopupMenu popMenu;
JMenuItem mnuiCls,mnuiOpen,mnuiSave,mnuiClock,mnuiExit,
mnuiContent,mnuiIndex,mnuiAbout,popOpen,popSave;
JLabel lblUser,lblNumber,lblInfo;
JLabel lblYou,lblContent,lblFile;
DefaultListModel dlmUser;
JList lstUser;
JTextArea taInfo;
JScrollPane jspInfo;
JComboBox cmbUser;
JCheckBox ckbSilent;
JTextField txtContent,txtFile;
JButton btnSend,btnSave,btnExit,btnCls,btnClock,
btnSendFile,btnBrowse;
JFileChooser fc;
FileNameExtensionFilter filter;
FileWriter fw;
FileOutputStream fos;
//構(gòu)造器部分
public Chat()
{
//組件初始化
fc=new JFileChooser();
mbMain=new JMenuBar();
pnlMain=new JPanel(null);
setContentPane(pnlMain);
mnuSystem=new JMenu("系統(tǒng)(S)");
mnuSystem.setMnemonic(KeyEvent.VK_S);
mnuHelp=new JMenu("幫助(H)");
mnuHelp.setMnemonic(KeyEvent.VK_H);
mbMain.add(mnuSystem);
mbMain.add(mnuHelp);
mnuiCls=new JMenuItem("清除屏幕顯示信息");
mnuiOpen=new JMenuItem("打開文件");
mnuiSave=new JMenuItem("保存聊天記錄");
mnuiClock=new JMenuItem("查看時(shí)間");
Icon icoExit=new ImageIcon("e:/java/images/exit.gif");
mnuiExit=new JMenuItem("退出系統(tǒng)(X)",icoExit);
mnuiExit.setMnemonic('X');
Icon icoContent=new ImageIcon("e:/java/images/content.gif");
mnuiContent=new JMenuItem("目錄",icoContent);
mnuiIndex=new JMenuItem("索引");
mnuiAbout=new JMenuItem("關(guān)于[HappyChat]聊天系統(tǒng)");
mnuHelp.add(mnuiOpen);
mnuHelp.add(mnuiContent);
mnuHelp.add(mnuiIndex);
mnuHelp.add(mnuiAbout);
mnuSystem.add(mnuiCls);
mnuSystem.add(mnuiOpen);
mnuSystem.add(mnuiSave);
mnuSystem.add(mnuiClock);
mnuSystem.addSeparator();
mnuSystem.add(mnuiExit);
popMenu=new JPopupMenu();//設(shè)置快捷菜單
popOpen=new JMenuItem("打開文件");
popSave=new JMenuItem("保存文件");
popMenu.add(popOpen);
popMenu.add(popSave);
lblUser=new JLabel("【在線用戶列表】");
lblNumber=new JLabel("在線人數(shù):");
lblInfo=new JLabel("【聊天信息】");
lblYou=new JLabel("你對(duì):");
lblContent=new JLabel("聊天內(nèi)容:");
lblFile=new JLabel("附件:");
dlmUser=new DefaultListModel();
dlmUser.addElement("張三");
dlmUser.addElement("李四");
dlmUser.addElement("liuzc");
dlmUser.addElement("wangym");
lstUser=new JList(dlmUser);
lstUser.setSelectedIndex(0);
lstUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lblNumber.setText(lblNumber.getText()+String.valueOf(dlmUser.getSize()));
taInfo=new JTextArea();
jspInfo=new JScrollPane(taInfo);
taInfo.append("【你】對(duì)【所有人】說:大家好!");
taInfo.append("\n【liuzc】說:大家好!");
taInfo.setEditable(false);
final String strUser[]={"所有人","張三","李四","劉德華","張學(xué)友","劉亦菲"};
cmbUser=new JComboBox(strUser);
ckbSilent=new JCheckBox("私聊");
ckbSilent.setEnabled(false);
txtContent=new JTextField(200);
txtFile=new JTextField(300);
btnSend=new JButton("發(fā)送(N)");
btnSave=new JButton("保存(S)");
btnExit=new JButton("退出(X)");
btnCls=new JButton("清屏(C)");
btnClock=new JButton("時(shí)鐘(T)");
btnSendFile=new JButton("發(fā)送文件(F)");
btnBrowse=new JButton("瀏覽...");
btnClock.addActionListener(this);
//設(shè)置按鈕快捷鍵
btnSend.setMnemonic('N');
btnSave.setMnemonic('S');
btnExit.setMnemonic('X');
btnCls.setMnemonic('C');
btnClock.setMnemonic('T');
btnSendFile.setMnemonic('F');
//設(shè)置組件事件
btnBrowse.addActionListener(this);
btnSend.addActionListener(this);
btnCls.addActionListener(this);
btnExit.addActionListener(this);
lstUser.addListSelectionListener(this);
cmbUser.addItemListener(this);
mnuiOpen.addActionListener(this);
mnuiCls.addActionListener(this);
mnuiExit.addActionListener(this);
popOpen.addActionListener(this);
popSave.addActionListener(this);
mnuiSave.addActionListener(this);
btnSave.addActionListener(this);
btnClock.addActionListener(this);
mnuiClock.addActionListener(this);
taInfo.addMouseListener(
new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
if(me.getButton()==MouseEvent.BUTTON3&& !me.isPopupTrigger())
popMenu.show(me.getComponent(), me.getX(), me.getY());
}
}
);
//設(shè)置窗體背景圖片
Icon logo=new ImageIcon("E:/java/images/chat.jpg");
JLabel lblLogo=new JLabel(logo);
setTitle("聊天室(HappyChar)");
Toolkit tk=Toolkit.getDefaultToolkit();
Image img=tk.getImage("E:/java/images/appico.jpg");
this.setIconImage(img);
//添加組件
pnlMain.add(lblUser);
pnlMain.add(lblNumber);
pnlMain.add(lblInfo);
pnlMain.add(lblYou);
pnlMain.add(lblContent);
pnlMain.add(lblFile);
pnlMain.add(lstUser);
pnlMain.add(jspInfo);
pnlMain.add(cmbUser);
pnlMain.add(ckbSilent);
pnlMain.add(txtContent);
pnlMain.add(txtFile);
pnlMain.add(btnSend);
pnlMain.add(btnSave);
pnlMain.add(btnExit);
pnlMain.add(btnCls);
pnlMain.add(btnClock);
pnlMain.add(btnSendFile);
pnlMain.add(btnBrowse);
pnlMain.add(lblLogo);
pnlMain.add(btnSend);
//設(shè)置組件位置
lblLogo.setBounds(0,0,600,400);
lblUser.setBounds(5,10,120,25);
lblNumber.setBounds(140, 10, 80, 25);
lblInfo.setBounds(240, 10, 80, 25);
lstUser.setBounds(5, 40, 200, 280);
jspInfo.setBounds(210, 40, 380, 280);
lblYou.setBounds(8, 320, 40, 30);
cmbUser.setBounds(45, 325, 80, 20);
ckbSilent.setBounds(130, 325, 60, 20);
lblContent.setBounds(200, 320, 70, 30);
txtContent.setBounds(270, 325, 200, 20);
lblFile.setBounds(8,345,40,30);
txtFile.setBounds(45,350,300,20);
btnSend.setBounds(500,325,80,20);
btnSave.setBounds(500,350,80,20);
btnExit.setBounds(500,375,80,20);
btnCls.setBounds(400,350,80,20);
btnClock.setBounds(400,375,80,20);
btnBrowse.setBounds(155, 375, 80, 20);
btnSendFile.setBounds(225, 375, 120, 20);
//btnSend.setBounds(50,50,30,30);
//窗體初始化
this.setJMenuBar(mbMain);
Dimension scrnSize=tk.getScreenSize();
setSize(600,455);
setLocation((scrnSize.width-this.getWidth())/2,(scrnSize.height-this.getHeight())/2);
setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void itemStateChanged(ItemEvent ie) {
Object oi=ie.getSource();
if(oi==cmbUser)
{
if(cmbUser.getSelectedIndex()!=0)
{
ckbSilent.setEnabled(true);
}
else
ckbSilent.setEnabled(false);
}
}
public void actionPerformed(ActionEvent ae)
{
Object o=ae.getSource();
if(o==btnSend && !txtContent.getText().equals(""))
{
if(ckbSilent.isSelected())
{
taInfo.append("\n【你】悄悄對(duì)"+"【"+cmbUser.getSelectedItem()+"】說: "+txtContent.getText());
txtContent.requestFocus();
}
else
{
taInfo.append("\n【你】對(duì)"+"【"+cmbUser.getSelectedItem()+"】說: "+txtContent.getText());
txtContent.requestFocus();
}
}
if(o.equals(btnExit)||o.equals(mnuiExit))
{
int option=JOptionPane.showConfirmDialog(null, "確定要退出嗎?", "提示", JOptionPane.YES_NO_OPTION);
if(option==0)
System.exit(0);
}
if(o.equals(btnCls)||o.equals(mnuiCls))
taInfo.setText("");
if(o.equals(btnBrowse)||o.equals(mnuiOpen)||o.equals(popOpen))
{
openFile();
}
if(o.equals(btnSave)||o.equals(popSave)||o.equals(mnuiSave))
{
toSave();
}
if(o.equals(btnClock)||o.equals(mnuiClock))
new Clock();
}
// 打開文件方法
public void openFile()
{
filter=new FileNameExtensionFilter("文本文件","txt");
fc.setFileFilter(filter);
int temp=fc.showOpenDialog(this );
String strFile=fc.getSelectedFile().toString();
if(temp==JFileChooser.APPROVE_OPTION)
{
try
{
//FileReader ras=new FileReader(strFile);
//temp=ras.readLine();
//System.out.println(String.valueOf(ras.read());
// while((strFile=ras.readLine())!=null)
// taInfo.append(strFile);
}
catch(Exception e){JOptionPane.showMessageDialog(null,"讀取失敗!");}
}
}
//保存按鈕方法
public void toSave()
{
if(!taInfo.getText().equals(null))
{
filter = new FileNameExtensionFilter("txt", ".txt");
fc.setFileFilter(filter);
int temp=fc.showSaveDialog(this);
String strFile=fc.getSelectedFile().toString();
if(strFile.indexOf(".txt")==-1)
{
strFile=strFile+".txt";
}
if(temp==JFileChooser.APPROVE_OPTION)
{
try
{
fw=new FileWriter(strFile);
fw.write(taInfo.getText());
fw.close();
JOptionPane.showMessageDialog(null, "保存成功!");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "保存失敗!");
}
}
}
}
public void valueChanged(ListSelectionEvent lse)
{
Boolean tUser=true;
String strUser=String.valueOf(lstUser.getSelectedValue());
for(int i=0;i<cmbUser.getItemCount();i++)
{
if(strUser==String.valueOf(cmbUser.getItemAt(i)))
{
tUser=false;
break;
}
}
if(tUser)
cmbUser.addItem(strUser);
}
/**
* @param args
*/
public static void main(String[] args) {
new Chat();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -