📄 frame2.java~2~
字号:
package chatclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.io.*;import java.net.*;//import java.util.*;//import java.lang.Thread;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class Frame2 extends JFrame { JPanel contentPane; XYLayout xYLayout1 = new XYLayout(); JPanel jPanel1 = new JPanel(); XYLayout xYLayout2 = new XYLayout(); JPanel jPanel2 = new JPanel(); XYLayout xYLayout3 = new XYLayout(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JTextField jTextField1 = new JTextField(); JTextField jTextField2 = new JTextField(); JScrollPane jScrollPane1 = new JScrollPane(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JRadioButton jRadioButton1 = new JRadioButton(); JRadioButton jRadioButton2 = new JRadioButton(); ButtonGroup buttonGroup1 = new ButtonGroup(); JLabel jLabel3 = new JLabel(); JTextField jTextField3 = new JTextField(); JButton jButton3 = new JButton(); JTextArea jTextArea1 = new JTextArea(); List list1 = new List(); Socket socket=null; PrintStream ps=null; Listen listen=null; int count=0; //Construct the frame public Frame2() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("Frame Title"); jPanel1.setLayout(xYLayout2); jPanel2.setLayout(xYLayout3); jButton1.setText("发送"); jButton1.addActionListener(new Frame2_jButton1_actionAdapter(this)); jButton2.setActionCommand("消除"); jButton2.setText("退出"); jButton2.addActionListener(new Frame2_jButton2_actionAdapter(this)); jTextField2.setEnabled(false); jTextField2.setEditable(true); jTextField2.setText(""); jTextField2.addActionListener(new Frame2_jTextField2_actionAdapter(this)); jLabel1.setMinimumSize(new Dimension(33, 16)); jLabel1.setText("连接用户 "); jLabel2.setText("选择消息发送方式"); jRadioButton1.setText("群发"); jRadioButton2.setText("单发"); jLabel3.setOpaque(false); jLabel3.setText("连接用户名"); jButton3.setText("连接"); jButton3.addActionListener(new Frame2_jButton3_actionAdapter(this)); jTextArea1.setText(""); jTextArea1.setTabSize(8); jTextField3.setText(""); jTextField1.setText(""); jPanel1.add(jScrollPane1, new XYConstraints(6, 29, 129, 119)); jPanel1.add(jLabel1, new XYConstraints(16, 4, 64, -1)); jPanel1.add(jRadioButton1, new XYConstraints(12, 168, -1, 10)); jPanel1.add(jLabel2, new XYConstraints(6, 148, 118, -1)); jPanel1.add(jRadioButton2, new XYConstraints(11, 184, 78, 11)); contentPane.add(jButton1, new XYConstraints(258, 221, 58, -1)); jScrollPane1.getViewport().add(list1, null); contentPane.add(jTextField1, new XYConstraints(8, 219, 245, 24)); contentPane.add(jTextField2, new XYConstraints(9, 254, 376, 23)); contentPane.add(jPanel2, new XYConstraints(142, 2, 249, 200)); jPanel2.add(jTextArea1, new XYConstraints(6, 34, 245, 165)); jPanel2.add(jButton3, new XYConstraints(183, 7, 61, 19)); jPanel2.add(jTextField3, new XYConstraints(76, 7, 99, -1)); jPanel2.add(jLabel3, new XYConstraints(6, 7, 69, 23)); contentPane.add(jButton2, new XYConstraints(330, 221, 60, -1)); contentPane.add(jPanel1, new XYConstraints(4, 7, -1, -1)); buttonGroup1.add(jRadioButton2); buttonGroup1.add(jRadioButton1); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void jTextField2_actionPerformed(ActionEvent e) { }//发送消息 void jButton1_actionPerformed(ActionEvent e) { if(socket!=null){ StringBuffer message=new StringBuffer("MSG"); try{ String msg=new String(jTextField1.getText()); }catch(Exception ee){} if(jRadioButton1.isSelected()){ message.append("BROAD:"); }else{ message.append(list1.getSelectedItem()+":"); } message.append(listen.name+":"); ps.println(message.append(jTextField1.getText())); ps.flush(); } } //退出 void jButton2_actionPerformed(ActionEvent e) { disconnect(); System.exit(0); } //对客户端连接监听 void jButton3_actionPerformed(ActionEvent e) { if(socket==null){ try{ socket=new Socket(InetAddress.getLocalHost(),4000); if(socket!=null){ ps=new PrintStream(socket.getOutputStream()); StringBuffer info=new StringBuffer("PEOPLE:"); String userinfo=jTextField3.getText()+":"+InetAddress.getLocalHost().toString(); ps.println(info.append(userinfo)); ps.flush(); listen=new Listen(this,jTextField3.getText(),socket);; listen.start(); jTextArea1.append(jTextField3.getText()+"登陆成功!"+"\n"); count++; jTextField2.setText("目前连接用户数:"+String.valueOf(count)); }//end if else{ jTextArea1.append(jTextField3.getText()+"登陆失败!"); }//end else }catch(IOException ee){ jTextArea1.append(jTextField3.getText()+"登陆失败"); //disconnect(); }//end try-catch }//end if }//end 监听 public void disconnect(){ if(socket!=null){ try{ ps.println("QUIT:"+listen.name); ps.flush(); socket.close(); count--; }catch(IOException ee){ jTextArea1.append("ERROR:"+ee); } } }//对服务器信息的监听 class Listen extends Thread{ String name=null; DataInputStream dis=null; Frame2 parent=null; public Listen(Frame2 p ,String n,Socket s){ parent=p; name=n; socket=s; try{ dis=new DataInputStream(s.getInputStream()); ps=new PrintStream(s.getOutputStream()); }catch(IOException e){ parent.jTextArea1.append("ERROR :"+e+"\n"); parent.disconnect(); }//end try-catch } public void run(){ String message=null; while(true){ try{ message=dis.readLine(); }catch(IOException e){ parent.jTextArea1.append("ERROR :"+e+"\n"); parent.disconnect(); } if(message==null){ parent.listen=null; parent.socket=null; parent.list1=null; return; } StringTokenizer str=new StringTokenizer(message,":"); String keyword=str.nextToken(); }//end while}//end run }////:~end class Listen}class Frame2_jTextField2_actionAdapter implements java.awt.event.ActionListener { Frame2 adaptee; Frame2_jTextField2_actionAdapter(Frame2 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jTextField2_actionPerformed(e); }}class Frame2_jButton1_actionAdapter implements java.awt.event.ActionListener { Frame2 adaptee; Frame2_jButton1_actionAdapter(Frame2 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }}class Frame2_jButton3_actionAdapter implements java.awt.event.ActionListener { Frame2 adaptee; Frame2_jButton3_actionAdapter(Frame2 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton3_actionPerformed(e); }}class Frame2_jButton2_actionAdapter implements java.awt.event.ActionListener { Frame2 adaptee; Frame2_jButton2_actionAdapter(Frame2 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -