我想写个applet程序 把原来在文本区显示的数据用JTable的表格形式显示出来,为N行5列的 ,请大家给我改一下。谢谢了!!! 急!!!
这是程序代码:
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;public class disp extends Applet implements ActionListener,Runnable
{
private TextField m_TextFiledIP;
private TextField m_TextFiledPort;
private Button m_BtnConnect;
private TextArea m_TextAreaSend;
private Button m_BtnSend;
private TextArea m_TextAreaRec; Socket m_conSocket;
DataOutputStream m_out;
DataInputStream m_in;
int m_iConnect=0;
Thread m_ThreadRec;
protected Button makebutton(String name, GridBagLayout gridbag,GridBagConstraints c)
{
Button btn = new Button(name);
gridbag.setConstraints(btn, c);
add(btn);
return btn;
}
protected void makelabel(String name, GridBagLayout gridbag,GridBagConstraints c)
{
Label label= new Label(name,Label.LEFT);
gridbag.setConstraints(label, c);
add(label);
}
protected TextField makeTextField(String name,int len, GridBagLayout gridbag,GridBagConstraints c)
{
TextField field;
field= new TextField(name,len);
gridbag.setConstraints(field, c);
add(field);
return field;
}
protected TextArea makeTextArea(String name,int lenx,int leny, GridBagLayout gridbag,GridBagConstraints c)
{
TextArea field;
field= new TextArea(name,lenx,leny,TextArea.SCROLLBARS_VERTICAL_ONLY);
gridbag.setConstraints(field, c);
add(field);
return field;
}
private String GetHostIP()
{
return getCodeBase().getHost();
/* String s1 = "";
if(s.length() < 15)
return s;
for(int i = 0; i < 16; i += 4)
s1 = s1 + String.valueOf(Long.valueOf(s.substring(i, i + 3), 16)) + '.'; return s1.substring(0, s1.length() - 1);
*/
}
public void init()
{
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridbag);
c.anchor=GridBagConstraints.NORTHWEST;
c.insets=new Insets(1,1,1,1);
c.gridwidth=1;
makelabel("设备地址:",gridbag, c);
m_TextFiledIP=makeTextField(GetHostIP(),12,gridbag,c);
m_TextFiledIP.enable(false);
makelabel("端口:",gridbag, c);
m_TextFiledPort=makeTextField("4001",5,gridbag,c);
c.gridwidth = GridBagConstraints.REMAINDER; //本行结束
/*m_BtnConnect=makebutton("连接",gridbag,c);
m_BtnConnect.addActionListener(this);
m_BtnConnect.setActionCommand("Connect");*/
makelabel(" ",gridbag, c);
makelabel("接收数据区:",gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //本行结束
m_TextAreaRec=makeTextArea("",5,50,gridbag,c);
makelabel(" ",gridbag, c);
c.gridwidth=1;
makelabel("发送数据区:",gridbag, c);
//c.gridwidth = GridBagConstraints.REMAINDER; //本行结束
m_BtnSend=makebutton("应用",gridbag,c);
m_BtnSend.addActionListener(this);
m_BtnSend.setActionCommand("Send");
c.gridwidth = GridBagConstraints.REMAINDER;
m_BtnSend=makebutton("清空",gridbag,c);
m_BtnSend.addActionListener(this);
m_BtnSend.setActionCommand("Refresh");
c.gridwidth = GridBagConstraints.REMAINDER;
m_TextAreaSend=makeTextArea("",1,50,gridbag,c);
m_conSocket=null;
m_out=null;
m_in=null;
m_ThreadRec=new Thread(this,"RecThread");
m_ThreadRec.start();
TCPInit();//Auto connect to ZNET-200T
m_iConnect=1;//Auto connect to ZNET-200T
resize(500,400);
}
public void destroy()
{
m_ThreadRec.stop();
m_ThreadRec = null;
}
/*public byte[] ConvertStrtoHex(String str,int len)
{
byte bufarr[]=new byte[len];
for(int i=0;i<len;i++)
{
bufarr[i]=Byte.parseByte(str.substring(i*3,i*3+2),16);
}
return bufarr;
}*/
public void actionPerformed(ActionEvent e)
{
/*if(e.getActionCommand()=="Connect")
{
if(m_iConnect==0)
{
if(TCPInit()==1)
{
m_BtnConnect.setLabel("断开");
m_iConnect=1; }
}
else
{
TCPClose();
m_BtnConnect.setLabel("连接");
m_iConnect=0;
}
}
else*/
if(e.getActionCommand()=="Send"/*&&m_iConnect==1*/)
{String str=m_TextAreaSend.getText();
int len=str.length();
byte[] bufarr=str.getBytes();
try
{
m_out.write(bufarr,0,len);
}
catch(IOException ioexception)
{
}
}
else if(e.getActionCommand()=="Refresh")//清空
{
m_TextAreaSend.setText("");//发送区
//m_TextAreaRec.setText("");//接受区
}
} public void TCPClose()
{
try
{
m_conSocket.close();
m_out.close();
m_in.close();
}
catch(IOException ioexception)
{
}
}
public int TCPInit()
{
try
{
String strip=m_TextFiledIP.getText();
int port=(int)Integer.parseInt(m_TextFiledPort.getText());
m_conSocket = new Socket(strip,port);
m_conSocket.setSoTimeout(0);
m_out = new DataOutputStream(m_conSocket.getOutputStream());
m_in = new DataInputStream(m_conSocket.getInputStream());
}
catch(UnknownHostException unknownhostexception)
{
System.err.println("Don't know about host: taranis.");
return 0;
}
catch(IOException ioexception)
{
System.err.println("Couldn't get I/O for the connection to: taranis.");
return 0;
}
return 1;
} public void PutBytetoTextField(byte[] bufarr,int len)//输出
{String str=new String(bufarr);//十六进制转字符
m_TextAreaRec.appendText(str);
/*String str="";
String tmp;
for(int i=0;i<len;i++)
{ tmp=Integer.toASCIIString((int)bufarr[i]);
if (tmp.length() == 1)
tmp = "0" + tmp;
str+=tmp+"";}*/
}
void Delay(int i)
{
try
{
Thread.sleep(i);
}
catch(Exception exception) { }
}
public void run()
{
String curRunning=Thread.currentThread().getName();
if(curRunning.equals("RecThread"))
{
while(true)
{
if(m_iConnect==1)
{
Delay(1000);
//m_TextAreaRec.setText("");//接受区清空
byte recbuf[]=new byte[1020];
int len;
try
{
len = m_in.read(recbuf,0,1020);//输出到网页窗口
byte temp[]=new byte[1020];
temp[0]=recbuf[1];
temp[1]=recbuf[2];
temp[2]=recbuf[3];
temp[3]=0x20;
temp[4]=0x20;
temp[5]=recbuf[4];
temp[6]=recbuf[5];
temp[7]=recbuf[6];
temp[8]=0x20;
temp[9]=0x20;
temp[10]=recbuf[7];
temp[11]=recbuf[8];
temp[12]=recbuf[9];
temp[13]=0x20;
temp[14]=0x20;
if(temp[2]=='A'&temp[7]=='A'&temp[12]=='A')
{
PutBytetoTextField(temp,15);
}
//PutBytetoTextField(recbuf,len);
}
catch(IOException ioexception)
{
Delay(1000);
}
}
else
{
Delay(1000);
}
}
}
}
public void paint(Graphics g)
{
//g.drawString("Welcome to Java!!", 50, 60 );
}
}
这是程序代码:
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;public class disp extends Applet implements ActionListener,Runnable
{
private TextField m_TextFiledIP;
private TextField m_TextFiledPort;
private Button m_BtnConnect;
private TextArea m_TextAreaSend;
private Button m_BtnSend;
private TextArea m_TextAreaRec; Socket m_conSocket;
DataOutputStream m_out;
DataInputStream m_in;
int m_iConnect=0;
Thread m_ThreadRec;
protected Button makebutton(String name, GridBagLayout gridbag,GridBagConstraints c)
{
Button btn = new Button(name);
gridbag.setConstraints(btn, c);
add(btn);
return btn;
}
protected void makelabel(String name, GridBagLayout gridbag,GridBagConstraints c)
{
Label label= new Label(name,Label.LEFT);
gridbag.setConstraints(label, c);
add(label);
}
protected TextField makeTextField(String name,int len, GridBagLayout gridbag,GridBagConstraints c)
{
TextField field;
field= new TextField(name,len);
gridbag.setConstraints(field, c);
add(field);
return field;
}
protected TextArea makeTextArea(String name,int lenx,int leny, GridBagLayout gridbag,GridBagConstraints c)
{
TextArea field;
field= new TextArea(name,lenx,leny,TextArea.SCROLLBARS_VERTICAL_ONLY);
gridbag.setConstraints(field, c);
add(field);
return field;
}
private String GetHostIP()
{
return getCodeBase().getHost();
/* String s1 = "";
if(s.length() < 15)
return s;
for(int i = 0; i < 16; i += 4)
s1 = s1 + String.valueOf(Long.valueOf(s.substring(i, i + 3), 16)) + '.'; return s1.substring(0, s1.length() - 1);
*/
}
public void init()
{
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridbag);
c.anchor=GridBagConstraints.NORTHWEST;
c.insets=new Insets(1,1,1,1);
c.gridwidth=1;
makelabel("设备地址:",gridbag, c);
m_TextFiledIP=makeTextField(GetHostIP(),12,gridbag,c);
m_TextFiledIP.enable(false);
makelabel("端口:",gridbag, c);
m_TextFiledPort=makeTextField("4001",5,gridbag,c);
c.gridwidth = GridBagConstraints.REMAINDER; //本行结束
/*m_BtnConnect=makebutton("连接",gridbag,c);
m_BtnConnect.addActionListener(this);
m_BtnConnect.setActionCommand("Connect");*/
makelabel(" ",gridbag, c);
makelabel("接收数据区:",gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //本行结束
m_TextAreaRec=makeTextArea("",5,50,gridbag,c);
makelabel(" ",gridbag, c);
c.gridwidth=1;
makelabel("发送数据区:",gridbag, c);
//c.gridwidth = GridBagConstraints.REMAINDER; //本行结束
m_BtnSend=makebutton("应用",gridbag,c);
m_BtnSend.addActionListener(this);
m_BtnSend.setActionCommand("Send");
c.gridwidth = GridBagConstraints.REMAINDER;
m_BtnSend=makebutton("清空",gridbag,c);
m_BtnSend.addActionListener(this);
m_BtnSend.setActionCommand("Refresh");
c.gridwidth = GridBagConstraints.REMAINDER;
m_TextAreaSend=makeTextArea("",1,50,gridbag,c);
m_conSocket=null;
m_out=null;
m_in=null;
m_ThreadRec=new Thread(this,"RecThread");
m_ThreadRec.start();
TCPInit();//Auto connect to ZNET-200T
m_iConnect=1;//Auto connect to ZNET-200T
resize(500,400);
}
public void destroy()
{
m_ThreadRec.stop();
m_ThreadRec = null;
}
/*public byte[] ConvertStrtoHex(String str,int len)
{
byte bufarr[]=new byte[len];
for(int i=0;i<len;i++)
{
bufarr[i]=Byte.parseByte(str.substring(i*3,i*3+2),16);
}
return bufarr;
}*/
public void actionPerformed(ActionEvent e)
{
/*if(e.getActionCommand()=="Connect")
{
if(m_iConnect==0)
{
if(TCPInit()==1)
{
m_BtnConnect.setLabel("断开");
m_iConnect=1; }
}
else
{
TCPClose();
m_BtnConnect.setLabel("连接");
m_iConnect=0;
}
}
else*/
if(e.getActionCommand()=="Send"/*&&m_iConnect==1*/)
{String str=m_TextAreaSend.getText();
int len=str.length();
byte[] bufarr=str.getBytes();
try
{
m_out.write(bufarr,0,len);
}
catch(IOException ioexception)
{
}
}
else if(e.getActionCommand()=="Refresh")//清空
{
m_TextAreaSend.setText("");//发送区
//m_TextAreaRec.setText("");//接受区
}
} public void TCPClose()
{
try
{
m_conSocket.close();
m_out.close();
m_in.close();
}
catch(IOException ioexception)
{
}
}
public int TCPInit()
{
try
{
String strip=m_TextFiledIP.getText();
int port=(int)Integer.parseInt(m_TextFiledPort.getText());
m_conSocket = new Socket(strip,port);
m_conSocket.setSoTimeout(0);
m_out = new DataOutputStream(m_conSocket.getOutputStream());
m_in = new DataInputStream(m_conSocket.getInputStream());
}
catch(UnknownHostException unknownhostexception)
{
System.err.println("Don't know about host: taranis.");
return 0;
}
catch(IOException ioexception)
{
System.err.println("Couldn't get I/O for the connection to: taranis.");
return 0;
}
return 1;
} public void PutBytetoTextField(byte[] bufarr,int len)//输出
{String str=new String(bufarr);//十六进制转字符
m_TextAreaRec.appendText(str);
/*String str="";
String tmp;
for(int i=0;i<len;i++)
{ tmp=Integer.toASCIIString((int)bufarr[i]);
if (tmp.length() == 1)
tmp = "0" + tmp;
str+=tmp+"";}*/
}
void Delay(int i)
{
try
{
Thread.sleep(i);
}
catch(Exception exception) { }
}
public void run()
{
String curRunning=Thread.currentThread().getName();
if(curRunning.equals("RecThread"))
{
while(true)
{
if(m_iConnect==1)
{
Delay(1000);
//m_TextAreaRec.setText("");//接受区清空
byte recbuf[]=new byte[1020];
int len;
try
{
len = m_in.read(recbuf,0,1020);//输出到网页窗口
byte temp[]=new byte[1020];
temp[0]=recbuf[1];
temp[1]=recbuf[2];
temp[2]=recbuf[3];
temp[3]=0x20;
temp[4]=0x20;
temp[5]=recbuf[4];
temp[6]=recbuf[5];
temp[7]=recbuf[6];
temp[8]=0x20;
temp[9]=0x20;
temp[10]=recbuf[7];
temp[11]=recbuf[8];
temp[12]=recbuf[9];
temp[13]=0x20;
temp[14]=0x20;
if(temp[2]=='A'&temp[7]=='A'&temp[12]=='A')
{
PutBytetoTextField(temp,15);
}
//PutBytetoTextField(recbuf,len);
}
catch(IOException ioexception)
{
Delay(1000);
}
}
else
{
Delay(1000);
}
}
}
}
public void paint(Graphics g)
{
//g.drawString("Welcome to Java!!", 50, 60 );
}
}
你哪里不会?
加个JTable进去?
最近工程很忙,没时间给你写出来了,我的Blog中有一个JTabel的例子,你可以参考下。
参考例子
我昏,大家都有事做。你不如自己做一下把不会的写出来还好点.自己动手丰衣足食
” 我觉得他纯粹是没是找骂型,我要是会,我还来这里问个啥,我标题都写:“急”了,他还来惹我的火,我能不着急吗?
再说了 ,我相信论坛里有不少高手是乐于助人的,而且不是为了多少分来帮人的,只有极个别变态的人,还会看重这样没有实际用处的、虚无缥缈的东西!
我只是发表我的想法,希望有热心的朋友给指教,那些没事瞎逛的,就不要再发些无关紧要的xx了!!!