java组播聊天室1
|
tutu
tutu
铜牌会员
|
1 # 大 中 小 发表于 2008-05-12 22:01:06
不用服务端 所有控制都在应用层 还差一个类在下一贴 那个类是复制CSDN网友5斗米的:)
import java.io.*;
import java.net.*;
import java.io.File;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class HelloChater extends JFrame implements WindowListener , ActionListener , ItemListener ,MouseListener {
private JPanel jContentPane = null;
private JPanel mainUpjPanel = null;
private JPanel literatureUpjPanel = null;
private JPanel musicUpjPanel = null;
private JPanel computerUpjPanel = null;
private JPanel sportUpjPanel = null;
private JPanel loveUpjPanel = null;
private JPanel englishUpjPanel = null;
private JScrollPane mainJScrollPane = null;
private JScrollPane literatureJScrollPane = null;
private JScrollPane musicJScrollPane = null;
private JScrollPane computerJScrollPane = null;
private JScrollPane sportJScrollPane = null;
private JScrollPane loveJScrollPane = null;
private JScrollPane englishJScrollPane = null;
private JPanel mainRightJPanel = null;
private JPanel literatureRightJPanel = null;
private JPanel musicRightJPanel = null;
private JPanel computerRightJPanel = null;
private JPanel sportRightJPanel = null;
private JPanel loveRightJPanel = null;
private JPanel englishRightJPanel = null;
private JTextPane mainOutput = null;
private JTextPane literatureOutput = null;
private JTextPane musicOutput = null;
private JTextPane computerOutput = null;
private JTextPane sportOutput = null;
private JTextPane loveOutput = null;
private JTextPane englishOutput = null;
private JTextArea mainAnnouncement = null;
private JTextArea literatureAnnouncement = null;
private JTextArea musicAnnouncement = null;
private JTextArea computerAnnouncement = null;
private JTextArea sportAnnouncement = null;
private JTextArea loveAnnouncement = null;
private JTextArea englishAnnouncement = null;
private List mainNameList = null;
private List literatureNameList = null;
private List musicNameList = null;
private List computerNameList = null;
private List sportNameList = null;
private List loveNameList = null;
private List englishNameList = null;
private JButton mainButton = null;
private JButton literatureButton = null;
private JButton musicButton = null;
private JButton computerButton = null;
private JButton sportButton = null;
private JButton loveButton = null;
private JButton englishButton = null;
private JButton aboutButton = null;
private JTextField showNameTextField = null;
private JButton joinButton = null;
private JComboBox userJComboBox = null;
private JButton secretButton = null;
private JButton clearButton = null;
private JComboBox fontSizeJComboBox = null;
private JComboBox fontStyleJComboBox = null;
private JComboBox fontColorJComboBox = null;
private JButton picButton = null;
private JTextArea input = null;
private JButton sendButton = null;
private JButton saveButton = null;
private JScrollPane messageJScrollPane = null;
private StyledDocument doc1 = null;
private StyledDocument doc2 = null;
private StyledDocument doc3 = null;
private StyledDocument doc4 = null;
private StyledDocument doc5 = null;
private StyledDocument doc6 = null;
private StyledDocument doc7 = null;
MulticastSocket s1,s2,s3,s4,s5,s6,s7 = null; //代表七个频道
protected InetAddress group = null; //组播地址
static String msg=null; //用于存放准备发送的消息
int localSize = 12; //用于存放本地设置的字体大小
int localStyle = 0; //用于存放本地设置的字体风格
int intSize = 0; //用于存放接收到的消息的字体大小
int intStyle = 0; //用于存放接收到的消息的字体风格
static int intColor = 0;
String strImage = null;
File localImage = null; //从本地取图象文件
int login = 0; //1表示已经连接网络 0表示未连接网络
String localName = null; //存放自己的用户名
String[] names = new String[1000]; //存放在线用户
int userCount = 0; //记录用户数量
int userCount1 = 0;
int flag = 0; // ????
DatagramPacket dataName = null; //有人登陆就发送!
int nameFlag = 0; //当连接后,变成1 并停止接收用户列表
String sendName = null ; //发送给谁? 用这个做修饰 被userJComboBox.getSelectedItem()复值
String secretFlag = null; //是否按下密聊按扭,两种状态,字符内容不同
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常规", "粗体", "斜体", "粗斜体" };
String[] str_Color = { "黑色", "红色", "蓝色", "黄色", "绿色" };
int myselfFlag = 0;
FontAttrib att = new FontAttrib(); //!!!
FontAttrib at = new FontAttrib(); //!!!
int m = 0;
String tempName = null; int defend = 0; //保护符
/**
* 构造方法
*/
public HelloChater() {
initialize();
names[userCount++] = "所有人";
}
/*
*此方法用于加入组播网络
*/
private synchronized void initNet() throws IOException {
try {
group = InetAddress.getByName("224.1.2.1");
} catch (UnknownHostException e1) {
System.out.println("组播地址绑定失败");
}
try {
s1 = new MulticastSocket(6001);
s2 = new MulticastSocket(6002);
s3 = new MulticastSocket(6003);
s4 = new MulticastSocket(6004);
s5 = new MulticastSocket(6005);
s6 = new MulticastSocket(6006);
s7 = new MulticastSocket(6007);
s1.setTimeToLive(3);
s2.setTimeToLive(3);
s3.setTimeToLive(3);
s4.setTimeToLive(3);
s5.setTimeToLive(3);
s6.setTimeToLive(3);
s7.setTimeToLive(3);
} catch (IOException e1) {
System.out.println("组播地址创建失败");
}
try {
s1.joinGroup(group);
s2.joinGroup(group);
s3.joinGroup(group);
s4.joinGroup(group);
s5.joinGroup(group);
s6.joinGroup(group);
s7.joinGroup(group);
} catch (IOException e1) {
System.out.println("组播地址加入失败");
}
recevMsg r1=new recevMsg(s1);
r1.start();
recevMsg r2=new recevMsg(s2);
r2.start();
recevMsg r3=new recevMsg(s3);
r3.start();
recevMsg r4=new recevMsg(s4);
r4.start();
recevMsg r5=new recevMsg(s5);
r5.start();
recevMsg r6=new recevMsg(s6);
r6.start();
recevMsg r7=new recevMsg(s7);
r7.start();
}
/*
* 内部类,负责接受消息,继承Thread类
*/
class recevMsg extends Thread {
MulticastSocket s = null;
int sFlag;
String str = null; //存放接收到消息字符串
public recevMsg(MulticastSocket s) {
sFlag = s.getLocalPort();
this.s=s;
}
/*重载Thread类的run方法
*/
public synchronized void run() {
byte[] buf = new byte[50000];
DatagramPacket recv = new DatagramPacket(buf, buf.length);
try {
while(true) {
s.receive(recv);
str=new String(recv.getData(),0, recv.getLength());
if(str.substring(4,5).equals("P"))
flag = 1;
else {
String strColor = str.substring(3,4);
intColor = Integer.parseInt(strColor);
String strSize = str.substring(0,2);
String strStyle = str.substring(2,3);
intSize = Integer.parseInt(strSize);
intStyle = Integer.parseInt(strStyle);
str = str.substring(4);
if(intStyle == 0)
at.setStyle(0);
if(intStyle == 1)
at.setStyle(1);
if(intStyle == 2)
at.setStyle(2);
if(intStyle == 3)
at.setStyle(3);
if(intSize == 12)
at.setSize(12);
if(intSize == 14)
at.setSize(14);
if(intSize == 18)
at.setSize(18);
if(intSize == 22)
at.setSize(22);
if(intSize == 30)
at.setSize(30);
if(intSize == 40)
at.setSize(40);
if(intColor == 0)
at.setColor(new Color(0, 0, 0));
if(intColor == 1)
at.setColor(new Color(255, 0, 0));
if(intColor == 2)
at.setColor(new Color(0, 0, 255));
if(intColor == 3)
at.setColor(new Color(255, 255, 0));
if(intColor == 4)
at.setColor(new Color(0, 255, 0));
}
if(sFlag == 6001)
{
if(flag == 0) {
String st = null;
int i = str.length();
int k = 0;
int x = 0;
if(!str.substring(0,1).equals(" ") && !str.substring(0,1).equals("N"))
{
receiveMessageHandle(mainOutput,doc1);
}
if(str.substring(0,1).equals(" ") && localName.equals(str.substring(1,i-4))) {
m++;
if(m == 2)
tempName = localName;
if(localName.equals(tempName))
{
System.out.println("看下这里");
defend = 1;
st = "1211TTT" + localName;
dataName = new DatagramPacket(st.getBytes(),st.getBytes().length,group, 6001);
s.send(dataName);
}
}
if(str.substring(0,3).equals("TTT") && localName.equals(str.substring(3)) && defend == 0) {
st = "1211" + " " + localName + "下线了";
dataName = new DatagramPacket(st.getBytes(),st.getBytes().length,group, 6001);
s.send(dataName);
new JOptionPane().showMessageDialog(null, "用户名被占用,退出后请更换");
System.exit(0);
}
if(str.substring(0,1).equals(" ") && !localName.equals(str.substring(1,i-4))) {
at.setText(str);
insert(doc1,at);
nameFlag = 0;
if(str.substring(i-3).equals("下线了")) {
System.out.println("hehe");
System.out.println(str.substring(1,i-4));
userCount--;
for(k = 0;k < userCount;k++) {
if(names[k].equals(str.substring(1,i-3)))
break;
}
names[999] = names[k];
for(x = k;x<userCount;x++)
{
names[x] = names[x+1];
}
userJComboBox.removeItem(str.substring(1,i-3));
mainNameList.remove(str.substring(1,i-3));literatureNameList.remove(str.substring(1,i-3));
musicNameList.remove(str.substring(1,i-3));computerNameList.remove(str.substring(1,i-3));
sportNameList.remove(str.substring(1,i-3));loveNameList.remove(str.substring(1,i-3));
englishNameList.remove(str.substring(1,i-3));
if(str.substring(1,i-3).equals(localName)) {
mainNameList.add(localName);
st = "1211N" + localName;
dataName = new DatagramPacket(st.getBytes(),st.getBytes().length,group, 6001);
s.send(dataName);
}
}
else
{
names[userCount++] = str.substring(1,i-4);
userJComboBox.addItem(names[userCount-1]);
mainNameList.add(names[userCount-1]);literatureNameList.add(names[userCount-1]);
musicNameList.add(names[userCount-1]);computerNameList.add(names[userCount-1]);
sportNameList.add(names[userCount-1]);loveNameList.add(names[userCount-1]);
englishNameList.add(names[userCount-1]);
st = "1211N" + localName;
dataName = new DatagramPacket(st.getBytes(),st.getBytes().length,group, 6001);
s.send(dataName);
}
}
else
if(str.substring(0,1).equals("N") && nameFlag == 1) {
names[userCount++] = str.substring(1);
userJComboBox.addItem(str.substring(1));
mainNameList.add(str.substring(1));literatureNameList.add(str.substring(1));
musicNameList.add(str.substring(1));computerNameList.add(str.substring(1));
sportNameList.add(str.substring(1));loveNameList.add(str.substring(1));
englishNameList.add(str.substring(1));
}
}
else{
String tempName = str.substring(5,str.length()-6);
strImage = "C:\\pic\\" + str.substring(str.length()-6);//+ ".gif";
localImage = new File(strImage);
at.setText(tempName + ": ");
try
{
doc1.insertString(doc1.getLength(), at.getText() , at.getAttrSet());
}catch(BadLocationException ble) {
System.out.println("NONO");
}
insertIcon(localImage,mainOutput,doc1);
flag = 0;
mainOutput.setSelectionStart(doc1.getLength());
}
}
if(sFlag == 6002)
{
if(flag == 0) {
String st = null;
int i = str.length();
int j = 0;
if(!str.substring(0,1).equals(" ") && !str.substring(0,1).equals("N"))
{
receiveMessageHandle(literatureOutput,doc2);
}
if(str.substring(0,1).equals(" ") && !localName.equals(str.substring(1,i-4))) {
at.setText(str);
insert(doc2,at);
}
}
else {
String tempName = str.substring(5,str.length()-6);
strImage = "C:\\pic\\" + str.substring(str.length()-6);//+ ".gif";
localImage = new File(strImage);
at.setText(tempName + ": ");
try
{
doc2.insertString(doc2.getLength(), at.getText() , at.getAttrSet());
}catch(BadLocationException ble) {
System.out.println("NONO");
}
insertIcon(localImage,literatureOutput,doc2);
flag = 0;
literatureOutput.setSelectionStart(doc2.getLength());
}
}
if(sFlag == 6003)
{
if(flag == 0) {
String st = null;
int i = str.length();
int j = 0;
if(!str.substring(0,1).equals(" ") && !str.substring(0,1).equals("N"))
{
receiveMessageHandle(musicOutput,doc3);
}
if(str.substring(0,1).equals(" ") && !localName.equals(str.substring(1,i-4))) {
at.setText(str);
insert(doc3,at);
}
}
else{
String tempName = str.substring(5,str.length()-6);
strImage = "C:\\pic\\" + str.substring(str.length()-6);//+ ".gif";
localImage = new File(strImage);
at.setText(tempName + ": ");
try
{
doc3.insertString(doc3.getLength(), at.getText() , at.getAttrSet());
}catch(BadLocationException ble) {
System.out.println("NONO");
}
insertIcon(localImage,musicOutput,doc3);
flag = 0;
musicOutput.setSelectionStart(doc3.getLength());
}
}
if(sFlag == 6004)
{
if(flag == 0) {
String st = null;
int i = str.length();
int j = 0;
if(!str.substring(0,1).equals(" ") && !str.substring(0,1).equals("N"))
{
receiveMessageHandle(computerOutput,doc4);
}
if(str.substring(0,1).equals(" ") && !localName.equals(str.substring(1,i-4))) {
at.setText(str);
insert(doc4,at);
}
}
else{
String tempName = str.substring(5,str.length()-6);
strImage = "C:\\pic\\" + str.substring(str.length()-6);//+ ".gif";
localImage = new File(strImage);
at.setText(tempName + ": ");
try
{
doc4.insertString(doc4.getLength(), at.getText() , at.getAttrSet());
}catch(BadLocationException ble) {
System.out.println("NONO");
}
insertIcon(localImage,computerOutput,doc4);
flag = 0;
computerOutput.setSelectionStart(doc4.getLength());
}
}
if(sFlag == 6005)
{
if(flag == 0) {
String st = null;
int i = str.length();
int j = 0;
if(!str.substring(0,1).equals(" ") && !str.substring(0,1).equals("N"))
{
receiveMessageHandle(sportOutput,doc5);
}
if(str.substring(0,1).equals(" ") && !localName.equals(str.substring(1,i-4))) {
at.setText(str);
insert(doc5,at);
}
}
else {
String tempName = str.substring(5,str.length()-6);
strImage = "C:\\pic\\" + str.substring(str.length()-6);//+ ".gif";
localImage = new File(strImage);
at.setText(tempName + ": ");
try
{
doc5.insertString(doc5.getLength(), at.getText() , at.getAttrSet());
}catch(BadLocationException ble) {
System.out.println("NONO");
}
insertIcon(localImage,sportOutput,doc5);
flag = 0;
sportOutput.setSelectionStart(doc5.getLength());
}
}
if(sFlag == 6006)
{
if(flag == 0) {
String st = null;
int i = str.length();
int j = 0;
if(!str.substring(0,1).equals(" ") && !str.substring(0,1).equals("N"))
{
receiveMessageHandle(loveOutput,doc6);
}
if(str.substring(0,1).equals(" ") && !localName.equals(str.substring(1,i-4))) {
at.setText(str);
insert(doc6,at);
}
}
else{
String tempName = str.substring(5,str.length()-6);
strImage = "C:\\pic\\" + str.substring(str.length()-6);//+ ".gif";
localImage = new File(strImage);
at.setText(tempName + ": ");
try
{
doc6.insertString(doc6.getLength(), at.getText() , at.getAttrSet());
}catch(BadLocationException ble) {
System.out.println("NONO");
}
insertIcon(localImage,loveOutput,doc6);
flag = 0;
loveOutput.setSelectionStart(doc6.getLength());
}
}
if(sFlag == 6007) {
if(flag == 0) {
String st = null;
int i = str.length();
int j = 0;
if(!str.substring(0,1).equals(" ") && !str.substring(0,1).equals("N"))
{
receiveMessageHandle(englishOutput,doc7);
}
if(str.substring(0,1).equals(" ") && !localName.equals(str.substring(1,i-4))) {
at.setText(str);
insert(doc7,at);
}
}
else{
String tempName = str.substring(5,str.length()-6);
strImage = "C:\\pic\\" + str.substring(str.length()-6);//+ ".gif";
localImage = new File(strImage);
at.setText(tempName + ": ");
try
{
doc7.insertString(doc7.getLength(), at.getText() , at.getAttrSet());
}catch(BadLocationException ble) {
System.out.println("NONO");
}
insertIcon(localImage,englishOutput,doc7);
flag = 0;
englishOutput.setSelectionStart(doc7.getLength());
}
}
}
}catch (IOException e1){
System.out.println("接受失败");
}
}
/**
* 此静态方法把接收到的消息进行分类处理
*/
public void receiveMessageHandle(JTextPane output,StyledDocument doc) {
if(str.indexOf("#SeCrEt#悄悄") != -1 && str.indexOf(localName) == -1 ) {
}
else
if(str.indexOf("#P" + localName + "@P") != -1 ) {
str = replace(str, "#P"+localName+"@P", "你");
str = str.replaceAll("#SeCrEt#","");
at.setText(str);
insert(doc,at);
output.setSelectionStart(doc.getLength());
}
else
if(str.indexOf(sendName) != -1 && !userJComboBox.getSelectedItem().equals("所有人")) {
str = str.replaceAll("#SeCrEt#","");
str = replace(str, sendName, "对"+ userJComboBox.getSelectedItem());
at.setText(str);
insert(doc,at);
output.setSelectionStart(doc.getLength());
}
else
if(str.indexOf("#P") != -1 && str.indexOf("@P") != -1) {
str = str.replaceAll( "#", "");
str = str.replaceAll( "P", "");
str = str.replaceAll( "@", "");
at.setText(str);
insert(doc,at);
output.setSelectionStart(doc.getLength());
}
else {
at.setText(str);
insert(doc,at);
output.setSelectionStart(doc.getLength());
}
}
}
//此静态方法对接受的消息字符串进行替换,类似String类的replace方法。
public static String replace(String line, String oldString,String newString){
if (line == null) {
return null;
}
int i = 0;
if ( (i = line.indexOf(oldString, i)) >= 0) {
char[] line2 = line.toCharArray(); //字符串放入数组
char[] newString2 = newString.toCharArray(); //要替换的字符串
int oLength = oldString.length(); //被替换的字符串的长度
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ( (i = line.indexOf(oldString, i)) > 0) {
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
*此方法用于初始化窗体
*
* @return void
*/
private synchronized void initialize() {
this.setSize(658, 438);
this.setResizable(false);
this.setContentPane(getJContentPane());
this.addWindowListener(this);
this.setTitle("HelloChater! 试用版本1.0");
this.setVisible(true);
}
/**
* 此方法用于初始化窗体中的jContentPane控件
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setPreferredSize(new Dimension(300, 200));
jContentPane.add(getMainUpjPanel(), null);
jContentPane.add(getLiteratureUpjPanel(), null);
jContentPane.add(getMusicUpjPanel(), null);
jContentPane.add(getComputerUpjPanel(), null);
jContentPane.add(getSportUpjPanel(), null);
jContentPane.add(getLoveUpjPanel(), null);
jContentPane.add(getEnglishUpjPanel(), null);
jContentPane.add(getMainButton(), null);
jContentPane.add(getLiteratureButton(), null);
jContentPane.add(getMusicButton(), null);
jContentPane.add(getComputerButton(), null);
jContentPane.add(getSportButton(), null);
jContentPane.add(getLoveButton(), null);
jContentPane.add(getEnglishButton(), null);
jContentPane.add(getAboutButton(), null);
jContentPane.add(getShowNameTextField(), null);
jContentPane.add(getJoinButton(), null);
jContentPane.add(getUserJComboBox(), null);
jContentPane.add(getSecretButton(), null);
jContentPane.add(getClearButton(), null);
jContentPane.add(getFontSizeJComboBox(), null);
jContentPane.add(getFontStyleJComboBox(), null);
jContentPane.add(getFontColorJComboBox(), null);
jContentPane.add(getPicButton(), null);
jContentPane.add(getMessageJScrollPane(), null);
jContentPane.add(getSendButton(), null);
jContentPane.add(getSaveButton(), null);
}
return jContentPane;
}
/**
* 下面七个方法用于初始化jPanel(七个用来装载上层控件的容器)
*
* @return javax.swing.JPanel
*/
private JPanel getMainUpjPanel() {
if (mainUpjPanel == null) {
mainUpjPanel = new JPanel();
mainUpjPanel.setLayout(new BoxLayout(getMainUpjPanel(), BoxLayout.X_AXIS));
mainUpjPanel.setPreferredSize(new Dimension(300, 200));
mainUpjPanel.setBounds(new Rectangle(0, 0, 649, 317));
mainUpjPanel.add(getMainJScrollPane(), null);
mainUpjPanel.add(getMainRightJPanel(),null);
}
return mainUpjPanel;
}
private JPanel getLiteratureUpjPanel() {
if (literatureUpjPanel == null) {
literatureUpjPanel = new JPanel();
literatureUpjPanel.setLayout(new BoxLayout(getLiteratureUpjPanel(), BoxLayout.X_AXIS));
literatureUpjPanel.setPreferredSize(new Dimension(300, 200));
literatureUpjPanel.setBounds(new Rectangle(0, 0, 649, 317));
literatureUpjPanel.add(getLiteratureJScrollPane(), null);
literatureUpjPanel.add(getLiteratureRightJPanel(),null);
literatureUpjPanel.setVisible(false);
}
return literatureUpjPanel;
}
private JPanel getMusicUpjPanel() {
if (musicUpjPanel == null) {
musicUpjPanel = new JPanel();
musicUpjPanel.setLayout(new BoxLayout(getMusicUpjPanel(), BoxLayout.X_AXIS));
musicUpjPanel.setPreferredSize(new Dimension(300, 200));
musicUpjPanel.setBounds(new Rectangle(0, 0, 649, 317));
musicUpjPanel.add(getMusicJScrollPane(), null);
musicUpjPanel.add(getMusicRightJPanel(),null);
musicUpjPanel.setVisible(false);
}
return musicUpjPanel;
}
private JPanel getComputerUpjPanel() {
if (computerUpjPanel == null) {
computerUpjPanel = new JPanel();
computerUpjPanel.setLayout(new BoxLayout(getComputerUpjPanel(), BoxLayout.X_AXIS));
computerUpjPanel.setPreferredSize(new Dimension(300, 200));
computerUpjPanel.setBounds(new Rectangle(0, 0, 649, 317));
computerUpjPanel.add(getComputerJScrollPane(), null);
computerUpjPanel.add(getComputerRightJPanel(),null);
computerUpjPanel.setVisible(false);
}
return computerUpjPanel;
}
private JPanel getSportUpjPanel() {
if (sportUpjPanel == null) {
sportUpjPanel = new JPanel();
sportUpjPanel.setLayout(new BoxLayout(getSportUpjPanel(), BoxLayout.X_AXIS));
sportUpjPanel.setPreferredSize(new Dimension(300, 200));
sportUpjPanel.setBounds(new Rectangle(0, 0, 649, 317));
sportUpjPanel.add(getSportJScrollPane(), null);
sportUpjPanel.add(getSportRightJPanel(),null);
sportUpjPanel.setVisible(false);
}
return sportUpjPanel;
}
private JPanel getLoveUpjPanel() {
if (loveUpjPanel == null) {
loveUpjPanel = new JPanel();
loveUpjPanel.setLayout(new BoxLayout(getLoveUpjPanel(), BoxLayout.X_AXIS));
loveUpjPanel.setPreferredSize(new Dimension(300, 200));
loveUpjPanel.setBounds(new Rectangle(0, 0, 649, 317));
loveUpjPanel.add(getLoveJScrollPane(), null);
loveUpjPanel.add(getLoveRightJPanel(),null);
loveUpjPanel.setVisible(false);
}
return loveUpjPanel;
}
private JPanel getEnglishUpjPanel() {
if (englishUpjPanel == null) {
englishUpjPanel = new JPanel();
englishUpjPanel.setLayout(new BoxLayout(getEnglishUpjPanel(), BoxLayout.X_AXIS));
englishUpjPanel.setPreferredSize(new Dimension(300, 200));
englishUpjPanel.setBounds(new Rectangle(0, 0, 649, 317));
englishUpjPanel.add(getEnglishJScrollPane(), null);
englishUpjPanel.add(getEnglishRightJPanel(),null);
englishUpjPanel.setVisible(false);
}
return englishUpjPanel;
}
/**
* 下面七个方法用于初始化JScrollPane(七个用于装载消息框的容器)
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getMainJScrollPane() {
if (mainJScrollPane == null) {
mainJScrollPane = new JScrollPane();
mainJScrollPane.setPreferredSize(new Dimension(470, 50));
mainJScrollPane.setViewportView(getMainOutput());
}
return mainJScrollPane;
}
private JScrollPane getLiteratureJScrollPane() {
if (literatureJScrollPane == null) {
literatureJScrollPane = new JScrollPane();
literatureJScrollPane.setPreferredSize(new Dimension(470, 50));
literatureJScrollPane.setViewportView(getLiteratureOutput());
}
return literatureJScrollPane;
}
private JScrollPane getMusicJScrollPane() {
if (musicJScrollPane == null) {
musicJScrollPane = new JScrollPane();
musicJScrollPane.setPreferredSize(new Dimension(470, 50));
musicJScrollPane.setViewportView(getMusicOutput());
}
return musicJScrollPane;
}
private JScrollPane getComputerJScrollPane() {
if (computerJScrollPane == null) {
computerJScrollPane = new JScrollPane();
computerJScrollPane.setPreferredSize(new Dimension(470, 50));
computerJScrollPane.setViewportView(getComputerOutput());
}
return computerJScrollPane;
}
private JScrollPane getSportJScrollPane() {
if (sportJScrollPane == null) {
sportJScrollPane = new JScrollPane();
sportJScrollPane.setPreferredSize(new Dimension(470, 50));
sportJScrollPane.setViewportView(getSportOutput());
}
return sportJScrollPane;
}
private JScrollPane getLoveJScrollPane() {
if (loveJScrollPane == null) {
loveJScrollPane = new JScrollPane();
loveJScrollPane.setPreferredSize(new Dimension(470, 50));
loveJScrollPane.setViewportView(getLoveOutput());
}
return loveJScrollPane;
}
private JScrollPane getEnglishJScrollPane() {
if (englishJScrollPane == null) {
englishJScrollPane = new JScrollPane();
englishJScrollPane.setPreferredSize(new Dimension(470, 50));
englishJScrollPane.setViewportView(getEnglishOutput());
}
return englishJScrollPane;
}
/**
* 下面七个方法用于初始化RightJPanel(用于装载公告栏于用户列表的容器)
*
* @return javax.swing.JPanel
*/
private JPanel getMainRightJPanel() {
if (mainRightJPanel == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(2);
mainRightJPanel = new JPanel();
mainRightJPanel.setLayout(gridLayout);
mainRightJPanel.add(getMainAnnouncement(), null);
mainRightJPanel.add(getMainNameList(), null);
}
return mainRightJPanel;
}
private JPanel getLiteratureRightJPanel() {
if (literatureRightJPanel == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(2);
literatureRightJPanel = new JPanel();
literatureRightJPanel.setLayout(gridLayout);
literatureRightJPanel.add(getLiteratureAnnouncement(), null);
literatureRightJPanel.add(getLiteratureNameList(), null);
}
return literatureRightJPanel;
}
private JPanel getMusicRightJPanel() {
if (musicRightJPanel == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(2);
musicRightJPanel = new JPanel();
musicRightJPanel.setLayout(gridLayout);
musicRightJPanel.add(getMusicAnnouncement(), null);
musicRightJPanel.add(getMusicNameList(), null);
}
return musicRightJPanel;
}
private JPanel getSportRightJPanel() {
if (sportRightJPanel == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(2);
sportRightJPanel = new JPanel();
sportRightJPanel.setLayout(gridLayout);
sportRightJPanel.add(getSportAnnouncement(), null);
sportRightJPanel.add(getSportNameList(), null);
}
return sportRightJPanel;
}
private JPanel getComputerRightJPanel() {
if (computerRightJPanel == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(2);
computerRightJPanel = new JPanel();
computerRightJPanel.setLayout(gridLayout);
computerRightJPanel.add(getComputerAnnouncement(), null);
computerRightJPanel.add(getComputerNameList(), null);
}
return computerRightJPanel;
}
private JPanel getLoveRightJPanel() {
if (loveRightJPanel == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(2);
loveRightJPanel = new JPanel();
loveRightJPanel.setLayout(gridLayout);
loveRightJPanel.add(getLoveAnnouncement(), null);
loveRightJPanel.add(getLoveNameList(), null);
}
return loveRightJPanel;
}
private JPanel getEnglishRightJPanel() {
if (englishRightJPanel == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(2);
englishRightJPanel = new JPanel();
englishRightJPanel.setLayout(gridLayout);
englishRightJPanel.add(getEnglishAnnouncement(), null);
englishRightJPanel.add(getEnglishNameList(), null);
}
return englishRightJPanel;
}
/**
* 下面七个方法用于初始化消息框
*
* @return javax.swing.JTextPane
*/
private JTextPane getMainOutput() {
if (mainOutput == null) {
mainOutput = new JTextPane();
doc1 = mainOutput.getStyledDocument(); // 获得JTextPane的Document
mainOutput.setEditable(false);
}
return mainOutput;
}
private JTextPane getLiteratureOutput() {
if (literatureOutput == null) {
literatureOutput = new JTextPane();
literatureOutput.setBackground(Color.orange);
doc2 = literatureOutput.getStyledDocument(); // 获得JTextPane的Document
literatureOutput.setEditable(false);
}
return literatureOutput;
}
private JTextPane getMusicOutput() {
if (musicOutput == null) {
musicOutput = new JTextPane();
doc3 = musicOutput.getStyledDocument(); // 获得JTextPane的Document
musicOutput.setEditable(false);
}
return musicOutput;
}
private JTextPane getComputerOutput() {
if (computerOutput == null) {
computerOutput = new JTextPane();
doc4 = computerOutput.getStyledDocument(); // 获得JTextPane的Document
computerOutput.setEditable(false);
}
return computerOutput;
}
private JTextPane getSportOutput() {
if (sportOutput == null) {
sportOutput = new JTextPane();
doc5 = sportOutput.getStyledDocument(); // 获得JTextPane的Document
sportOutput.setEditable(false);
}
return sportOutput;
}
private JTextPane getLoveOutput() {
if (loveOutput == null) {
loveOutput = new JTextPane();
doc6 = loveOutput.getStyledDocument(); // 获得JTextPane的Document
loveOutput.setEditable(false);
}
return loveOutput;
}
private JTextPane getEnglishOutput() {
if (englishOutput == null) {
englishOutput = new JTextPane();
doc7 = englishOutput.getStyledDocument(); // 获得JTextPane的Document
englishOutput.setEditable(false);
}
return englishOutput;
}
/**
* 下面七个方法用于初始化公告栏
*
* @return javax.swing.JTextArea
*/
private JTextArea getMainAnnouncement() {
if (mainAnnouncement == null) {
mainAnnouncement = new JTextArea();
mainAnnouncement.append(" 公告\n文明聊天,不要刷频");
}
return mainAnnouncement;
}
private JTextArea getLiteratureAnnouncement() {
if (literatureAnnouncement == null) {
literatureAnnouncement = new JTextArea();
literatureAnnouncement.append(" 公告\n文明聊天,不要刷频");
}
return literatureAnnouncement;
}
private JTextArea getMusicAnnouncement() {
if (musicAnnouncement == null) {
musicAnnouncement = new JTextArea();
musicAnnouncement.append(" 公告\n文明聊天,不要刷频");
}
return musicAnnouncement;
}
private JTextArea getComputerAnnouncement() {
if (computerAnnouncement == null) {
computerAnnouncement = new JTextArea();
computerAnnouncement.append(" 公告\n文明聊天,不要刷频");
}
return computerAnnouncement;
}
private JTextArea getSportAnnouncement() {
if (sportAnnouncement == null) {
sportAnnouncement = new JTextArea();
sportAnnouncement.append(" 公告\n文明聊天,不要刷频");
}
return sportAnnouncement;
}
private JTextArea getLoveAnnouncement() {
if (loveAnnouncement == null) {
loveAnnouncement = new JTextArea();
loveAnnouncement.append(" 公告\n文明聊天,不要刷频");
}
return loveAnnouncement;
}
private JTextArea getEnglishAnnouncement() {
if (englishAnnouncement == null) {
englishAnnouncement = new JTextArea();
englishAnnouncement.append(" 公告\n文明聊天,不要刷频");
}
return englishAnnouncement;
}
/**
* 下面七个方法用于初始化七个用户列表(其实只用一个,因为没时间修改)
*
* @return javax.swing.JList
*/
private List getMainNameList() {
if (mainNameList == null) {
mainNameList = new List();
}
return mainNameList;
}
private List getLiteratureNameList() {
if (literatureNameList == null) {
literatureNameList = new List();
}
return literatureNameList;
}
private List getMusicNameList() {
if (musicNameList == null) {
musicNameList = new List();
}
return musicNameList;
}
private List getComputerNameList() {
if (computerNameList == null) {
computerNameList = new List();
}
return computerNameList;
}
private List getSportNameList() {
if (sportNameList == null) {
sportNameList = new List();
}
return sportNameList;
}
private List getLoveNameList() {
if (loveNameList == null) {
loveNameList = new List();
}
return loveNameList;
}
private List getEnglishNameList() {
if (englishNameList == null) {
englishNameList = new List();
}
return englishNameList;
}
/**
* 这是初始化选择进入随便聊聊频道的按钮
*
* @return javax.swing.JButton
*/
private JButton getMainButton() {
if (mainButton == null) {
mainButton = new JButton();
mainButton.setBounds(new Rectangle(0, 317, 86, 18));
mainButton.setText("\u968f\u4fbf\u804a\u804a");
mainButton.setEnabled(false);
mainButton.addActionListener(this);
}
return mainButton;
}
/**
* 这是初始化选择进入咬文嚼字频道的按钮
*
* @return javax.swing.JButton
*/
private JButton getLiteratureButton() {
if (literatureButton == null) {
literatureButton = new JButton();
literatureButton.setBounds(new Rectangle(85, 317, 86, 18));
literatureButton.setText("\u54ac\u6587\u56bc\u5b57");
literatureButton.setActionCommand("\u54ac\u6587\u56bc\u5b57");
literatureButton.addActionListener(this);
}
return literatureButton;
}
/**
* 这是初始化选择进入音乐频道的按钮
*
* @return javax.swing.JButton
*/
private JButton getMusicButton() {
if (musicButton == null) {
musicButton = new JButton();
musicButton.setBounds(new Rectangle(170, 317, 86, 18));
musicButton.setText("\u6211\u4e3a\u6b4c\u72c2");
musicButton.setPreferredSize(new Dimension(86, 28));
musicButton.addActionListener(this);
}
return musicButton;
}
/**
* 这是初始化选择进入计算机频道的按钮
*
* @return javax.swing.JButton
*/
private JButton getComputerButton() {
if (computerButton == null) {
computerButton = new JButton();
computerButton.setBounds(new Rectangle(255, 317, 86, 18));
computerButton.setText("\u7535\u8111\u6280\u672f");
computerButton.addActionListener(this);
}
return computerButton;
}
/**
* 这是初始化选择进入体育频道的按钮
*
* @return javax.swing.JButton
*/
private JButton getSportButton() {
if (sportButton == null) {
sportButton = new JButton();
sportButton.setBounds(new Rectangle(340, 317, 86, 18));
sportButton.setText("\u4f53\u80b2\u8054\u76df");
sportButton.addActionListener(this);
}
return sportButton;
}
/**
* 这是初始化选择进入谈情说爱频道的按钮
*
* @return javax.swing.JButton
*/
private JButton getLoveButton() {
if (loveButton == null) {
loveButton = new JButton();
loveButton.setBounds(new Rectangle(425, 317, 86, 18));
loveButton.setText("\u8c08\u60c5\u8bf4\u7231");
loveButton.addActionListener(this);
}
return loveButton;
}
/**
* 这是初始化选择进入英语角频道的按钮
*
* @return javax.swing.JButton
*/
private JButton getEnglishButton() {
if (englishButton == null) {
englishButton = new JButton();
englishButton.setBounds(new Rectangle(510, 317, 80, 18));
englishButton.setText("英语角");
englishButton.setActionCommand("\u54ac\u6587\u56bc\u5b57");
englishButton.addActionListener(this);
}
return englishButton;
}
/**
* 这个方法初始化"关于"按钮 用于提供本软件的一些有用信息与帮助文档
*
* @return javax.swing.JButton
*/
private JButton getAboutButton() {
if (aboutButton == null) {
aboutButton = new JButton();
aboutButton.setBounds(new Rectangle(588, 317, 61, 18));
aboutButton.setText("关于");
}
return aboutButton;
}
/**
* 这个方法用于初始化姓名输入框
*
* @return javax.swing.JTextField
*/
private JTextField getShowNameTextField() {
if (showNameTextField == null) {
showNameTextField = new JTextField();
showNameTextField.setBounds(new Rectangle(0, 335, 91, 22));
showNameTextField.setText("请取个名字");
showNameTextField.addMouseListener(this);
}
return showNameTextField;
}
/**
* 这个方法用于初始化连接按钮
*
* @return javax.swing.JButton
*/
private JButton getJoinButton() {
if (joinButton == null) {
joinButton = new JButton();
joinButton.setBounds(new Rectangle(81, 335, 72, 21));
joinButton.setText("连接");
joinButton.addActionListener(this);
}
return joinButton;
}
/**
* 这个方法用于初始化选择密聊对象的JComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getUserJComboBox() {
if (userJComboBox == null) {
userJComboBox = new JComboBox();
userJComboBox.setBounds(new Rectangle(151, 335, 73, 21));
userJComboBox.addItemListener(this);
}
return userJComboBox;
}
/**
* 此方法用于初始化选择密聊或公聊开关的按钮
*
* @return javax.swing.JButton
*/
private JButton getSecretButton() {
if (secretButton == null) {
secretButton = new JButton();
secretButton.setBounds(new Rectangle(223, 335, 91, 21));
secretButton.setText("私聊模式");
secretButton.addActionListener(this);
}
return secretButton;
}
/**
* 此方法用于初始化清屏按钮
*
* @return javax.swing.JComboBox
*/
private JButton getClearButton() {
if (clearButton == null) {
clearButton = new JButton();
clearButton.setBounds(new Rectangle(312, 335, 67, 21));
clearButton.setText("清屏");
clearButton.addActionListener(this);
}
return clearButton;
}
/**
* 此方法用于初始化fontSizeJComboBox(一个用于选择字体大小的JComboBox)
*
* @return javax.swing.JComboBox
*/
private JComboBox getFontSizeJComboBox() {
if (fontSizeJComboBox == null) {
fontSizeJComboBox = new JComboBox(str_Size);
fontSizeJComboBox.setBounds(new Rectangle(378, 335, 67, 21));
fontSizeJComboBox.addItemListener(this);
}
return fontSizeJComboBox;
}
/**
* 此方法用于初始化fontStyleJComboBox(一个用于选择字体风格的JComboBox)
*
* @return javax.swing.JComboBox
*/
private JComboBox getFontStyleJComboBox() {
if (fontStyleJComboBox == null) {
fontStyleJComboBox = new JComboBox(str_Style);
fontStyleJComboBox.setBounds(new Rectangle(444, 335, 67, 21));
fontStyleJComboBox.addItemListener(this);
}
return fontStyleJComboBox;
}
/**
* 此方法用于初始化fontColorJComboBox (一个用于选择字体颜色的JComboBox)
*
* @return javax.swing.JComboBox
*/
private JComboBox getFontColorJComboBox() {
if (fontColorJComboBox == null) {
fontColorJComboBox = new JComboBox(str_Color);
fontColorJComboBox.setBounds(new Rectangle(509, 335, 67, 21));
fontColorJComboBox.addItemListener(this);
}
return fontColorJComboBox;
}
/**
* 此方法用于初始化插图按钮
*
* @return javax.swing.JButton
*/
private JButton getPicButton() {
if (picButton == null) {
picButton = new JButton();
picButton.setBounds(new Rectangle(574, 335, 81, 21));
picButton.setVerticalTextPosition(SwingConstants.CENTER);
picButton.setText("插图");
picButton.addActionListener(this);
}
return picButton;
}
// 此方法用于初始化放置消息输入框的容器 是个JScrollPane控件
private JScrollPane getMessageJScrollPane() {
if (messageJScrollPane == null) {
messageJScrollPane = new JScrollPane();
messageJScrollPane.setBounds(new Rectangle(0, 356, 534, 52));
messageJScrollPane.setViewportView(getInput());
}
return messageJScrollPane;
}
/**
* 此方法用于初始化消息输入框
*
* @return javax.swing.JTextArea
*/
private JTextArea getInput() {
if (input == null) {
input = new JTextArea();
input.setBounds(new Rectangle(0, 356, 534, 52));
}
return input;
}
/**
* 此方法用于初始化发送按钮
*
* @return javax.swing.JButton
*/
private JButton getSendButton() {
if (sendButton == null) {
sendButton = new JButton();
sendButton.setBounds(new Rectangle(533, 356, 60, 51));
sendButton.setText("发送");
sendButton.addActionListener(this);
}
return sendButton;
}
/**
* 此方法用于初始化保存按钮
*
* @return javax.swing.JButton
*/
private JButton getSaveButton() {
if (saveButton == null) {
saveButton = new JButton();
saveButton.setBounds(new Rectangle(593, 356, 60, 51));
saveButton.setText("保存");
saveButton.addActionListener(this);
}
return saveButton;
}
// 事件处理
// 事件处理
public synchronized void actionPerformed(java.awt.event.ActionEvent e) {
DatagramPacket data = null;
if(e.getSource() == sendButton)
{
try {
if(login == 0 )
{
new JOptionPane().showMessageDialog(null, "未连接网络");
}
else
if(input.getText().trim().equals(""))
new JOptionPane().showMessageDialog(null, "请不要发空消息");
else
{
if(userJComboBox.getSelectedItem().equals("所有人"))
sendName = "";
if(mainUpjPanel.isVisible())
{
String s = String.valueOf(localSize);
msg=(s+localStyle+att.getColorNum()+showNameTextField.getText()+
secretFlag + sendName + "说: "+input.getText());
data=
new DatagramPacket(msg.getBytes(),msg.getBytes().length,group, 6001);
s1.send(data);
input.setText("");
}
else
if(literatureUpjPanel.isVisible())
{
String s = String.valueOf(localSize);
msg=(s+localStyle+att.getColorNum()+showNameTextField.getText()+
secretFlag + sendName + "说: "+input.getText());
data=
new DatagramPacket(msg.getBytes(),msg.getBytes().length,group, 6002);
s2.send(data);
input.setText("");
}
else
if(musicUpjPanel.isVisible())
{
String s = String.valueOf(localSize);
msg=(s+localStyle+att.getColorNum()+showNameTextField.getText()+
secretFlag + sendName + "说: "+input.getText());
data=
new DatagramPacket(msg.getBytes(),msg.getBytes().length,group, 6003);
s3.send(data);
input.setText("");
}
else
if(computerUpjPanel.isVisible())
{
String s = String.valueOf(localSize);
msg=(s+localStyle+att.getColorNum()+showNameTextField.getText()+
secretFlag + sendName + "说: "+input.getText());
data=
new DatagramPacket(msg.getBytes(),msg.getBytes().length,group, 6004);
s4.send(data);
input.setText("");
}
else
if(sportUpjPanel.isVisible())
{
String s = String.valueOf(localSize);
msg=(s+localStyle+att.getColorNum()+showNameTextField.getText()+
secretFlag + sendName + "说: "+input.getText());
data=
new DatagramPacket(msg.getBytes(),msg.getBytes().length,group, 6005);
s5.send(data);
input.setText("");
}
else
if(loveUpjPanel.isVisible())
{
String s = String.valueOf(localSize);
msg=(s+localStyle+att.getColorNum()+showNameTextField.getText()+
secretFlag + sendName + "说: "+input.getText());
data=
new DatagramPacket(msg.getBytes(),msg.getBytes().length,group, 6006);
s6.send(data);
input.setText("");
}
else
if(englishUpjPanel.isVisible())
{
String s = String.valueOf(localSize);
msg=(s+localStyle+att.getColorNum()+showNameTextField.getText()+
secretFlag + sendName + ": "+input.getText());
data=
new DatagramPacket(msg.getBytes(),msg.getBytes().length,group, 6007);
s7.send(data);
input.setText("");
}
}
}catch (IOException e1) {
System.out.println("发送失败");
}
input.setText("");
}
if(e.getSource() == mainButton)
{
literatureUpjPanel.setVisible(false);musicUpjPanel.setVisible(false);
computerUpjPanel.setVisible(false);sportUpjPanel.setVisible(false);
loveUpjPanel.setVisible(false);englishUpjPanel.setVisible(false);
mainUpjPanel.setVisible(true);
mainButton.setEnabled(false);
literatureButton.setEnabled(true);musicButton.setEnabled(true);
computerButton.setEnabled(true);sportButton.setEnabled(true);
loveButton.setEnabled(true);englishButton.setEnabled(true);
}
if(e.getSource() == literatureButton)
{
mainUpjPanel.setVisible(false);musicUpjPanel.setVisible(false);
computerUpjPanel.setVisible(false);sportUpjPanel.setVisible(false);
loveUpjPanel.setVisible(false);englishUpjPanel.setVisible(false);
literatureUpjPanel.setVisible(true);
literatureButton.setEnabled(false);
mainButton.setEnabled(true);musicButton.setEnabled(true);
computerButton.setEnabled(true);sportButton.setEnabled(true);
loveButton.setEnabled(true);englishButton.setEnabled(true);
}
if(e.getSource() == musicButton)
{
literatureUpjPanel.setVisible(false);mainUpjPanel.setVisible(false);
computerUpjPanel.setVisible(false);sportUpjPanel.setVisible(false);
loveUpjPanel.setVisible(false);englishUpjPanel.setVisible(false);
musicUpjPanel.setVisible(true);
musicButton.setEnabled(false);
literatureButton.setEnabled(true);mainButton.setEnabled(true);computerButton.setEnabled(true);
sportButton.setEnabled(true);loveButton.setEnabled(true);englishButton.setEnabled(true);
}
if(e.getSource() == computerButton)
{
literatureUpjPanel.setVisible(false);musicUpjPanel.setVisible(false);
mainUpjPanel.setVisible(false);sportUpjPanel.setVisible(false);
loveUpjPanel.setVisible(false);englishUpjPanel.setVisible(false);
computerUpjPanel.setVisible(true);
computerButton.setEnabled(false);
literatureButton.setEnabled(true);musicButton.setEnabled(true);mainButton.setEnabled(true);
sportButton.setEnabled(true);loveButton.setEnabled(true);englishButton.setEnabled(true);
}
if(e.getSource() == sportButton)
{
literatureUpjPanel.setVisible(false);musicUpjPanel.setVisible(false);
computerUpjPanel.setVisible(false);mainUpjPanel.setVisible(false);
loveUpjPanel.setVisible(false);englishUpjPanel.setVisible(false);
sportUpjPanel.setVisible(true);
sportButton.setEnabled(false);
literatureButton.setEnabled(true);musicButton.setEnabled(true);computerButton.setEnabled(true);
mainButton.setEnabled(true);loveButton.setEnabled(true);englishButton.setEnabled(true);
}
if(e.getSource() == loveButton)
{
literatureUpjPanel.setVisible(false);musicUpjPanel.setVisible(false);
computerUpjPanel.setVisible(false);sportUpjPanel.setVisible(false);
mainUpjPanel.setVisible(false);englishUpjPanel.setVisible(false);
loveUpjPanel.setVisible(true);
loveButton.setEnabled(false);
literatureButton.setEnabled(true);musicButton.setEnabled(true);
computerButton.setEnabled(true);sportButton.setEnabled(true);
mainButton.setEnabled(true);englishButton.setEnabled(true);
}
if(e.getSource() == englishButton)
{
literatureUpjPanel.setVisible(false);musicUpjPanel.setVisible(false);
computerUpjPanel.setVisible(false);sportUpjPanel.setVisible(false);
mainUpjPanel.setVisible(false);loveUpjPanel.setVisible(false);
englishUpjPanel.setVisible(true);
englishButton.setEnabled(false);
literatureButton.setEnabled(true);musicButton.setEnabled(true);
computerButton.setEnabled(true);sportButton.setEnabled(true);
mainButton.setEnabled(true);loveButton.setEnabled(true);
}
if(e.getSource() == secretButton)
{
if(userJComboBox.getSelectedItem().equals("所有人"))
{
new JOptionPane().showMessageDialog(null, "对所有人密聊无意义");
}
else
{
secretFlag = "#SeCrEt#悄悄";
secretButton.setEnabled(false);
}
}
if(e.getSource() == clearButton)
{
if(mainUpjPanel.isVisible())
mainOutput.setText("");
if(literatureUpjPanel.isVisible())
literatureOutput.setText("");
if(musicUpjPanel.isVisible())
musicOutput.setText("");
if(computerUpjPanel.isVisible())
computerOutput.setText("");
if(sportUpjPanel.isVisible())
sportOutput.setText("");
if(loveUpjPanel.isVisible())
loveOutput.setText("");
if(englishUpjPanel.isVisible())
englishOutput.setText("");
}
if(e.getSource() == saveButton)
{
String saveString = null;
try
{
if(mainUpjPanel.isVisible())
{
saveString = mainOutput.getText();
FileOutputStream Note=new FileOutputStream("main.txt");
new JOptionPane().showMessageDialog(null, "保存成功");
Note.write(saveString.getBytes());
Note.close();
}
if(literatureUpjPanel.isVisible())
{
saveString = literatureOutput.getText();
FileOutputStream Note=new FileOutputStream("literature.txt");
new JOptionPane().showMessageDialog(null, "保存成功");
Note.write(saveString.getBytes());
Note.close();
}
if(musicUpjPanel.isVisible())
{
saveString = musicOutput.getText();
FileOutputStream Note=new FileOutputStream("music.txt");
new JOptionPane().showMessageDialog(null, "保存成功");
Note.write(saveString.getBytes());
Note.close();
}
if(computerUpjPanel.isVisible())
{
saveString = computerOutput.getText();
FileOutputStream Note=new FileOutputStream("computer.txt");
new JOptionPane().showMessageDialog(null, "保存成功");
Note.write(saveString.getBytes());
Note.close();
}
if(sportUpjPanel.isVisible())
{
saveString = sportOutput.getText();
FileOutputStream Note=new FileOutputStream("sport.txt");
new JOptionPane().showMessageDialog(null, "保存成功");
Note.write(saveString.getBytes());
Note.close();
}
if(loveUpjPanel.isVisible())
{
saveString = loveOutput.getText();
FileOutputStream Note=new FileOutputStream("love.txt");
new JOptionPane().showMessageDialog(null, "保存成功");
Note.write(saveString.getBytes());
Note.close();
}
if(englishUpjPanel.isVisible())
{
saveString = englishOutput.getText();
FileOutputStream Note=new FileOutputStream("english.txt");
new JOptionPane().showMessageDialog(null, "保存成功");
Note.write(saveString.getBytes());
Note.close();
}
}catch(IOException ioe) {
System.out.println("保存失败");
}
}
if(e.getSource() == aboutButton)
{
new JOptionPane().showMessageDialog(null, "名称长度有问题,正常输入应该是0-11个字符");
}
if(e.getSource() == joinButton)
{
if(showNameTextField.getText().equals("") || showNameTextField.getText().length() > 11) {
new JOptionPane().showMessageDialog(null, "名称长度有问题,正常输入应该是0-11个字符");
}
else
if(showNameTextField.getText().equals("所有人") || showNameTextField.getText().equals("请取个名字")) {
new JOptionPane().showMessageDialog(null, "这种名字可能引起误会,请更换");
}
else
if(showNameTextField.getText().substring(0,1).equals(" ")) {
new JOptionPane().showMessageDialog(null, "第一个字符不能是空格,否则程序会出错");
}
else
{
login = 1;
nameFlag = 1;
joinButton.setEnabled(false);
showNameTextField.setEnabled(false);
localName = showNameTextField.getText();
userJComboBox.addItem("所有人");
mainNameList.add(localName);literatureNameList.add(localName);musicNameList.add(localName);
computerNameList.add(localName);sportNameList.add(localName);loveNameList.add(localName);
englishNameList.add(localName);
String msgName = null;
try {
initNet();
}catch(IOException ioe) {
}
msgName = ("1211" + " " + localName+ "上线了\n");
try
{
data = new DatagramPacket(msgName.getBytes(),msgName.getBytes().length,group, 6001);
s1.send(data);
data = new DatagramPacket(msgName.getBytes(),msgName.getBytes().length,group, 6002);
s2.send(data);
data = new DatagramPacket(msgName.getBytes(),msgName.getBytes().length,group, 6003);
s3.send(data);
data = new DatagramPacket(msgName.getBytes(),msgName.getBytes().length,group, 6004);
s4.send(data);
data = new DatagramPacket(msgName.getBytes(),msgName.getBytes().length,group, 6005);
s5.send(data);
data = new DatagramPacket(msgName.getBytes(),msgName.getBytes().length,group, 6006);
s6.send(data);
data = new DatagramPacket(msgName.getBytes(),msgName.getBytes().length,group, 6007);
s7.send(data);
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
if(e.getSource() == picButton)
{
byte[] utf = null;
JFileChooser f = new JFileChooser("c:\\pic"); // 查找文件
File fileName = null;
String fName = null;
f.showOpenDialog(null);
fileName = f.getSelectedFile();
fName = "1211P" + localName + fileName.getName();
utf = fName.getBytes();
try
{
if(mainUpjPanel.isVisible())
{
data = new DatagramPacket(new byte[1], 1,group, 6001);
data.setData(utf);
s1.send(data);
}
else
if(literatureUpjPanel.isVisible())
{
data = new DatagramPacket(new byte[1], 1,group, 6002);
data.setData(utf);
s2.send(data);
}
else
if(musicUpjPanel.isVisible())
{
data = new DatagramPacket(new byte[1], 1,group, 6003);
data.setData(utf);
s3.send(data);
}
else
if(computerUpjPanel.isVisible())
{
data = new DatagramPacket(new byte[1], 1,group, 6004);
data.setData(utf);
s4.send(data);
}
else
if(sportUpjPanel.isVisible())
{
data = new DatagramPacket(new byte[1], 1,group, 6005);
data.setData(utf);
s5.send(data);
}
else
if(loveUpjPanel.isVisible())
{
data = new DatagramPacket(new byte[1], 1,group, 6006);
data.setData(utf);
s6.send(data);
}
else
if(englishUpjPanel.isVisible())
{
data = new DatagramPacket(new byte[1], 1,group, 6007);
data.setData(utf);
s7.send(data);
}
} catch(IOException ioe) {
System.out.println("IOException ERROR");
}
}
}
public void windowOpened(WindowEvent event) {}
public void windowClosing(WindowEvent event) {
if(login == 1) {
String msgName ="1211" + " " + localName + "下线了";
try {
DatagramPacket data = new DatagramPacket(msgName.getBytes(),msgName.getBytes().length,group, 6001);
s1.send(data);
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
System.exit(0);
}
public void windowClosed(WindowEvent event) {}
public void windowIconified(WindowEvent event) {}
public void windowDeiconified(WindowEvent event) {}
public void windowActivated(WindowEvent event) {}
public void windowDeactivated(WindowEvent event) {}
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
if(e.getSource() == fontColorJComboBox) {
if(e.getItem().equals(fontColorJComboBox.getItemAt(0)))
att.setColorNum(0);
if(e.getItem().equals(fontColorJComboBox.getItemAt(1)))
att.setColorNum(1);
if(e.getItem().equals(fontColorJComboBox.getItemAt(2)))
att.setColorNum(2);
if(e.getItem().equals(fontColorJComboBox.getItemAt(3)))
att.setColorNum(3);
if(e.getItem().equals(fontColorJComboBox.getItemAt(4)))
att.setColorNum(4);
}
if(e.getSource() == fontSizeJComboBox) {
if(e.getItem().equals(fontSizeJComboBox.getItemAt(0)))
localSize = 12;
if(e.getItem().equals(fontSizeJComboBox.getItemAt(1)))
localSize = 14;
if(e.getItem().equals(fontSizeJComboBox.getItemAt(2)))
localSize = 18;
if(e.getItem().equals(fontSizeJComboBox.getItemAt(3)))
localSize = 22;
if(e.getItem().equals(fontSizeJComboBox.getItemAt(4)))
localSize = 30;
if(e.getItem().equals(fontSizeJComboBox.getItemAt(5)))
localSize = 40;
}
if(e.getSource() == fontStyleJComboBox) {
if(e.getItem().equals(fontStyleJComboBox.getItemAt(0)))
localStyle = 0;
if(e.getItem().equals(fontStyleJComboBox.getItemAt(1)))
localStyle = 1;
if(e.getItem().equals(fontStyleJComboBox.getItemAt(2)))
localStyle = 2;
if(e.getItem().equals(fontStyleJComboBox.getItemAt(3)))
localStyle= 3;
}
if(e.getSource() == userJComboBox) {
if(userJComboBox.getSelectedItem().equals("所有人"))
{
secretFlag = "";
secretButton.setEnabled(true);
}
sendName = "对#P" + userJComboBox.getSelectedItem() + "@P";
}
}
}
public void mouseClicked(MouseEvent e) {
if(e.getSource() == showNameTextField) {
if(showNameTextField.getText().equals("请取个名字"))
showNameTextField.setText("");
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
/**
* 插入图片
*
* @param icon
*/
private void insertIcon(File file,JTextPane text,StyledDocument doc) {
text.setCaretPosition(doc.getLength()); // 设置插入位置
text.insertIcon(new ImageIcon(file.getPath())); // 插入图片
insert(doc,new FontAttrib()); // 这样做可以换行
}
/**
* 将文本插入JTextPane
*
* @param attrib
*/
private void insert(StyledDocument doc , FontAttrib attrib) {
try { // 插入文本
doc.insertString(doc.getLength(), attrib.getText()+'\n' , attrib.getAttrSet());
}catch (BadLocationException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new HelloChater();
}
}
|
|||||
|




看来要抓紧啊