📄 client.java
字号:
setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); leftPanel.setLayout(null); leftPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0))); leftPanel.setOpaque(false); firstNameLabel.setText("First Name"); firstNameLabel.setName("firstNameLabel"); leftPanel.add(firstNameLabel); firstNameLabel.setBounds(20, 30, 80, 17); lastNameLabel.setText("Last Name"); lastNameLabel.setName("lastNameLabel"); leftPanel.add(lastNameLabel); lastNameLabel.setBounds(20, 60, 80, 17); firstName.setName("firstName"); leftPanel.add(firstName); firstName.setBounds(120, 30, 100, 21); lastName.setName("lastName"); leftPanel.add(lastName); lastName.setBounds(120, 60, 100, 21); ageLabel.setText("Age"); ageLabel.setDisabledIcon(new javax.swing.ImageIcon("ageLabel")); leftPanel.add(ageLabel); ageLabel.setBounds(20, 90, 70, 17); age.setName("age"); leftPanel.add(age); age.setBounds(120, 90, 100, 21); send.setText("Send Info"); send.setName("send"); leftPanel.add(send); send.setBounds(50, 170, 120, 27); getContentPane().add(leftPanel); leftPanel.setBounds(20, 20, 230, 230); rightPanel.setLayout(null); rightPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0))); statusLabel.setText("Status"); statusLabel.setName("statusLabel"); rightPanel.add(statusLabel); statusLabel.setBounds(20, 10, 50, 17); status.setText("Not Connected"); status.setName("status"); rightPanel.add(status); status.setBounds(20, 30, 140, 17); serverLabel.setText("Server"); serverLabel.setName("serverNameLabel"); rightPanel.add(serverLabel); serverLabel.setBounds(20, 70, 80, 17); server.setName("serverName"); rightPanel.add(server); server.setBounds(20, 90, 140, 21); portLabel.setText("Port"); portLabel.setName("portLabel"); rightPanel.add(portLabel); portLabel.setBounds(20, 120, 50, 17); port.setName("port"); port.setText(Integer.toString(Server.PORT_NUMBER)); rightPanel.add(port); port.setBounds(20, 140, 140, 21); connect.setText("Connect"); connect.setName("connect"); rightPanel.add(connect); connect.setBounds(40, 170, 100, 27); getContentPane().add(rightPanel); rightPanel.setBounds(260, 20, 180, 230); bottomPanel.setLayout(null); bottomPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0))); messageScroller.setViewportView(serverMessages); bottomPanel.add(messageScroller); messageScroller.setBounds(0, 0, 420, 100); getContentPane().add(bottomPanel); bottomPanel.setBounds(20, 270, 420, 100); serverMessagesLabel.setText("Server Messages"); serverMessagesLabel.setName("serverMessagesLabel"); getContentPane().add(serverMessagesLabel); serverMessagesLabel.setBounds(20, 250, 110, 17); setEnabledForm(false); setSize(480,420); } /** * This method handles button pressed events, ie. <i>Connect/Disconnect</i> and <i>send * information</i>. * @pre true * @post true * @param e ActionEvent object describing which kind of event occurred in * the gui. */ public void actionPerformed(ActionEvent e) { Object o = e.getSource(); if (o==send) { try { PersonalInformation p = new PersonalInformation(firstName.getText(), lastName.getText(), Integer.parseInt(age.getText())); clientConnectionManager.sendDataEvent(new DataEvent(new Integer(1), p)); firstName.setText(""); lastName.setText(""); age.setText(""); } catch (Exception ex) { serverMessages.append("Something's wrong with the data you entered!\n"); } } if ((o==connect) && (! connected)) { try { clientConnectionManager.setPortNumber(Integer.parseInt(port.getText())); } catch (Exception ex) { port.setText(Integer.toString(clientConnectionManager.getPortNumber())); } if (clientConnectionManager.open(server.getText())) { status.setText("Connected to server"); connect.setText("Disconnect"); connected = true; setEnabledForm(true); } else { serverMessages.append("Connection failed!\n"); } } else { if ((o==connect) && (connected)) { setDisconnectedState(); } } } /** * @obligation method may not be called */ public void setDisconnectedState() { setEnabledForm(false); clientConnectionManager.close(); status.setText("Connection closed"); connect.setText("Connect"); connected = false; } /** * This method enables/disables the part of gui which can only be used depending * on the state of a connection, i.e. when a connection is closed or broken, * the fields should be disabled, if a connection is opened the fields should be * enabled. * @pre true * @post (lastName.getEnabled() && send.getEnabled() && leftPanel.getEnabled() && * firstName.getEnabled() && age.getEnabled()) == true * @param flag If true the connection dependent parts of the gui are enabled, * if false the connection dependent parts are disabled. */ private void setEnabledForm(boolean flag) { lastName.setEnabled(flag); send.setEnabled(flag); leftPanel.setEnabled(flag); firstName.setEnabled(flag); age.setEnabled(flag); } /** * This method closes the gui and terminates the application, * as soon the user closes it. * @pre true * @post true */ private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); } /** * This method starts up the client. * @param args the command line arguments */ public static void main(String args[]) { new Client().setVisible(true); } /** * This interface method is called by the communication component. The implenting * class has to register at the specific * object of the class {@link net.sf.cscc.ClientConnectionManager#addObserver(DataEventBaseObserver debo)} * to get notifications about new data events in the event queue. The data is * given as argument. * * @pre true * @post true * @param de DataEvent object, encapsulating the transmitted data. * @see net.sf.cscc.DataEventObserver#receiveEvent() */ public void receiveEvent(DataEvent de) { serverMessages.append("Data from Server: " + de.getData()); } /** * This interface method is called by communication component class, every time a * communication event occurred. The implenting class has to register at the specific * object of the class {@link net.sf.cscc.ClientConnectionManager#addObserver(DataEventBaseObserver debo)} * to get notifications about new communication events in the event queue. This is * done by calling the method {@link net.sf.cscc.ServerConnectionManager#addObserver(CommunicationEventObserver o)} * for observing the communication events of the server, respectively the method * {@link net.sf.cscc.ClientConnectionManager#addObserver(CommunicationEventObserver o)} for * the communication events of the client. * * @pre true * @post true * @param ce CommunicationEvent which is delivered to the implementor of * this interface (ie. the observer). */ public void receiveEvent(CommunicationEvent ce) { switch (ce.getEventId()) { case (CommunicationEvent.CONNECTION_ESTABLISHED): serverMessages.append("connection opened! \n"); break; case (CommunicationEvent.CONNECTION_BROKEN): serverMessages.append("connection broken! \n"); status.setText("Connection broken"); setDisconnectedState(); break; case (CommunicationEvent.CONNECTION_CLOSED): serverMessages.append("connection closed! \n"); break; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -