⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frameappletsc.java

📁 最简单的FRAME 聊天空能 本地测试
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class FrameAppletSC extends Applet implements ActionListener{
	CFrame cf=new CFrame();
	
	ServerSocket ss;
	String strLine;
	Socket socket;
	BufferedReader in;
	PrintWriter out;
	
	Label lta=new Label("内容");
	TextArea ta=new TextArea(15,30);
	Label ltf=new Label("发送");
	TextField tf=new TextField(30);
	Button b1=new Button("确定");
	Button b2=new Button("帮助");
	public void init()  {
		this.setName("主机");
		this.setLayout(new FlowLayout());
		this.add(b2);
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cf.setVisible(true);
				try {
				ss=new ServerSocket(5000);
				socket=ss.accept();
				
				in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				out=new PrintWriter(socket.getOutputStream(),true);
				out.println("Hello");
				String strLine=null;
				
				strLine=in.readLine();
														
				ta.append("主机:"+"\n"+strLine+"\n");//System.out.println(strLine+"\n");					
				
				
				}
				catch(Exception e1) {
					e1.printStackTrace();
				}
					
			}
		});
		cf.setSize(300,400);
		cf.setTitle("主机");
		cf.setLayout(new FlowLayout());
	
		cf.add(lta);
		cf.add(ta);
		cf.add(ltf);
		
		cf.add(tf);
		cf.add(b1);
		
		b1.addActionListener(this);
		cf.addWindowListener(new WindowClose());	
		
			
	}
	

	
	public void actionPerformed(ActionEvent e) {
		strLine=tf.getText();
		out.println(strLine);
		ta.append("主机:"+"\n"+tf.getText()+"\n");
		
		
		try {
			
			strLine=in.readLine();
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
												
		ta.append("客人:"+"\n"+strLine+"\n");//System.out.println(strLine+"\n");
		
	}
	
	class CFrame extends Frame{
		
	}	
	
	class WindowClose extends WindowAdapter{
		public void windowClosing(WindowEvent e) {
			cf.setVisible(false);
			
		}
	}
}




----------------
import java.applet.Applet;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class FrameAppletC extends Applet implements ActionListener{
	CFrame cf=new CFrame();
	
	String strLine;
	Socket socket;
	BufferedReader in;
	PrintWriter out;
	
	Label lta=new Label("内容");
	TextArea ta=new TextArea(15,30);
	Label ltf=new Label("发送");
	TextField tf=new TextField(30);
	Button b1=new Button("确定");
	Button b2=new Button("帮助");
	public void init()  {
		this.setName("客人");
		this.setLayout(new FlowLayout());
		this.add(b2);
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cf.setVisible(true);
				try {
				
				socket=new Socket("127.0.0.1",6000);
				in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				out=new PrintWriter(socket.getOutputStream(),true);
				
				String strLine=null;
				
				strLine=in.readLine();
														
				ta.append("客户:"+"\n"+strLine+"\n");//System.out.println(strLine+"\n");					
				
				
				}
				catch(Exception e1) {
					e1.printStackTrace();
				}
					
			}
		});
		cf.setSize(300,400);
		cf.setTitle("客人");
		cf.setLayout(new FlowLayout());
	
		cf.add(lta);
		cf.add(ta);
		cf.add(ltf);
		
		cf.add(tf);
		cf.add(b1);
		
		b1.addActionListener(this);
		cf.addWindowListener(new WindowClose());	
		
			
	}
	

	
	public void actionPerformed(ActionEvent e) {
		strLine=tf.getText();
		out.println(strLine);
		ta.append("客人:"+"\n"+tf.getText()+"\n");
		
		
		try {
			
			strLine=in.readLine();
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
												
		ta.append("客户:"+"\n"+strLine+"\n");//System.out.println(strLine+"\n");
		
	}
	
	class CFrame extends Frame{
		
	}	
	
	class WindowClose extends WindowAdapter{
		public void windowClosing(WindowEvent e) {
			cf.setVisible(false);
			
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -