简单改了一下,写了一个main方法,其中用了JFrame,在放到页面中时应去掉此方法.否则javax.swing找不到.
服务器不用applet,就不用改了.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;public class AppletClient extends Applet {
Label label=new Label("交谈");
Panel panel=new Panel();
TextField tf=new TextField(10);
TextArea ta=new TextArea();
Socket client;
InputStream in;
OutputStream out; boolean isStandalone = false;
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public AppletClient() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(new BorderLayout());
this.setSize(new Dimension(250,250));
tf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
tf_actionPerformed(e);
}
});
panel.add(label);
panel.add(tf);
this.add(panel,BorderLayout.NORTH);
this.add(ta,BorderLayout.CENTER);
show();
try{
client=new Socket(InetAddress.getLocalHost(),4000);
ta.append("几经接到服务器:"+client.getInetAddress().getHostName()+"\n\n");
in=client.getInputStream();
out=client.getOutputStream();
}catch(IOException ioe){}
while(true){
try{byte[] buf=new byte[256];
in.read(buf);
String str=new String(buf);
ta.append("服务器说:"+str);
ta.append("\n");
}catch(IOException e){}
} }
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
} //static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
} void tf_actionPerformed(ActionEvent e) {
try{
String str=tf.getText();
byte[] buf=str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("我说:"+str);
ta.append("\n");
}
catch(IOException ioe){} } public static void main(String[] args){
javax.swing.JFrame frame = new javax.swing.JFrame("¿Í»§»ú");
AppletClient ac = new AppletClient();
frame.setContentPane(ac);
frame.setSize(ac.getSize());
frame.pack();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
ac.init();
}
}
服务器不用applet,就不用改了.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;public class AppletClient extends Applet {
Label label=new Label("交谈");
Panel panel=new Panel();
TextField tf=new TextField(10);
TextArea ta=new TextArea();
Socket client;
InputStream in;
OutputStream out; boolean isStandalone = false;
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public AppletClient() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(new BorderLayout());
this.setSize(new Dimension(250,250));
tf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
tf_actionPerformed(e);
}
});
panel.add(label);
panel.add(tf);
this.add(panel,BorderLayout.NORTH);
this.add(ta,BorderLayout.CENTER);
show();
try{
client=new Socket(InetAddress.getLocalHost(),4000);
ta.append("几经接到服务器:"+client.getInetAddress().getHostName()+"\n\n");
in=client.getInputStream();
out=client.getOutputStream();
}catch(IOException ioe){}
while(true){
try{byte[] buf=new byte[256];
in.read(buf);
String str=new String(buf);
ta.append("服务器说:"+str);
ta.append("\n");
}catch(IOException e){}
} }
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
} //static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
} void tf_actionPerformed(ActionEvent e) {
try{
String str=tf.getText();
byte[] buf=str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("我说:"+str);
ta.append("\n");
}
catch(IOException ioe){} } public static void main(String[] args){
javax.swing.JFrame frame = new javax.swing.JFrame("¿Í»§»ú");
AppletClient ac = new AppletClient();
frame.setContentPane(ac);
frame.setSize(ac.getSize());
frame.pack();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
ac.init();
}
}
解决方案 »
- jquery 获取一些属性的问题
- 这个java移位程序怎样推算呢?
- 如和将dom4j-1.6.zip整合到MyEclipse6.0中?
- 真的能够实现吗???
- Java中如何实现类似C++的多重继承
- 急死人了,请问怎么改变JComboBox的选择背景色………………
- 字符串:str = "21495" 如何转换成 char a =21495 ??现在等待....,我没分了,帮帮忙,多谢!!!
- datarow的格式
- 高手,一秒钟:JTextField如何限制长度输入,一定要编一个事件函数吗,谢谢
- 华为求输入字符串最后一个单词长度问题~~~~
- 一个关于main()函数的问题,在线等候,谢谢!!!!
- 新手 jdk如何设置环境变量?
import java.net.*;
import java.awt.*;
import java.awt.event.*;class Server extends Frame implements ActionListener{
Label label=new Label("交谈");
Panel panel=new Panel();
TextField tf=new TextField(10);
TextArea ta=new TextArea();
ServerSocket server;
Socket client;
InputStream in;
OutputStream out;public Server(){
super("服务器");
setSize(250,250);
panel.add(label);
panel.add(tf);
tf.addActionListener(this);
add("North",panel);
add("Center",ta);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
show();
try{
server=new ServerSocket(4000);
client=server.accept();
ta.append("已连接的客户机:"+client.getInetAddress().getHostName()+"\n\n");
in=client.getInputStream();
out=client.getOutputStream();
}
catch(IOException ioe) {}
while(true){
try{
byte[] buf=new byte[256];
in.read(buf);
String str=new String(buf);
ta.append("客户机说:"+str);
ta.append("\n");
}catch(IOException e){}
}
}
public void actionPerformed(ActionEvent e){
try{
String str=tf.getText();
byte[] buf=str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("我说:"+str);
ta.append("\n");
}catch(IOException ioe){}
}
public static void main(String[] args){
new Server();
}
}
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class AppletServer extends Applet {
boolean isStandalone = false;
Label label=new Label("交谈");
Panel panel=new Panel();
TextField tf=new TextField(10);
TextArea ta=new TextArea();
ServerSocket server;
Socket client;
InputStream in;
OutputStream out; //Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public AppletServer() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
setSize(250,250);
this.setLayout(new BorderLayout());
tf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
tf_actionPerformed(e);
}
});
panel.add(label);
panel.add(tf);
add("North",panel);
add("Center",ta); try{
server=new ServerSocket(4000);
client=server.accept();
ta.append("已连接的客户机:"+client.getInetAddress().getHostName()+"\n\n");
in=client.getInputStream();
out=client.getOutputStream();
}
catch(IOException ioe) {
}
while(true){
try{
byte[] buf=new byte[256];
in.read(buf);
String str=new String(buf);
ta.append("客户机说:"+str);
ta.append("\n");
}catch(IOException e){}
}
} //Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
} void tf_actionPerformed(ActionEvent e) {
try{
String str=tf.getText();
byte[] buf=str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("我说:"+str);
ta.append("\n");
}catch(IOException ioe){
}
} public static void main(String[] args){
javax.swing.JFrame frame = new javax.swing.JFrame("服务器");
AppletServer as = new AppletServer();
frame.setContentPane(as);
frame.setSize(as.getSize());
frame.pack();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
as.init();
}
}