문제
채팅 GUI 클라이언트 문제입니다 이를 해결하는 다음 프로그램에 대해 빈칸을 채우세요
코드 빈칸 채우기
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import chat.ChatNetworkPanel;
public class ChatGUIClient
{
public static void main( String[] args ) {
String serverIP = "";
ChatNetworkPanel panel =
new ChatNetworkPanel( serverIP );
JFrame frame = new JFrame( "채팅(클라이언트)" );
frame.getContentPane().add( panel );
frame.addWindowListener( new () {
public void ( WindowEvent event ) {
panel.close();
}
} );
frame.setPreferredSize( new Dimension( 320, 445 ) );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
package chat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import network.Network;
public class ChatNetworkPanel JPanel ActionListener,
{
private JTextArea chatWindow;
private JTextField inputField;
private Network network;
private String id;
private final String header = "[CHAT] ";
public ChatNetworkPanel() {
chatWindow = new JTextArea();
chatWindow.setBackground( Color.lightGray );
chatWindow.setEditable( false );
chatWindow.setLineWrap( true );
JScrollPane scroll = new JScrollPane( chatWindow );
scroll.setPreferredSize( new Dimension( 290, 360 ) );
add( scroll );
inputField = new JTextField();
inputField.setPreferredSize( new Dimension( 290, 30 ) );
inputField.addActionListener( this );
inputField.requestFocus();
add( inputField );
setVisible( true );
}
public ChatNetworkPanel( String serverIP ) {
this();
network =
new Network();
network.connectAsClient( serverIP, this );
id = "[" + JOptionPane.showInputDialog( this, "아이디를 입력해주세요.", "아이디" ) + "]";
network.write( header + id + "님이 들어오셨습니다" );
}
@Override
public void actionPerformed( ActionEvent event ) {
String message = inputField.getText();
network.write( header + id + message );
inputField.setText( "" );
}
@Override
public void () {
for ( String message = null; ( message =
network.read() )
!= null; ) {
if ( !message.contains( header ) )
continue;
chatWindow.append( message.substring( header.length() ) + "\n" );
chatWindow.setCaretPosition( chatWindow.getText().length() );
}
}
public void close() {
network.write( header + " " + id + "님이 나가셨습니다" );
network.close();
}
}
package network;
import java.io.*;
import java.net.*;
public class Network
{
public static final int serverPort = 7700;
protected serverSocket;
protected socket;
private in;
private out;
private waitForCounterpart;
public Network() {
serverSocket = null;
socket = null;
in = null;
out = null;
waitForCounterpart = null;
}
public void connectAsServer( obj ) {
{
serverSocket = new ( serverPort );
socket =
serverSocket.();
connectInOut( obj );
} ( Exception e ) {
e.printStackTrace();
}
}
public void connectAsClient( String serverIP, obj ) {
{
socket =
new ( serverIP, serverPort );
connectInOut( obj );
} ( Exception e ) {
e.printStackTrace();
}
}
public void connectInOut( obj ) {
{
in = new ( new ( socket.() ));
out = new ( socket.(), true );
waitForCounterpart = new ( obj );
waitForCounterpart.();
} ( Exception e ) {
e.printStackTrace();
}
}
public String read() {
{
if ( this.isConnecting() == true )
return
in.readLine();
} ( Exception e ) {
e.printStackTrace();
}
return null;
}
public void write( String data ) {
{
if ( this.isConnecting() == true )
out.println( data );
} ( Exception e ) {
e.printStackTrace();
}
}
public boolean isConnecting() {
if ( ( socket != null )
&& ( in != null )
&& ( out != null )
&& ( waitForCounterpart != null )
&& ( waitForCounterpart.getState() != .State.TERMINATED ) )
return true;
else
return false;
}
public void close() {
{
if ( waitForCounterpart != null ) {
waitForCounterpart.();
waitForCounterpart = null;
}
if ( in != null ) {
in.close();
in=null;
}
if ( out != null ) {
out.flush();
out.close();
out = null;
}
if ( socket != null ) {
socket.close();
socket = null;
}
if ( serverSocket != null ) {
serverSocket.close();
serverSocket = null;
}
} ( Exception e ) {
e.printStackTrace();
}
}
}