import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.util.*;
import java.awt.datatransfer.*;
import java.util.Date;
public class ClientChat extends JFrame implements ActionListener,Runnable
{
public JFrame aFrame;
public JTextArea aTextArea, bTextArea;
public JLabel aLabel;
public JTextField aTextField;
public JButton aButton;
public JPanel aPanel1,aPanel2,aPanel3;
public JScrollPane aScrollPane,bScrollPane; Socket socket;
InputStream Is;
PrintStream PS;
OutputStream Os;
BufferedReader DIS;
String s;
String ClientName=""; public ClientChat(String name)
{
super("ClientChat");
this.ClientName=name;
setSize(400,300);
show();
this.ClientName=JOptionPane.showInputDialog(null,"Name");
this.setTitle(this.ClientName);
getContentPane().setLayout(new BorderLayout());
aTextArea =new JTextArea();
aTextArea.setBounds(20,20,300,300);
bTextArea = new JTextArea();
bTextArea.setBounds(0,0,200,200);
aLabel =new JLabel("我的第一个聊天程序!!!!");
aTextField = new JTextField(10);
aButton =new JButton("发送");
aButton.addActionListener(this);
aPanel1 =new JPanel();
aPanel2 =new JPanel();
aPanel3 = new JPanel();
aPanel1.setLayout(new BorderLayout());
aPanel2.setLayout(new BorderLayout());
aPanel3.setLayout(new BorderLayout());
aScrollPane =new JScrollPane(aTextArea);
bScrollPane =new JScrollPane(bTextArea);
getContentPane().add(aPanel1,BorderLayout.CENTER);
getContentPane().add(aPanel2,BorderLayout.SOUTH);
aPanel1.add(aScrollPane,BorderLayout.CENTER);
aPanel1.add(aLabel,BorderLayout.SOUTH);
aPanel3.add(aTextField,BorderLayout.WEST);
aPanel3.add(aButton,BorderLayout.EAST);
aPanel2.add(bScrollPane,BorderLayout.CENTER);
aPanel2.add(aPanel3,BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
startNet();
bTextArea.requestFocus();
}
public void startNet()
{
try
{
socket =new Socket(InetAddress.getLocalHost(),2020);
aTextField.setText(socket.getInetAddress().getHostAddress());
Is= socket.getInputStream();
Os= socket.getOutputStream();
DIS=new BufferedReader(new InputStreamReader(Is));
PS= new PrintStream(Os);
Thread thread1=new Thread(this);
thread1.start();
}catch(Exception e){}
}
public void run()
{ try{
int flag=0;
while(flag==0)
{
s=DIS.readLine();
aTextArea.append("server reply:"+"\n"+s+"\n");
if(s.trim().equals("bye"))
{
flag=1;
PS.println("bye");
DIS.close();
PS.close();
Is.close();
Os.close();
socket.close();
startNet();
}
}
}catch(Exception e){}
} public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="发送")
try{
PS.println(bTextArea.getText()); }
catch(Exception ex){
System.out.println("Error:"+ex);
}
aTextArea.append("client said:"+"\n"+bTextArea.getText()+"\n");
bTextArea.setText("");
}
public static void main(String[] args)
{
ClientChat c=new ClientChat("aa");
c.startNet();
}
}
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.util.*;
import java.awt.datatransfer.*;
import java.util.Date;
public class ClientChat extends JFrame implements ActionListener,Runnable
{
public JFrame aFrame;
public JTextArea aTextArea, bTextArea;
public JLabel aLabel;
public JTextField aTextField;
public JButton aButton;
public JPanel aPanel1,aPanel2,aPanel3;
public JScrollPane aScrollPane,bScrollPane; Socket socket;
InputStream Is;
PrintStream PS;
OutputStream Os;
BufferedReader DIS;
String s;
String ClientName=""; public ClientChat(String name)
{
super("ClientChat");
this.ClientName=name;
setSize(400,300);
show();
this.ClientName=JOptionPane.showInputDialog(null,"Name");
this.setTitle(this.ClientName);
getContentPane().setLayout(new BorderLayout());
aTextArea =new JTextArea();
aTextArea.setBounds(20,20,300,300);
bTextArea = new JTextArea();
bTextArea.setBounds(0,0,200,200);
aLabel =new JLabel("我的第一个聊天程序!!!!");
aTextField = new JTextField(10);
aButton =new JButton("发送");
aButton.addActionListener(this);
aPanel1 =new JPanel();
aPanel2 =new JPanel();
aPanel3 = new JPanel();
aPanel1.setLayout(new BorderLayout());
aPanel2.setLayout(new BorderLayout());
aPanel3.setLayout(new BorderLayout());
aScrollPane =new JScrollPane(aTextArea);
bScrollPane =new JScrollPane(bTextArea);
getContentPane().add(aPanel1,BorderLayout.CENTER);
getContentPane().add(aPanel2,BorderLayout.SOUTH);
aPanel1.add(aScrollPane,BorderLayout.CENTER);
aPanel1.add(aLabel,BorderLayout.SOUTH);
aPanel3.add(aTextField,BorderLayout.WEST);
aPanel3.add(aButton,BorderLayout.EAST);
aPanel2.add(bScrollPane,BorderLayout.CENTER);
aPanel2.add(aPanel3,BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
startNet();
bTextArea.requestFocus();
}
public void startNet()
{
try
{
socket =new Socket(InetAddress.getLocalHost(),2020);
aTextField.setText(socket.getInetAddress().getHostAddress());
Is= socket.getInputStream();
Os= socket.getOutputStream();
DIS=new BufferedReader(new InputStreamReader(Is));
PS= new PrintStream(Os);
Thread thread1=new Thread(this);
thread1.start();
}catch(Exception e){}
}
public void run()
{ try{
int flag=0;
while(flag==0)
{
s=DIS.readLine();
aTextArea.append("server reply:"+"\n"+s+"\n");
if(s.trim().equals("bye"))
{
flag=1;
PS.println("bye");
DIS.close();
PS.close();
Is.close();
Os.close();
socket.close();
startNet();
}
}
}catch(Exception e){}
} public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="发送")
try{
PS.println(bTextArea.getText()); }
catch(Exception ex){
System.out.println("Error:"+ex);
}
aTextArea.append("client said:"+"\n"+bTextArea.getText()+"\n");
bTextArea.setText("");
}
public static void main(String[] args)
{
ClientChat c=new ClientChat("aa");
c.startNet();
}
}
C:\Documents and Settings\p3>javac ClintChat.java
error: cannot read: ClintChat.java
1 errorWHY??
public class ServerChat extends JFrame implements ActionListener,Runnable
^
1 errorProcess completed.
這是在JCreate 编译中出现的提示,看的不是很明白,能不能解释以下呢??
你的第一个问题是:编译java文件要到类文件目录下
你的问题是如果声明的类为公共的即(public )则类文件名应该和类名相同