|
|
|
@ -0,0 +1,186 @@
|
|
|
|
|
package chatpanel;
|
|
|
|
|
|
|
|
|
|
import java.awt.BorderLayout;
|
|
|
|
|
import java.awt.Color;
|
|
|
|
|
import java.awt.Dimension;
|
|
|
|
|
import java.awt.FlowLayout;
|
|
|
|
|
import java.awt.GridLayout;
|
|
|
|
|
import java.awt.Toolkit;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.util.Vector;
|
|
|
|
|
|
|
|
|
|
import javax.swing.JButton;
|
|
|
|
|
import javax.swing.JFrame;
|
|
|
|
|
import javax.swing.JLabel;
|
|
|
|
|
import javax.swing.JList;
|
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
|
import javax.swing.JScrollPane;
|
|
|
|
|
import javax.swing.JTextArea;
|
|
|
|
|
import javax.swing.JTextField;
|
|
|
|
|
import javax.swing.border.Border;
|
|
|
|
|
import javax.swing.border.EtchedBorder;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ChatPanel extends JFrame
|
|
|
|
|
{
|
|
|
|
|
JButton send,connect,disconnect;
|
|
|
|
|
JList list;
|
|
|
|
|
JLabel userlabel,textlabel;
|
|
|
|
|
JTextField textfield;
|
|
|
|
|
JTextArea textarea;
|
|
|
|
|
JPanel uppanel,downpanel,userpanel,listtitle,texttitle,textpanel,sendpanel,serverpanel;
|
|
|
|
|
JScrollPane listpanel,areapanel;
|
|
|
|
|
|
|
|
|
|
Border border = new EtchedBorder(Color.BLUE, Color.yellow); //各面板组件镶的边的类型。
|
|
|
|
|
|
|
|
|
|
public ChatPanel()
|
|
|
|
|
{
|
|
|
|
|
//创建用户列表标题
|
|
|
|
|
userlabel = new JLabel("用户列表",userlabel.CENTER);
|
|
|
|
|
listtitle = new JPanel();
|
|
|
|
|
listtitle.setBackground(Color.LIGHT_GRAY);
|
|
|
|
|
listtitle.add(userlabel);
|
|
|
|
|
|
|
|
|
|
//创建用户列表
|
|
|
|
|
list = new JList();
|
|
|
|
|
//list.setBackground(Color.LIGHT_GRAY);
|
|
|
|
|
Vector<String> vUserName = new Vector<String>();//向量列表,< >向量列表里面对应的对象的类型
|
|
|
|
|
for (int i = 1; i < 30; i++)
|
|
|
|
|
{
|
|
|
|
|
vUserName.add("用户"+i);
|
|
|
|
|
}
|
|
|
|
|
list.setListData(vUserName); // 往这个列表对象中放入要显示的数据。
|
|
|
|
|
list.setFixedCellWidth(80); //设置默认宽度
|
|
|
|
|
|
|
|
|
|
//将用户列表加入用户列表面板
|
|
|
|
|
listpanel = new JScrollPane(list);
|
|
|
|
|
|
|
|
|
|
listpanel.setPreferredSize(new Dimension(120,0));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//将列表标题和用户列表加入用户列表界面
|
|
|
|
|
userpanel = new JPanel();
|
|
|
|
|
userpanel.setLayout(new BorderLayout());
|
|
|
|
|
userpanel.add(listtitle,BorderLayout.NORTH);
|
|
|
|
|
userpanel.add(listpanel,BorderLayout.CENTER);
|
|
|
|
|
userpanel.setBorder(border);
|
|
|
|
|
|
|
|
|
|
//创建显示信息界面
|
|
|
|
|
textlabel = new JLabel("用户聊天界面");
|
|
|
|
|
texttitle = new JPanel();
|
|
|
|
|
texttitle.setBackground(Color.LIGHT_GRAY);
|
|
|
|
|
texttitle.add(textlabel);
|
|
|
|
|
|
|
|
|
|
//聊天信息面板
|
|
|
|
|
textarea = new JTextArea("来自用户1的消息:hello!");
|
|
|
|
|
textarea.setEditable(false);
|
|
|
|
|
textarea.setLineWrap(true); //设置换行策略
|
|
|
|
|
areapanel = new JScrollPane(textarea);
|
|
|
|
|
|
|
|
|
|
textpanel = new JPanel();
|
|
|
|
|
textpanel.setLayout(new BorderLayout());
|
|
|
|
|
textpanel.add(texttitle,BorderLayout.NORTH);
|
|
|
|
|
textpanel.add(areapanel,BorderLayout.CENTER);
|
|
|
|
|
textpanel.setBorder(border);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//将用户列表界面和显示信息窗口加入uppanel面板
|
|
|
|
|
uppanel = new JPanel();
|
|
|
|
|
uppanel.setLayout(new BorderLayout());
|
|
|
|
|
uppanel.add(userpanel,BorderLayout.WEST);
|
|
|
|
|
uppanel.add(textpanel,BorderLayout.CENTER);
|
|
|
|
|
//uppanel.setBorder(border);
|
|
|
|
|
|
|
|
|
|
//创建发送信息面板sendpanel
|
|
|
|
|
send = new JButton("发送");
|
|
|
|
|
textfield = new JTextField("请输入信息......",30);
|
|
|
|
|
sendpanel = new JPanel();
|
|
|
|
|
sendpanel.setLayout(new BorderLayout(0,10));
|
|
|
|
|
sendpanel.add(textfield,BorderLayout.CENTER);
|
|
|
|
|
sendpanel.add(send,BorderLayout.EAST);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建服务器面板serverpanel
|
|
|
|
|
connect = new JButton("连接服务器");
|
|
|
|
|
disconnect = new JButton("断开服务器");
|
|
|
|
|
serverpanel = new JPanel();
|
|
|
|
|
serverpanel.setBackground(Color.LIGHT_GRAY);
|
|
|
|
|
serverpanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,10));
|
|
|
|
|
serverpanel.add(connect);
|
|
|
|
|
serverpanel.add(disconnect);
|
|
|
|
|
|
|
|
|
|
//将发送信息面板和服务器面板加入downpanel面板
|
|
|
|
|
downpanel = new JPanel();
|
|
|
|
|
downpanel.setLayout(new GridLayout(2,1));
|
|
|
|
|
downpanel.add(sendpanel);
|
|
|
|
|
downpanel.add(serverpanel);
|
|
|
|
|
downpanel.setBorder(border);
|
|
|
|
|
|
|
|
|
|
registerListener(); //注册监听器
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args)
|
|
|
|
|
{
|
|
|
|
|
ChatPanel chatpanel = new ChatPanel();
|
|
|
|
|
chatpanel.lunchframe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void lunchframe()
|
|
|
|
|
{
|
|
|
|
|
JFrame jframe = new JFrame("聊天室界面");
|
|
|
|
|
jframe.setSize(600, 600);
|
|
|
|
|
jframe.setResizable(false);
|
|
|
|
|
jframe.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-jframe.getWidth())/2,
|
|
|
|
|
(Toolkit.getDefaultToolkit().getScreenSize().height-jframe.getHeight())/2);
|
|
|
|
|
|
|
|
|
|
jframe.setLayout(new BorderLayout());
|
|
|
|
|
jframe.add(uppanel,BorderLayout.CENTER);
|
|
|
|
|
jframe.add(downpanel,BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
|
|
jframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
|
|
|
jframe.setVisible(true);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void registerListener()
|
|
|
|
|
{
|
|
|
|
|
send.addActionListener(new Controller()) ;
|
|
|
|
|
connect.addActionListener(new Controller()) ;
|
|
|
|
|
disconnect.addActionListener(new Controller()) ;
|
|
|
|
|
textfield.addActionListener(new Controller()) ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Controller implements ActionListener
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public void actionPerformed(ActionEvent e)
|
|
|
|
|
{
|
|
|
|
|
if((e.getSource())instanceof JButton)
|
|
|
|
|
{
|
|
|
|
|
JButton button = (JButton)e.getSource();
|
|
|
|
|
if(button == send)
|
|
|
|
|
{
|
|
|
|
|
System.out.println("发送消息");
|
|
|
|
|
}
|
|
|
|
|
else if(button == connect)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
System.out.println("连接服务器");
|
|
|
|
|
}
|
|
|
|
|
else if(button == disconnect)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
System.out.println("断开服务器");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//信息编辑框发出的事件处理
|
|
|
|
|
System.out.println("未知事件处理");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|