📄 list.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * list.java * * Created on 2009-1-20, 22:50:16 */package list;import broadcast.BroadCastWord;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.StringTokenizer;import javax.swing.DefaultListModel;import javax.swing.JList;import lanchat.GUIchat;import lanchat.Mserver;/** * * @author Administrator */public class list extends javax.swing.JFrame { DefaultListModel listModel = null; private about ab = null; private String lg = null; private String str = login.str; public static String ip =null; /** Creates new form list */ public list() { initComponents(); //JScrollPane scrollPane = new JScrollPane(this); listModel= new DefaultListModel(); this.jList1.setModel(listModel); this.setTitle("局域网聊天系统"); this.setLocation(400, 200); this.setVisible(true); lg = "logou " + str; // 设置登出信息 //设置鼠标 双击 事件 MouseListener mouseListener = new MouseAdapter() { @Override public void mouseClicked(MouseEvent mouseEvent) { JList theList = (JList) mouseEvent.getSource(); if (mouseEvent.getClickCount() == 2) { int index = theList.locationToIndex(mouseEvent.getPoint()); if (index >= 0) { Object o = theList.getModel().getElementAt(index); //System.out.println("Double-clicked on: " + o.toString()); str = o.toString(); ip = str; Mserver.m = index;//设置 静态变量m newGUI(str,index);//打开 Mserver.guichat[Mserver.m]聊天界面 System.out.println(str); } } } }; jList1.addMouseListener(mouseListener);//添加事件 } public void newGUI(String str,int index){ if(Mserver.guichat[Mserver.m] ==null) Mserver.guichat[index] = new GUIchat(str); Mserver.guichat[index].setVisible(true); //提取IP 字段 StringTokenizer st=new StringTokenizer(str," "); int m = st.countTokens(); for(int i = 0; i <m ; i++){ str = st.nextToken(); } Mserver.guichat[Mserver.m].setip(str) ;//设置 Mserver.guichat[Mserver.m] 的ip } //对比 源IP地址。以原定对应的聊天对话框 public void comitem(String str){ //从JList中获得这个集合,转换为默认项集合类型 listModel= (DefaultListModel) this.jList1.getModel(); Object[] anArray = new Object[255]; int m = -1; listModel.copyInto(anArray); for(int i=0;i<anArray.length;i++){ if(anArray[i] != null){ StringTokenizer st=new StringTokenizer((String)anArray[i]," "); int t = st.countTokens(); String temp = null; for(int j=0; j<t; j++) temp = st.nextToken(); if(str.compareTo(temp) == 0){ m=i; break; } } } Mserver.m = m;//确定m,就确定了对话框 } public void additem(String str){ //从JList中获得这个集合,转换为默认项集合类型 listModel= (DefaultListModel) this.jList1.getModel(); //追加元素 listModel.add(listModel.getSize(),str); //将这个集合添加到JList中 this.jList1.setModel(listModel); } public void removeitem(String str){ //从JList中获得这个集合,转换为默认项集合类型 listModel= (DefaultListModel) this.jList1.getModel(); //删除所选元素在默认项集合中 Object[] anArray = new Object[255]; int m = -1; listModel.copyInto(anArray); if (anArray != null){ for(int i=0;i<anArray.length;i++){ if(str.compareTo((String) anArray[i]) == 0){ m=i; break; } } } if (m>=0) listModel.remove(m); //重新添加到JList中 this.jList1.setModel(listModel); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); exit = new javax.swing.JButton(); about = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { closing(evt); } }); jPanel1.setBackground(new java.awt.Color(0, 204, 255)); jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); exit.setBackground(new java.awt.Color(51, 204, 255)); exit.setText("退出"); exit.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); exit.setName("exit"); // NOI18N exit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitActionPerformed(evt); } }); about.setBackground(new java.awt.Color(51, 204, 255)); about.setText("关于"); about.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); about.setName("about"); // NOI18N about.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { aboutActionPerformed(evt); } }); jList1.setForeground(new java.awt.Color(255, 0, 102)); jScrollPane1.setViewportView(jList1); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(about, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(about, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE) .addComponent(exit, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold>//GEN-END:initComponents private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed // TODO add your handling code here: //关于按钮 if (ab == null) //防止new两个 关于界面 ab = new about(); ab.setVisible(true); }//GEN-LAST:event_aboutActionPerformed private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed // TODO add your handling code here: //退出 按钮 new BroadCastWord(lg);//登出前,设置登出信息 System.exit(0); }//GEN-LAST:event_exitActionPerformed private void closing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closing // TODO add your handling code here: //Jframe 的窗口关闭按钮 new BroadCastWord(lg); System.exit(0); }//GEN-LAST:event_closing // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton about; private javax.swing.JButton exit; public javax.swing.JList jList1; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -