📄 client.java
字号:
/* * Copyright (c) 2003 - 2007, Silvio Meier and Tobias Reinhard * * All rights reserved. * * Redistribution and use in source and binary forms, * with or without modification, are permitted provided * that the following conditions are met: * * o Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * o Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * o The names of its contributors may not be used to endorse * or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package net.sf.cscc.examples.simplesystem;// awt packagesimport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import net.sf.cscc.ClientConnectionManager;import net.sf.cscc.CommunicationEvent;import net.sf.cscc.CommunicationEventObserver;import net.sf.cscc.DataEvent;import net.sf.cscc.DataEventBaseObserver;import net.sf.cscc.DataEventReceivingObserver;/** * This class provides a client application, which demonstrates the usage * of this client/server component. The client application allows to connect * to a specific server with a specific port and sends then some * information about the user over the net. The data is sent by calling * the method * {@link net.sf.cscc.ClientConnectionManager#sendDataEvent(DataEvent de)}. <br> * Important for using the client/server component is the implementation of the * methods {@link #receiveEvent(CommunicationEvent ce)} and {@link #receiveEvent(DataEvent de)} * which allow to receive data and communication events from the client/server * component. * * @author Silvio Meier * @copyright Silvio Meier, Tobias Reinhard, 2003 * @history 2003-05-15 SM First Version. * 2003-05-16 SM Comments and full implemented functionality. * 2003-05-17 SM Comments corrected * 2003-05-18 SM Unecessary close call removed when getting * a closed connection communication event. * 2003-05-28 SM Make up in gui resized some labels and buttons * to fit on screen with different sized fonts. * 2003-06-12 SM Now it is checked if the connection was * established or not. The client does not * change into connected status anymore, if the * connection was not established successfully. * 2004-12-13 SM Some cleaning of unused things. * 2006-11-28 SM Comments revised. * @version $Date: 2007/07/01 17:04:07 $, $Author: reode_orm $, $Revision: 1.1 $ */public class Client extends javax.swing.JFrame implements ActionListener, DataEventReceivingObserver, CommunicationEventObserver { /** * Serial Id. */ private static final long serialVersionUID = -1197331091403391467L; /** * Client connection manager, handles the connection to the server. */ private ClientConnectionManager clientConnectionManager = null; /** * Status label */ private javax.swing.JLabel status; /** * Textfield for entering the last name */ private javax.swing.JTextField lastName; /** * Button for start the sending of data. */ private javax.swing.JButton send; /** * Left panel to structure the output screen. */ private javax.swing.JPanel leftPanel; /** * Textarea which shows messages about all the events that occur. */ private javax.swing.JTextArea serverMessages; /** * Button for connecting to server. */ private javax.swing.JButton connect; /** * Label describing the status label * @see #status */ private javax.swing.JLabel statusLabel; /** * Textfield for entering the last name */ private javax.swing.JTextField firstName; /** * Right panel to structure the output screen. */ private javax.swing.JPanel rightPanel; /** * Bottom panel to structure the output screen. */ private javax.swing.JPanel bottomPanel; /** * Label describing the age field. */ private javax.swing.JLabel ageLabel; /** * Label describing the message text area field. */ private javax.swing.JLabel serverMessagesLabel; /** * Scroll panel for scrolling within the text area. */ private javax.swing.JScrollPane messageScroller; /** * Label indicating the textfield for entering the * the server name. */ private javax.swing.JLabel serverLabel; /** * Textfield for entering the TCP/IP port number. */ private javax.swing.JTextField port; /** * Textfield for entering the age of the person. */ private javax.swing.JTextField age; /** * Label indicating the textfield for entering the * last name. */ private javax.swing.JLabel lastNameLabel; /** * Label indicating the textfield for entering the * first name. */ private javax.swing.JLabel firstNameLabel; /** * Label indicating the textfield for entering the port * number. */ private javax.swing.JLabel portLabel; /** * Textfield for entering the destination server. */ private javax.swing.JTextField server; /** * Bottom panel to structure the output screen. */ private boolean connected = false; /** * Constructor initializes the gui and prepares for creating a * connection to a server * @pre true * @post true */ public Client() { super("Example Client"); // new client connection manager which handles the connection to the server clientConnectionManager = new ClientConnectionManager(Server.PORT_NUMBER); // register this class as observer for communication events clientConnectionManager.addObserver((CommunicationEventObserver)this); // register this class as observer for data events clientConnectionManager.addObserver((DataEventReceivingObserver)this); // init the gui elements initComponents(); } /** * This method initializes the gui. * @pre true * @post true */ private void initComponents() { leftPanel = new javax.swing.JPanel(); firstNameLabel = new javax.swing.JLabel(); lastNameLabel = new javax.swing.JLabel(); firstName = new javax.swing.JTextField(); lastName = new javax.swing.JTextField(); ageLabel = new javax.swing.JLabel(); age = new javax.swing.JTextField(); send = new javax.swing.JButton(); rightPanel = new javax.swing.JPanel(); statusLabel = new javax.swing.JLabel(); status = new javax.swing.JLabel(); serverLabel = new javax.swing.JLabel(); server = new javax.swing.JTextField(); portLabel = new javax.swing.JLabel(); port = new javax.swing.JTextField(); connect = new javax.swing.JButton(); bottomPanel = new javax.swing.JPanel(); messageScroller = new javax.swing.JScrollPane(); serverMessages = new javax.swing.JTextArea(); serverMessagesLabel = new javax.swing.JLabel(); send.addActionListener(this); connect.addActionListener(this); getContentPane().setLayout(null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -