📄 schattingframe.java
字号:
import java.io.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.net.*;
/**
* 主窗口
*
* @author 戚荣波
*
* @version 1.0
* @since J2SE 1.6
*/
public class SChattingFrame extends JFrame
{
private int upGap = 10,downGap = 10,leftGap = 10,rightGap = 10,gap = 10;//间隙
private int m_spShowHeight = 250;
private int m_spWriteHeight = 70;
private int m_tbStyleHeight = 25;
private int m_spWriteWidth = 330,m_spShowWidth = 330;
private int m_buttonHeight = 20;
private int m_buttonWidth = 80;
//private int m_photoWidth = 140;
private int m_photoWidth = 0;
private int m_photoHeight;
private int m_height,m_width;
public static SPopupMenu pm = new SPopupMenu();
private JTextPane tp_show = new JTextPane(); //显示聊天记录
private JTextPane tp_write = new JTextPane();//输入文本区域
private JButton b_history = new SButton("History");
private JButton b_disconnect = new SButton("Disconnect");
private JButton b_close = new SButton("Close");
private JButton b_send = new SButton("Send");
private JScrollPane sp_show = new JScrollPane(tp_show);
private JScrollPane sp_write = new JScrollPane(tp_write);
private StyleToolBar tb = new StyleToolBar();
private FontToolBar ftb = new FontToolBar();
private Container c;
private Dimension s;
private Color textPaneColor = new Color(255,255,255);
public SChattingFrame()
{
super("SChatting");
//弹出输入窗口获得用户名
String userName = JOptionPane.showInputDialog(ShowWindow.app,"Please input your name");
try
{
if(userName == null||userName.equals(""))userName = InetAddress.getLocalHost().getHostAddress();
}
catch(UnknownHostException uhe)
{
userName = "no name";
}
SomethingStored.userName = userName;
c = getContentPane();
s = c.getSize();
setJMenuBar(new SChattingMenu());//载入菜单
c.setLayout(null);//无布局管理
//c.setForeground(ShowWindow.myColor);
c.setBackground(ShowWindow.myColor);//设置背景色
tp_show.setBackground(textPaneColor);
tp_show.setEditable(false);
SomethingStored.tp_show = tp_show;
SomethingStored.tp_write = tp_write;
SomethingStored.sp_show = sp_show;
SomethingStored.fontBar = ftb;
//加入组件
c.add(ftb);
c.add(tb);
c.add(sp_show,"show");
c.add(sp_write,"write");
c.add(b_history);
//c.add(b_disconnect);
c.add(b_close);
c.add(b_send);
//事件监听器
b_send.addActionListener(new sendListener());
b_close.addActionListener(new closeListener());
b_history.addActionListener(new viewListener());
tp_show.addMouseListener(new rightButtonClicked(false));
tp_write.addMouseListener(new rightButtonClicked(true));
//默认字体
SimpleAttributeSet myAttrib = new SimpleAttributeSet();
StyleConstants.setBold(myAttrib,false);
StyleConstants.setItalic(myAttrib,false);
StyleConstants.setFontSize(myAttrib,12);
StyleConstants.setForeground(myAttrib,Color.black);
StyleConstants.setFontFamily(myAttrib,"宋体");
SomethingStored.myAttributeSet = new SimpleAttributeSet(myAttrib);//设置的格式
SomethingStored.defaultAttributeSet = new SimpleAttributeSet(myAttrib);//普通的格式
SomethingStored.systemMessageAttributeSet = new SimpleAttributeSet(myAttrib);//系统消息的格式
StyleConstants.setForeground(SomethingStored.systemMessageAttributeSet,Color.red);
tp_write.setFont(new Font("宋体",0,12));
showMessage(SomethingStored.userName+" ...ready");
//通过resize方法来更新各组件的位置以及大小
this.addComponentListener(new ComponentListener()
{
public void componentMoved(ComponentEvent e)
{
}
public void componentShown(ComponentEvent e)
{
}
public void componentHidden(ComponentEvent e)
{
}
public void componentResized(ComponentEvent e)
{
SChattingFrame f = (SChattingFrame)e.getSource();
c = f.getContentPane();
c.setLayout(null);
s = c.getSize();
m_height = (int)s.getHeight();
m_width = (int)s.getWidth();
m_spShowWidth = m_width - leftGap - rightGap - m_photoWidth - gap;
m_spWriteWidth = m_spShowWidth;
m_spShowHeight = m_height - upGap - gap*3 - m_buttonHeight - m_spWriteHeight - m_tbStyleHeight;
int m_buttonY = m_height-m_buttonHeight-downGap;
sp_show.setBounds(leftGap,upGap,m_spShowWidth,m_spShowHeight);
sp_show.updateUI();
tb.setBounds(leftGap,upGap+m_spShowHeight + gap,m_spWriteWidth,m_tbStyleHeight);
sp_write.setBounds(leftGap,upGap + m_spShowHeight + gap*2 + m_tbStyleHeight,m_spWriteWidth,m_spWriteHeight);
sp_write.updateUI();
b_history.setBounds(leftGap,m_buttonY,m_buttonWidth,m_buttonHeight);
//b_disconnect.setBounds(leftGap + m_buttonWidth,m_buttonY,m_buttonWidth,m_buttonHeight);
b_close.setBounds(leftGap + m_spShowWidth - m_buttonWidth*2,m_buttonY,m_buttonWidth,m_buttonHeight);
b_send.setBounds(leftGap + m_spShowWidth - m_buttonWidth,m_buttonY,m_buttonWidth,m_buttonHeight);
f.repaint();
}
}
);
}
/**
*在显示区域输出系统消息
*
*@param str 所要输出的字符串
*/
public static void showMessage(String str)
{
try
{
SomethingStored.fontBar.setVisible(false);
StyledDocument doc = SomethingStored.tp_show.getStyledDocument();
doc.insertString
(doc.getLength(),str+"\n",SomethingStored.systemMessageAttributeSet);
SomethingStored.tp_show.setCaretPosition(doc.getLength());
}
catch(BadLocationException e)
{
System.err.println("BadLocationException"+e);
e.printStackTrace();
}
}
/**
*在显示区域输出图像
*
*@param position 图像输出的位置
*@param str 文件路径
*/
public static void showPicture(int position,String str)
{
SomethingStored.fontBar.setVisible(false);
JTextPane tp = SomethingStored.tp_show;
tp.setCaretPosition(position);
tp.insertIcon(new ImageIcon(str));
tp.setCaretPosition(tp.getStyledDocument().getLength());
}
/**
*声音的播放
*
*@param name 对应的声音文件名,文件在程序所在目录下的sound子目录下
*/
public static void audioPlay(String name)
{
AudioClip sound;
try
{
sound = Applet.newAudioClip(new URL("file:Sound\\"+name));
}
catch(Exception e)
{
return;
}
sound.play();
}
/**
*以SMessage的构造方法输出SMessage对象
*
*/
public static void writeSMessage(int messageType,String nowTime,String str,SimpleAttributeSet att)
{
try
{
SomethingStored.m_output.writeObject(
new SMessage(messageType,nowTime,str,att));
}
catch(IOException ioe)
{
}
}
/**
*输出SMessage对象
*
*@param aMessage 要输出的对象
*
*/
public static void writeSMessage(SMessage aMessage)
{
try
{
SomethingStored.m_output.writeObject(aMessage);
}
catch(IOException ioe)
{
Connect.mb_disconnect();
}
}
}
/**
* 一些常用参数的存储
* @author 戚荣波
*
* @version 1.0
* @since J2SE 1.6
*/
final class SomethingStored
{
protected static String userName;
protected static String yourName;
protected static JScrollPane sp_show;
protected static FontToolBar fontBar;
protected static JTextPane tp_show;
protected static JTextPane tp_write;
protected static Socket s;
protected static ObjectInputStream m_input;
protected static ObjectOutputStream m_output;
protected static InetAddress destIP;
protected static ServerSocket server;
protected static InetAddress host;
protected static final int port = 7777;
protected static final int filePort = 7778;
protected static Color defaultfontColor = Color.black;
protected static Color fontColor = Color.black;
protected static SimpleAttributeSet myAttributeSet;
protected static SimpleAttributeSet defaultAttributeSet;
protected static SimpleAttributeSet systemMessageAttributeSet;
protected static FileServerThread wait;
protected static boolean isSending = false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -