D:\mine>javac -deprecation TicTacToeClient.java TicTacToeClient.java:75: warning: mouseUp(java.awt.Event,int,int) in java.awt.Co mponent has been deprecated public boolean mouseUp( Event e, int x, int y ) ^ TicTacToeClient.java:133: warning: appendText(java.lang.String) in java.awt.Text Area has been deprecated display.appendText( "Valid move,please wait.\n" ); ^ TicTacToeClient.java:134: cannot resolve symbol symbol : method setMark (char) location: class Square currentSquare.setMark( myMark ); ^ TicTacToeClient.java:139: warning: appendText(java.lang.String) in java.awt.Text Area has been deprecated display.appendText( s + "\n" ); ^ TicTacToeClient.java:152: cannot resolve symbol symbol : method setMark (char) location: class Square board[ row ][ col ].setMark( ( myMark == 'X' ? 'O' :'X' ) ); ^ TicTacToeClient.java:157: warning: appendText(java.lang.String) in java.awt.Text Area has been deprecated display.appendText( "Opponent moved.Your turn.\n" ); ^ TicTacToeClient.java:166: warning: appendText(java.lang.String) in java.awt.Text Area has been deprecated display.appendText( s + "\n" ); ^ TicTacToeClient.java:178: warning: resize(int,int) in java.awt.Component has bee n deprecated resize( 30, 30 ); ^ 2 errors 6 warningsD:\mine>
D:\mine>javac TicTacToeClient.java TicTacToeClient.java:134: cannot resolve symbol symbol : method setMark (char) location: class Square currentSquare.setMark( myMark ); ^ TicTacToeClient.java:152: cannot resolve symbol symbol : method setMark (char) location: class Square board[ row ][ col ].setMark( ( myMark == 'X' ? 'O' :'X' ) ); ^ Note: TicTacToeClient.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details. 2 errors
在你的Square对象中缺少一个方法 public void setMark(char ){ this. = ; } --------------------------------full source -------------------------- //client for the TicTacToe program import java.applet.Applet; import java.awt.*; import java.net.*; import java.io.*;//client class to let a user play Tic-Tac-Toe with //another user across a network. public class TicTacToeClient extends Applet implements Runnable { TextField id; TextArea display; Panel boardPanel, panel2; Square board[][], currentSquare; Socket connection; DataInputStream input; DataOutputStream output; Thread outputThread; char myMark; //set up user-interface and board public void init() { setLayout( new BorderLayout() ); display = new TextArea( 4, 30 ); display.setEditable( false ); add( "South", display ); boardPanel = new Panel(); boardPanel.setLayout( new GridLayout( 3, 3, 0, 0 ) ); board = new Square[ 3 ][ 3 ]; for( int row = 0; row < board.length; row++ ) for ( int col = 0; col < board[ row ].length; col++ ) { board[ row ][ col ] = new Square(); boardPanel.add( board[ row ][ col ] ); } id = new TextField(); id.setEditable( false ); add( "North", id ); panel2 = new Panel(); panel2.add( boardPanel ); add( "Center", panel2 ); } //make connection to server and get associated streams. //start separate thread to allow this applet to //continually update its output in text area display. public void start() { try { connection = new Socket( InetAddress.getLocalHost(), 5000 ); input = new DataInputStream( connection.getInputStream() ); output = new DataOutputStream( connection.getOutputStream() ); } catch ( IOException e) { e.printStackTrace(); } outputThread = new Thread( this ); outputThread.start(); } //when user clicks mouse and releases in a square, //send an integer representing the clicked square //to the server.The integer is a number from 0 to 8 //where the first row contains positions 0, 1 and 2, //the secend row contains positions 3, 4 and 5 and //the third row contains positions 6, 7 and 8. public boolean mouseUp( Event e, int x, int y ) { for ( int row = 0; row < board.length ;row++ ) { for ( int col = 0 ; col < board[ row ].length ;col++ ) { try { if ( e.target == board[ row ][ col ] ) { currentSquare = board[ row ][ col ]; output.writeInt( row*3+col ); } } catch ( IOException ie) { ie.printStackTrace(); } } } return true; } //control thread that allows continuous update of the //text area display. public void run() { //first get player's ( X or O ) try { myMark = input.readChar(); id.setText( "You are player \"" + myMark + "\"" ); } catch ( IOException e) { e.printStackTrace(); } //receive message sent to client while( true ) { try { String s = input.readUTF(); processMessage( s ); } catch ( IOException e) { e.printStackTrace(); } } } //process message sent to client public void processMessage( String s ) { if (s.equals( "Valid move." ) ) { display.appendText( "Valid move,please wait.\n" ); currentSquare.setMark( myMark ); currentSquare.repaint(); } else if (s.equals( "Invalid move,tra again" ) ) { display.appendText( s + "\n" ); } else if (s.equals( "Opponent moved" ) ) { try { int loc = input.readInt(); done: for ( int row = 0; row < board.length ; row++ ) for ( int col = 0; col < board[ row ].length ; col++ ) if ( row*3+col == loc) { board[ row ][ col ].setMark( ( myMark == 'X' ? 'O' :'X' ) ); board[ row ][ col ].repaint(); break done; } display.appendText( "Opponent moved.Your turn.\n" ); } catch ( IOException e) { e.printStackTrace(); } } else { display.appendText( s + "\n" ); } } }//maintains one square on the board class Square extends Canvas { char ; public Square() { resize( 30, 30 ); } public void paint( Graphics g ) { g.drawRect( 0, 0, 29, 29 ); g.drawString( String.valueOf( ), 11, 20 ); }
这两个提示是警告,说你使用了被deprecated的类或者方法
这个是不影响你的程序运行的
java TicTacToeClient.java这正是java的独特之处啊,这些类在JDK1.4已经不赞成使用了
Exception in thread "main" java.lang.NoClassDefFoundError: TicTacToeClient/java
D:\mine>appletviewer TicTacToeClient
TicTacToeClient.java:75: warning: mouseUp(java.awt.Event,int,int) in java.awt.Co
mponent has been deprecated
public boolean mouseUp( Event e, int x, int y )
^
TicTacToeClient.java:133: warning: appendText(java.lang.String) in java.awt.Text
Area has been deprecated
display.appendText( "Valid move,please wait.\n" );
^
TicTacToeClient.java:134: cannot resolve symbol
symbol : method setMark (char)
location: class Square
currentSquare.setMark( myMark );
^
TicTacToeClient.java:139: warning: appendText(java.lang.String) in java.awt.Text
Area has been deprecated
display.appendText( s + "\n" );
^
TicTacToeClient.java:152: cannot resolve symbol
symbol : method setMark (char)
location: class Square
board[ row ][ col ].setMark( ( myMark == 'X' ?
'O' :'X' ) );
^
TicTacToeClient.java:157: warning: appendText(java.lang.String) in java.awt.Text
Area has been deprecated
display.appendText( "Opponent moved.Your turn.\n" );
^
TicTacToeClient.java:166: warning: appendText(java.lang.String) in java.awt.Text
Area has been deprecated
display.appendText( s + "\n" );
^
TicTacToeClient.java:178: warning: resize(int,int) in java.awt.Component has bee
n deprecated
resize( 30, 30 );
^
2 errors
6 warningsD:\mine>
进行读取时出现 I/O 异常: D:\mine\TicTacToeClient (系统找不到指定的文件。)
TicTacToeClient.java:134: cannot resolve symbol
symbol : method setMark (char)
location: class Square
currentSquare.setMark( myMark );
^
TicTacToeClient.java:152: cannot resolve symbol
symbol : method setMark (char)
location: class Square
board[ row ][ col ].setMark( ( myMark == 'X' ?
'O' :'X' ) );
^
Note: TicTacToeClient.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
2 errors
public void setMark(char ){
this. = ;
}
--------------------------------full source --------------------------
//client for the TicTacToe program
import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.io.*;//client class to let a user play Tic-Tac-Toe with
//another user across a network.
public class TicTacToeClient extends Applet implements Runnable
{
TextField id;
TextArea display;
Panel boardPanel, panel2;
Square board[][], currentSquare;
Socket connection;
DataInputStream input;
DataOutputStream output;
Thread outputThread;
char myMark; //set up user-interface and board
public void init()
{
setLayout( new BorderLayout() );
display = new TextArea( 4, 30 );
display.setEditable( false );
add( "South", display ); boardPanel = new Panel();
boardPanel.setLayout( new GridLayout( 3, 3, 0, 0 ) );
board = new Square[ 3 ][ 3 ]; for( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board[ row ].length; col++ )
{
board[ row ][ col ] = new Square();
boardPanel.add( board[ row ][ col ] );
} id = new TextField();
id.setEditable( false );
add( "North", id ); panel2 = new Panel();
panel2.add( boardPanel );
add( "Center", panel2 );
} //make connection to server and get associated streams.
//start separate thread to allow this applet to
//continually update its output in text area display.
public void start()
{
try
{
connection = new Socket( InetAddress.getLocalHost(), 5000 );
input = new DataInputStream( connection.getInputStream() );
output = new DataOutputStream( connection.getOutputStream() );
}
catch ( IOException e)
{
e.printStackTrace();
} outputThread = new Thread( this );
outputThread.start();
} //when user clicks mouse and releases in a square,
//send an integer representing the clicked square
//to the server.The integer is a number from 0 to 8
//where the first row contains positions 0, 1 and 2,
//the secend row contains positions 3, 4 and 5 and
//the third row contains positions 6, 7 and 8.
public boolean mouseUp( Event e, int x, int y )
{
for ( int row = 0; row < board.length ;row++ )
{
for ( int col = 0 ; col < board[ row ].length ;col++ )
{
try
{
if ( e.target == board[ row ][ col ] )
{
currentSquare = board[ row ][ col ];
output.writeInt( row*3+col );
}
}
catch ( IOException ie)
{
ie.printStackTrace();
}
}
}
return true;
} //control thread that allows continuous update of the
//text area display.
public void run()
{
//first get player's ( X or O )
try
{
myMark = input.readChar();
id.setText( "You are player \"" + myMark + "\"" );
}
catch ( IOException e)
{
e.printStackTrace();
} //receive message sent to client
while( true )
{
try
{
String s = input.readUTF();
processMessage( s );
}
catch ( IOException e)
{
e.printStackTrace();
}
}
} //process message sent to client
public void processMessage( String s )
{
if (s.equals( "Valid move." ) )
{
display.appendText( "Valid move,please wait.\n" );
currentSquare.setMark( myMark );
currentSquare.repaint();
}
else if (s.equals( "Invalid move,tra again" ) )
{
display.appendText( s + "\n" );
}
else if (s.equals( "Opponent moved" ) )
{
try
{
int loc = input.readInt(); done:
for ( int row = 0; row < board.length ; row++ )
for ( int col = 0; col < board[ row ].length ; col++ )
if ( row*3+col == loc)
{
board[ row ][ col ].setMark( ( myMark == 'X' ? 'O' :'X' ) );
board[ row ][ col ].repaint();
break done;
} display.appendText( "Opponent moved.Your turn.\n" );
}
catch ( IOException e)
{
e.printStackTrace();
}
}
else
{
display.appendText( s + "\n" );
}
}
}//maintains one square on the board
class Square extends Canvas
{
char ; public Square()
{
resize( 30, 30 );
} public void paint( Graphics g )
{
g.drawRect( 0, 0, 29, 29 );
g.drawString( String.valueOf( ), 11, 20 );
}
public void setMark(char ){
this. = ;
}
}