📄 listuser.java
字号:
package dssclient;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
public class ListUser extends JFrame implements ActionListener{
DefaultListModel theNLM = new DefaultListModel();
JList talkerlist;
Socket sk;
//Vector talkername=new Vector();
Vector winopenname=new Vector();
Vector winopen=new Vector();
String host,sender1;
ClientListen cl;
JButton quitbutton = new JButton("退出");
FileReceiver fr;
FileSender fs;
void buildConstraints(GridBagConstraints gbc,int gx,int gy,int gw,int gh,int wx,int wy){
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
public ListUser(String name, Socket soc) throws IOException{
super(name+"使用数字签名系统");//
sk = soc;
initFrame(name); //
//打开监听线程
cl = new ClientListen(this,sk);
cl.start();
sendmsg("ASKFOR:");
}
private void initFrame(String name) throws IOException {
setSize(300,300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridbag = new GridBagLayout();
JPanel talkerpanel = new JPanel(gridbag);
GridBagConstraints constraints = new GridBagConstraints();
host=name;
buildConstraints(constraints,0,0,1,1,100,5);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.CENTER;
talkerlist = new JList(theNLM);
talkerlist.setCellRenderer(new MyCellRenderer());
JScrollPane jsp = new JScrollPane(talkerlist);
talkerlist.setVisibleRowCount(8);
talkerlist.setBackground(Color.white);
talkerlist.setForeground(Color.black);
talkerlist.setFont(new Font("Arial", 1, 14));
talkerlist.setBackground(Color.white);
talkerlist.setForeground(Color.black);
talkerlist.setFont(new Font("Arial", 1, 14));
Color aqua = new Color(0, 102, 102);
talkerlist.setSelectionBackground(aqua);
talkerlist.setSelectionForeground(Color.white);
talkerlist.setFixedCellWidth(100);
talkerlist.setFixedCellHeight(15);
talkerlist.setFixedCellWidth(100);
talkerlist.setFixedCellHeight(15);
buildConstraints(constraints,0,3,1,1,100,90);
constraints.fill = GridBagConstraints.BOTH;
gridbag.setConstraints(jsp,constraints);
talkerpanel.add(jsp, new GridBagConstraints());
buildConstraints(constraints,0,3,1,1,50,5);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(quitbutton,constraints);
final GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.WEST;
talkerpanel.add(quitbutton, gridBagConstraints);
quitbutton.addActionListener(this);
setContentPane(talkerpanel);
MouseListener mouseListener = new MouseAdapter() {//监听鼠标动作
public void mouseClicked(MouseEvent e) {
ListUser nm = cl.tkparent;
if(e.getClickCount() == 2) {
try{
int index = talkerlist.locationToIndex(e.getPoint());//鼠标点击
openmsgframe((String)talkerlist.getSelectedValue());
}catch(NullPointerException e2){}
}
}
};
talkerlist.addMouseListener(mouseListener);
}
//各种方法
void disconnect(){ //退出
sendmsg("QUIT:");
}
void adduser(String name){ //增加用户
theNLM.addElement(name);
try{
// talkername.addElement(name);
}
catch(NullPointerException e){
}
}
void removeuser(String name){ //移除用户
if(name=="SERVERSHUTDOWN"){
theNLM.removeAllElements();
//talkername.removeAllElements();
}
else{
theNLM.removeElement(name);
//talkername.remove(name);
}
}
void batchandle(String msg){
StringTokenizer st=new StringTokenizer(msg,":");
String receiver=st.nextToken();
String sender=st.nextToken();
sender1=sender;
System.out.println(sender);
String show=sender+": "+st.nextToken()+"\n";
}
void openmsgframe(String sender){ //监听到向随发送信息
if(!winopenname.contains(sender)){
sendmsg("FILE_TO:"+sender+":"+host+":"); //打开发送界面
try{
// winopen.addElement(mf);
winopenname.addElement(sender);
}catch(NullPointerException e){
}
}
}
void sendmsg(String info){
cl.send(info);
}
public void sendbatch(String string) {
cl.send(string);
}
void FileTobox(String acquir){
int response;
StringTokenizer st=new StringTokenizer(acquir,":");
System.out.println(st);
String me = st.nextToken();
String he = st.nextToken();
String ip = st.nextToken();
String local = new String();
try {
local = InetAddress.getLocalHost().toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
StringTokenizer lc=new StringTokenizer(local,"/");
String localname=lc.nextToken();
String localip=lc.nextToken();
//response = JOptionPane.showConfirmDialog(null,
// he+"将要传送文件给你,是否接受?",
// "",
// JOptionPane.YES_NO_OPTION,
// JOptionPane.QUESTION_MESSAGE);
//if(response==JOptionPane.YES_OPTION){
sendmsg("FILE_FROM:"+he+":"+localname+":"+"YES:"+localip+":");
fr = new FileReceiver();
fr.show();
fr.start();
// }
// else if(response==JOptionPane.NO_OPTION){
// sendmsg("FILE_FROM:"+he+":"+localname+":"+"NO:");
//}
}
void FileFrombox(String answer){
StringTokenizer st=new StringTokenizer(answer,":");
System.out.println(st);
String me = st.nextToken();
String he = st.nextToken();
String chioce=st.nextToken();
String ip = st.nextToken();
//if(chioce.equals("YES")){
//JOptionPane.showMessageDialog(null,he+"同意接收文件.");
fs.theApp.ip=ip;
//}
//else if(chioce.equals("NO")){
//JOptionPane.showMessageDialog(null,he+"不同意接收文件.");
//}
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="退出"){
disconnect();
//ClientLogin ca=new ClientLogin();
//ca.show();
this.dispose();
}
}
class MyCellRenderer extends JLabel implements ListCellRenderer {
public Component getListCellRendererComponent(
JList list,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{ final ImageIcon icon = new ImageIcon("images/face.gif");
String s = value.toString();
setText(s);
setIcon(icon);
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -