JPanel centerJP=new JPanel();
JTexArea msgContentJTA =new JTextArea(5,5);
centerJP.add(msgContentJTB);//这一句是没有用的,可以删掉
centerJP.add(new JScrollPane(msgContentJTB);//只用这句就可以了
我用这两句代码实现了滚动条的功能
但是这两句代码可以写成一句吗?可以教我怎么写吗?多谢冰封大哥!
你不是要退出论坛吗?难道舍不得我们吗?呵呵
这是有关于UDP通讯的例子
/*
* Receive.java
*
* Created on 2006年11月29日, 下午8:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package testUDP;
/**
*
* @author lbf
*/
import java.net.*;
import java.io.*;
public class Receive extends Thread{
private DatagramSocket ds;
private DatagramPacket dp;
/** Creates a new instance of Receive */
public Receive() {
initOther();
this.start();
}
private void initOther(){
try{
ds=new DatagramSocket(5000);
dp=new DatagramPacket(new byte[8192],8192);
}
catch(Exception exe){
exe.printStackTrace();
}
}
private void displayPacket(){
String s=new String(dp.getData(),0,dp.getLength());
String ip=dp.getAddress().getHostAddress();
int port=dp.getPort();
System.out.println(s+" from "+ip+":"+port);
}
public void run(){
System.out.println("start receive.....");
while(true){
try{
ds.receive(dp);
displayPacket();
}
catch(Exception exe){
exe.printStackTrace();
}
}
}
public static void main(String[] args) {
new Receive();
}
}
/*
* Send.java
*
* Created on 2006年11月29日, 下午8:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package testUDP;
/**
*
* @author lbf
*/
import java.io.*;
import java.net.*;
public class Send extends Thread{
private DatagramSocket ds;
private DatagramPacket dp;
/** Creates a new instance of Send */
public Send() {
initOther();
this.start();
}
private void initOther(){
try{
byte[] data="Hello World!".getBytes();
InetAddress to=InetAddress.getLocalHost();
ds=new DatagramSocket();
dp=new DatagramPacket(data,data.length,to,5000);
}
catch(Exception exe){
exe.printStackTrace();
}
}
private void sendPacket()throws Exception{
ds.send(dp);
System.out.println("send over");
}
public void run(){
System.out.println("start to send.....");
while(true){
try{
sendPacket();
Thread.sleep(1000);
}
catch(Exception exe){
exe.printStackTrace();
}
}
}
public static void main(String[] args) {
new Send();
}
}
============================================================================================
以下是关于TCP通讯的例子,你好好看吧
/*
* Receive.java
*
* Created on 2006年12月19日, 下午2:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package testTCP;
/**
*
* @author lbf
*/
import java.io.*;
import java.net.*;
public class Receive{
/** Creates a new instance of Receive */
public Receive() {
listen();
}
private void listen(){
try{
ServerSocket ss=new ServerSocket(10000);
while(true){
Socket s=ss.accept();
new HandleConnect(s).start();
}
}
catch(Exception exe){
exe.printStackTrace();
}
}
private class HandleConnect extends Thread{
private BufferedReader br;
public HandleConnect(Socket s){
try{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch(Exception exe ){
exe.printStackTrace();
}
}
public void run(){
while(true){
try{
System.out.println("收到的信息:"+br.readLine());
}
catch(Exception exe ){
exe.printStackTrace();
break;
}
}
}
}
public static void main(String[] args) {
new Receive();
}
}
/*
* Send.java
*
* Created on 2006年12月19日, 下午2:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package testTCP;
/**
*
* @author lbf
*/
import java.io.*;
import java.net.*;
public class Send extends Thread{
private PrintWriter pw;
/** Creates a new instance of Send */
public Send(String name) {
super(name);
initSocket();
this.start();
}
private void initSocket(){
try{
Socket s=new Socket("localhost",10000);
pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
}
catch(Exception exe ){
exe.printStackTrace();
}
}
public void run(){
while(true){
try{
Thread.sleep(1000);
pw.println(this.getName()+"这是通过TCP发过去的信息!");
pw.flush();
}catch(Exception exe){
exe.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
new Send("线程1");
}
}
都是先启动Receive,再启动Send