📄 chatclientapplet.java~4~
字号:
package chatrmio;import java.awt.*;import java.awt.event.*;import java.applet.*;import com.borland.jbcl.layout.*;import javax.swing.*;import java.util.*;import java.rmi.*;import java.rmi.server.*;import java.rmi.registry.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2009</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class ChatClientApplet extends Applet implements IChatClient{ IChatService service=null; String user; boolean connected=false; private boolean isStandalone = false; XYLayout xYLayout1 = new XYLayout(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JTextField jTextField1 = new JTextField(); JTextField jTextField2 = new JTextField(); JTextField jTextField3 = new JTextField(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JTextField jTextField4 = new JTextField(); JLabel jLabel5 = new JLabel(); JButton jButton3 = new JButton(); JScrollPane jScrollPane1 = new JScrollPane(); JTextArea jTextArea1 = new JTextArea(); JLabel jLabel6 = new JLabel(); //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public ChatClientApplet() { //设置安全管理 System.setSecurityManager(new RMISecurityManager()); setStatus(); jTextField1.setEditable(false); jTextField1.setText("localhost"); jTextField2.setEnabled(false); jTextField2.setText("1055"); jTextArea1.setEditable(false); try { /* 导出对象 */ UnicastRemoteObject.exportObject(this); } catch(RemoteException e) { } } private void connect() { try {// service = (IChatService)Naming.lookup("rmi://localhost/IChatService"); connected=true; setStatus(); user=jTextField3.getText().trim(); service.addClient(this,user+"连入"); } catch(Exception e) { jTextArea1.append("连接失败 ...\n"); connected=false; setStatus(); service=null; System.out.println(e); } } private void disconnect() { try { if(service==null) return; service.removeClient(this,user+"退出"); service=null; } catch(Exception e) { jTextArea1.append("连接失败 ...\n"); } finally { connected=false; setStatus(); } } private void setStatus() { if(connected) { jLabel6.setText("连接状态"+" : 已连接"); } else { jLabel6.setText("连接状态"+" : 未连接"); } } // 实现IChatClient 接口getName() public String getName() { return user; } // 实现IChatClient 接口sendMessge() public void sendMessage(String msg) { jTextArea1.append(msg+"\n"); } //Initialize the applet public void init() { try { jbInit(); ChatClientApplet client=new ChatClientApplet();//调用构造方法 } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { jLabel1.setFont(new java.awt.Font("Dialog", 1, 20)); jLabel1.setForeground(Color.red); jLabel1.setText("JavaRMI聊天室客户端"); this.setBackground(new Color(150, 208, 255)); xYLayout1.setWidth(504); xYLayout1.setHeight(483); this.setLayout(xYLayout1); jLabel2.setFont(new java.awt.Font("Dialog", 1, 16)); jLabel2.setForeground(Color.blue); jLabel2.setText("服务器名:"); jLabel3.setFont(new java.awt.Font("Dialog", 1, 16)); jLabel3.setForeground(Color.blue); jLabel3.setText("服务端口:"); jLabel4.setFont(new java.awt.Font("Dialog", 1, 16)); jLabel4.setForeground(Color.blue); jLabel4.setText("你的名字:"); jButton1.setBackground(UIManager.getColor("OptionPane.questionDialog.titlePane.shadow")); jButton1.setFont(new java.awt.Font("Dialog", 1, 18)); jButton1.setForeground(Color.blue); jButton1.setToolTipText("连接服务器"); jButton1.setText("连 接"); jButton1.addActionListener(new ChatClientApplet_jButton1_actionAdapter(this)); jButton2.setBackground(UIManager.getColor("OptionPane.questionDialog.titlePane.shadow")); jButton2.setFont(new java.awt.Font("Dialog", 1, 18)); jButton2.setForeground(Color.red); jButton2.setToolTipText("与服务器断开"); jButton2.setText("断 开"); jButton2.addActionListener(new ChatClientApplet_jButton2_actionAdapter(this)); jTextField1.setBackground(UIManager.getColor("FormattedTextField.selectionBackground")); jTextField1.setForeground(Color.red); jTextField2.setBackground(new Color(215, 224, 242)); jTextField2.setForeground(Color.red); jTextField3.setBackground(new Color(215, 224, 242)); jTextField3.setFont(new java.awt.Font("Dialog", 0, 18)); jTextField3.setForeground(SystemColor.activeCaption); jLabel5.setFont(new java.awt.Font("Dialog", 1, 15)); jLabel5.setForeground(Color.blue); jLabel5.setText("信息输入框:"); jTextField4.setBackground(new Color(215, 224, 242)); jTextField4.setForeground(SystemColor.activeCaption); jButton3.setBackground(new Color(203, 158, 98)); jButton3.setFont(new java.awt.Font("Serif", 1, 18)); jButton3.setForeground(SystemColor.activeCaption); jButton3.setToolTipText("发送信息"); jButton3.setText("发 送"); jButton3.addActionListener(new ChatClientApplet_jButton3_actionAdapter(this)); jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane1.getViewport().setBackground(new Color(120, 143, 255)); jScrollPane1.setForeground(new Color(120, 143, 255)); jTextArea1.setBackground(new Color(215, 224, 242)); jLabel6.setFont(new java.awt.Font("DialogInput", 1, 16)); jLabel6.setForeground(UIManager.getColor("Button.focus")); jLabel6.setToolTipText(""); this.add(jLabel1, new XYConstraints(141, 24, 222, -1)); this.add(jLabel4, new XYConstraints(60, 150, -1, 30)); this.add(jLabel2, new XYConstraints(60, 70, -1, 30)); this.add(jLabel3, new XYConstraints(60, 110, -1, 30)); this.add(jTextField1, new XYConstraints(140, 70, 120, 30)); this.add(jTextField2, new XYConstraints(140, 110, 120, 30)); this.add(jTextField3, new XYConstraints(140, 150, 120, 30)); this.add(jButton2, new XYConstraints(330, 110, 100, 30)); this.add(jTextField4, new XYConstraints(140, 208, 188, 30)); this.add(jLabel5, new XYConstraints(50, 208, -1, 31)); this.add(jButton3, new XYConstraints(340, 208, 86, 31)); this.add(jScrollPane1, new XYConstraints(68, 254, 349, 206)); this.add(jButton1, new XYConstraints(330, 70, 100, 30)); this.add(jLabel6, new XYConstraints(300, 152, 171, 30)); jScrollPane1.getViewport().add(jTextArea1, null); } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } void jButton1_actionPerformed(ActionEvent e) { connect(); if(connected) { jTextField3.setEnabled(false); jButton1.setEnabled(false); } } void jButton2_actionPerformed(ActionEvent e) { disconnect(); jTextField1.setEnabled(true); jTextField2.setEnabled(true); jTextField3.setEnabled(true); jButton1.setEnabled(true); } void jButton3_actionPerformed(ActionEvent e) { if(service==null) return; try { service.sendMessage(this,jTextField4.getText()); jTextField4.setText(""); }catch(RemoteException exp) { System.out.println(exp); jTextArea1.append("信息发送失败 ...\n"); } }}class ChatClientApplet_jButton1_actionAdapter implements java.awt.event.ActionListener { ChatClientApplet adaptee; ChatClientApplet_jButton1_actionAdapter(ChatClientApplet adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }}class ChatClientApplet_jButton2_actionAdapter implements java.awt.event.ActionListener { ChatClientApplet adaptee; ChatClientApplet_jButton2_actionAdapter(ChatClientApplet adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); }}class ChatClientApplet_jButton3_actionAdapter implements java.awt.event.ActionListener { ChatClientApplet adaptee; ChatClientApplet_jButton3_actionAdapter(ChatClientApplet adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton3_actionPerformed(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -