将Application转换为Applet,需要做哪几步工作?我的这个程序怎样改?(100分)

X

xuzy888

Unregistered / Unconfirmed
GUEST, unregistred user!
将Application转换为Applet,需要做哪几步工作?我的这个程序怎样改?
//Filename: udpsx.java
// Revision: 1.00
// Date Written: Mar 05 2000
// Last Revised:
// Program Description: GUI demo for SX PPP/UDP board
// Revision History:
//
//
// Operation.
// To set an IP address Highlight the IP field. type in the new address and
// press return.
// To set a port numbers Highlight the IP field. type in the new port and
// press return.
// To change a bit value click on the bit. A local change will toggel between
// yellow and blue. Remote verification will change the button between
// red ( 1) and green (0)
//
//*****************************************************************************************
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class udpsx extends Frame implements ActionListener
{
// Class wide ========

public udpsx ()
{
addWindowListener (new DWAdapter());

// Bit values
b0.addActionListener(this);
// button for bit 0
b1.addActionListener(this);
// button for bit 1
b2.addActionListener(this);
// button for bit 2
b3.addActionListener(this);
// button for bit 3
b4.addActionListener(this);
// button for bit 4
b5.addActionListener(this);
// button for bit 5
b6.addActionListener(this);
// button for bit 6
b7.addActionListener(this);
// button for bit 7
b8.addActionListener(this);
// button for bit 8

tf1 = new TextField ( Host,10);
tf2 = new TextField ( Port,2);
tf1.addActionListener(this);
tf2.addActionListener(this);
Label lb0 = new Label("IP");
Label lb1 = new Label("Port");
Panel pnl0 = new Panel();
Panel pnl1 = new Panel();

setTitle (" SX Ethernet UDP Demo ");
pnl0.setLayout ( new FlowLayout());
pnl1.setLayout ( new FlowLayout());

b0.setBackground(Color.gray);
b1.setBackground(Color.gray);
b2.setBackground(Color.gray);
b3.setBackground(Color.gray);
b4.setBackground(Color.gray);
b5.setBackground(Color.gray);
b6.setBackground(Color.gray);
b7.setBackground(Color.gray);
b7.setBackground(Color.white);
pnl1.add (b0);
pnl1.add (b1);
pnl1.add (b2);
pnl1.add (b3);
pnl1.add (b4);
pnl1.add (b5);
pnl1.add (b6);
pnl1.add (b7);
pnl1.add (b8);
pnl0.add (lb0);
pnl0.add (tf1);
pnl0.add (lb1);
pnl0.add (tf2);


add("South", pnl0);
add("North", pnl1);
ta0 = new TextArea ( 8,30);
add ( "Center", ta0);
// ---------------------- Create a Socket ------------------------
try
{
socket = new DatagramSocket( );
}
catch( SocketException se )
{
se.printStackTrace();
System.exit( 1 );
}


}
class DWAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
System.exit(0);

}
} // end DWAdapter


public void actionPerformed(ActionEvent evnt)
{
Object who = evnt.getSource();

if( who.equals(b0))
{
BitNum = 0;
btn_callBack(((Button) who));
}
else
if( who.equals(b1) )
{
BitNum = 1;
btn_callBack(((Button) who));
}
else
if( who.equals(b2) )
{
BitNum = 2;
btn_callBack(((Button) who));
}
else
if( who.equals(b3) )
{
BitNum = 3;
btn_callBack(((Button) who));
}
else
if( who.equals(b4) )
{
BitNum = 4;
btn_callBack(((Button) who));
}
else
if( who.equals(b5) )
{
BitNum = 5;
btn_callBack(((Button) who));
}
else
if( who.equals(b6))
{
BitNum = 6;
btn_callBack(((Button) who));
}
else
if( who.equals(b7))
{
BitNum = 7;
btn_callBack(((Button) who));
}
else
if( who.equals(b8))
{
OutValue = 0;
ta0.append( "/n Data set to " + OutValue);
OutData[0]= (byte) OutValue;
command_send();
}
else
if( who==tf1 || who==tf2)
{
textCallBack ( (TextField)who);
}

repaint ();
}

public void paint (Graphics g)
{
Font fnt = new Font("Dialog" , Font.BOLD, 14);
g.setColor (Color.yellow);
g.setFont (fnt);
g.drawString(" External Control ",75,250);
}

public static void main(String [] args)
{
udpsx f = new udpsx();
f.setBackground (Color.lightGray);
f.setSize (400, 300);
f.show();
f.waitForPackets();
}
//
//------------------------Data Area -------------------

private DatagramPacket sendPacket, receivePacket;
private DatagramSocket socket;

private String Host = "10.1.1.20";
private String Port = "1025";
private String Addr = "007";
private String Cmd = " ";
private BitSet PortC_Map = new BitSet(8);
private int BitNum = 0;
private byte OutData[] = new byte[2];
private Button b0 = new Button ("bit 0");
private Button b1 = new Button ("bit 1");
private Button b2 = new Button ("bit 2");
private Button b3 = new Button ("bit 3");
private Button b4 = new Button ("bit 4");
private Button b5 = new Button ("bit 5");
private Button b6 = new Button ("bit 6");
private Button b7 = new Button ("bit 7");
private Button b8 = new Button ("Refresh");
private TextArea ta0;
private TextField tf1;
private TextField tf2;
private static int OutValue = 0;
private static int InValue = 0;
//----------------------------------------------------------

public void waitForPackets()
{
while ( true ) {
try {
// set up packet
byte data[] = new byte[ 1 ];
receivePacket =
new DatagramPacket( data, data.length );
// wait for packet
socket.receive( receivePacket );

// process packet
ta0.append( "/nPacket received:" +
"/nFrom host: " + Host +
"/nHost port: " + receivePacket.getPort() +
"/nLength: " + receivePacket.getLength() +
"/nContaining:" );
InValue = (int) data[0];

// bit of a cheat here because the integer cast
// sign extends the value in InValue corrupting bit 7
if ( InValue < 0) InValue = InValue + 256;
ta0.append ( InValue + " /n");
set_bits();
}
catch( IOException exception ) {
ta0.append( exception.toString() + "/n" );
exception.printStackTrace();
}
}
}


private void textCallBack (TextField tf)
{
if (tf == tf1)
{
Host = tf1.getText();
ta0.append ("/n HOST set to --> "+Host);
}
if (tf == tf2)
{
Port = tf2.getText();
ta0.append ("/n Port set to --> "+Port);
}


}


private void command_send( )
{

try
{
ta0.append( "/nSending packet requesting " +
Cmd + "/n" );
byte data[] = OutData;

sendPacket = new DatagramPacket( data, data.length,
InetAddress.getByName(Host) ,Integer.parseInt( Port ));
socket.send( sendPacket );
ta0.append( "Packet sent/n" );
}
catch ( IOException exception )
{
ta0.append( exception.toString() + "/n" );
exception.printStackTrace();
}
}




private void btn_callBack( Button b )
{
OutValue = (int)(Math.pow(2,BitNum));
ta0.append( "/n Data set to " + OutValue);
OutData[0]= (byte) OutValue;
command_send();
}

private void set_bits()
{

if ( (InValue - 128) >= 0 )
{
b7.setBackground(Color.red);
InValue = InValue - 128;
}
else
b7.setBackground(Color.green);
if ( (InValue - 64) >= 0 )
{
b6.setBackground(Color.red);
InValue = InValue - 64;
}
else
b6.setBackground(Color.green);
if ( (InValue - 32) >= 0 )
{
b5.setBackground(Color.red);
InValue = InValue - 32;
}
else
b5.setBackground(Color.green);
if ( (InValue - 16) >= 0 )
{
b4.setBackground(Color.red);
InValue = InValue - 16;
}
else
b4.setBackground(Color.green);
if ( (InValue - 8) >= 0 )
{
b3.setBackground(Color.red);
InValue = InValue - 8;
}
else
b3.setBackground(Color.green);
if ( (InValue - 4) >= 0 )
{
b2.setBackground(Color.red);
InValue = InValue - 4;
}
else
b2.setBackground(Color.green);
if ( (InValue - 2) >= 0 )
{
b1.setBackground(Color.red);
InValue = InValue - 2;
}
else
b1.setBackground(Color.green);
if ( (InValue - 1) >= 0 )
{
b0.setBackground(Color.red);
InValue = InValue - 1;
}
else
b0.setBackground(Color.green);
}
} // end of sxudp.java
我将public class udpsx extends Frame implements ActionListener改为
public class udpsx extends Applet implements ActionListener,加入
import java.applet.Applet,改main为init,编译没通过。
 
这样改(在你已经改的基础之上):
1。把public udpsx()里的addWindowListener (new DWAdapter());
和setTitle (" SX Ethernet UDP Demo ");去掉。
2。在public udpsx()的最后加上
  setBackground (Color.lightGray);
  waitForPackets();
3。将main()全部去掉
4。将public udpsx()改为public void init()
 
1、要搞清楚二者执行过程的不同;
2、要分清哪些方法是用在Application中,哪些是用在Applet中;
3、在App->Applet的过程中,一定要注意本地IO操作,这些在App中是允许的,但在
Applet中是禁止的,不过如果对Applet做一个签名的话,条件是可以放宽的;
4、特别注意你的IO包。
 
eGuy,
我已照您的意思改过了,但什么控件也没显示。还有什么不对?
另外addWindowListener (new DWAdapter());是干什么用的?不要它会有什么功能损失?
//*****************************************************************************************
//
// Filename: udpsx.java
//
// Authors: irons
//
//
//
// Revision: 1.00
// Date Written: Mar 05 2000
//
// Last Revised:
//
// Program Description: GUI demo for SX PPP/UDP board
//
//
//
// Revision History:
//
//
// Operation.
// To set an IP address Highlight the IP field. type in the new address and
// press return.
// To set a port numbers Highlight the IP field. type in the new port and
// press return.
// To change a bit value click on the bit. A local change will toggel between
// yellow and blue. Remote verification will change the button between
// red ( 1) and green (0)
//
//*****************************************************************************************
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.applet.Applet;
public class udpsx extends Applet implements ActionListener
{
// Class wide ========

public void init()//public udpsx ()
{
//addWindowListener (new DWAdapter());

// Bit values
b0.addActionListener(this);
// button for bit 0
b1.addActionListener(this);
// button for bit 1
b2.addActionListener(this);
// button for bit 2
b3.addActionListener(this);
// button for bit 3
b4.addActionListener(this);
// button for bit 4
b5.addActionListener(this);
// button for bit 5
b6.addActionListener(this);
// button for bit 6
b7.addActionListener(this);
// button for bit 7
b8.addActionListener(this);
// button for bit 8

tf1 = new TextField ( Host,10);
tf2 = new TextField ( Port,2);
tf1.addActionListener(this);
tf2.addActionListener(this);
Label lb0 = new Label("IP");
Label lb1 = new Label("Port");
Panel pnl0 = new Panel();
Panel pnl1 = new Panel();

//setTitle (" SX Ethernet UDP Demo ");
pnl0.setLayout ( new FlowLayout());
pnl1.setLayout ( new FlowLayout());

b0.setBackground(Color.gray);
b1.setBackground(Color.gray);
b2.setBackground(Color.gray);
b3.setBackground(Color.gray);
b4.setBackground(Color.gray);
b5.setBackground(Color.gray);
b6.setBackground(Color.gray);
b7.setBackground(Color.gray);
b7.setBackground(Color.white);
pnl1.add (b0);
pnl1.add (b1);
pnl1.add (b2);
pnl1.add (b3);
pnl1.add (b4);
pnl1.add (b5);
pnl1.add (b6);
pnl1.add (b7);
pnl1.add (b8);
pnl0.add (lb0);
pnl0.add (tf1);
pnl0.add (lb1);
pnl0.add (tf2);


add("South", pnl0);
add("North", pnl1);
ta0 = new TextArea ( 8,30);
add ( "Center", ta0);
// ---------------------- Create a Socket ------------------------
try
{
socket = new DatagramSocket( );
}
catch( SocketException se )
{
se.printStackTrace();
System.exit( 1 );
}
setBackground (Color.lightGray);
waitForPackets();

}
class DWAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
System.exit(0);

}
} // end DWAdapter


public void actionPerformed(ActionEvent evnt)
{
Object who = evnt.getSource();

if( who.equals(b0))
{
BitNum = 0;
btn_callBack(((Button) who));
}
else
if( who.equals(b1) )
{
BitNum = 1;
btn_callBack(((Button) who));
}
else
if( who.equals(b2) )
{
BitNum = 2;
btn_callBack(((Button) who));
}
else
if( who.equals(b3) )
{
BitNum = 3;
btn_callBack(((Button) who));
}
else
if( who.equals(b4) )
{
BitNum = 4;
btn_callBack(((Button) who));
}
else
if( who.equals(b5) )
{
BitNum = 5;
btn_callBack(((Button) who));
}
else
if( who.equals(b6))
{
BitNum = 6;
btn_callBack(((Button) who));
}
else
if( who.equals(b7))
{
BitNum = 7;
btn_callBack(((Button) who));
}
else
if( who.equals(b8))
{
OutValue = 0;
ta0.append( "/n Data set to " + OutValue);
OutData[0]= (byte) OutValue;
command_send();
}
else
if( who==tf1 || who==tf2)
{
textCallBack ( (TextField)who);
}

repaint ();
}

public void paint (Graphics g)
{
Font fnt = new Font("Dialog" , Font.BOLD, 14);
g.setColor (Color.yellow);
g.setFont (fnt);
g.drawString(" External Control ",75,250);
}

// public static void main(String [] args)
// {
// udpsx f = new udpsx();
// f.setBackground (Color.lightGray);
// f.setSize (400, 300);
// f.show();
// f.waitForPackets();
// }
//
//------------------------Data Area -------------------

private DatagramPacket sendPacket, receivePacket;
private DatagramSocket socket;

private String Host = "10.1.1.20";
private String Port = "1025";
private String Addr = "007";
private String Cmd = " ";
private BitSet PortC_Map = new BitSet(8);
private int BitNum = 0;
private byte OutData[] = new byte[2];
private Button b0 = new Button ("bit 0");
private Button b1 = new Button ("bit 1");
private Button b2 = new Button ("bit 2");
private Button b3 = new Button ("bit 3");
private Button b4 = new Button ("bit 4");
private Button b5 = new Button ("bit 5");
private Button b6 = new Button ("bit 6");
private Button b7 = new Button ("bit 7");
private Button b8 = new Button ("Refresh");
private TextArea ta0;
private TextField tf1;
private TextField tf2;
private static int OutValue = 0;
private static int InValue = 0;
//----------------------------------------------------------

public void waitForPackets()
{
while ( true ) {
try {
// set up packet
byte data[] = new byte[ 1 ];
receivePacket =
new DatagramPacket( data, data.length );
// wait for packet
socket.receive( receivePacket );

// process packet
ta0.append( "/nPacket received:" +
"/nFrom host: " + Host +
"/nHost port: " + receivePacket.getPort() +
"/nLength: " + receivePacket.getLength() +
"/nContaining:" );
InValue = (int) data[0];

// bit of a cheat here because the integer cast
// sign extends the value in InValue corrupting bit 7
if ( InValue < 0) InValue = InValue + 256;
ta0.append ( InValue + " /n");
set_bits();
}
catch( IOException exception ) {
ta0.append( exception.toString() + "/n" );
exception.printStackTrace();
}
}
}


private void textCallBack (TextField tf)
{
if (tf == tf1)
{
Host = tf1.getText();
ta0.append ("/n HOST set to --> "+Host);
}
if (tf == tf2)
{
Port = tf2.getText();
ta0.append ("/n Port set to --> "+Port);
}


}


private void command_send( )
{

try
{
ta0.append( "/nSending packet requesting " +
Cmd + "/n" );
byte data[] = OutData;

sendPacket = new DatagramPacket( data, data.length,
InetAddress.getByName(Host) ,Integer.parseInt( Port ));
socket.send( sendPacket );
ta0.append( "Packet sent/n" );
}
catch ( IOException exception )
{
ta0.append( exception.toString() + "/n" );
exception.printStackTrace();
}
}




private void btn_callBack( Button b )
{
OutValue = (int)(Math.pow(2,BitNum));
ta0.append( "/n Data set to " + OutValue);
OutData[0]= (byte) OutValue;
command_send();
}

private void set_bits()
{

if ( (InValue - 128) >= 0 )
{
b7.setBackground(Color.red);
InValue = InValue - 128;
}
else
b7.setBackground(Color.green);
if ( (InValue - 64) >= 0 )
{
b6.setBackground(Color.red);
InValue = InValue - 64;
}
else
b6.setBackground(Color.green);
if ( (InValue - 32) >= 0 )
{
b5.setBackground(Color.red);
InValue = InValue - 32;
}
else
b5.setBackground(Color.green);
if ( (InValue - 16) >= 0 )
{
b4.setBackground(Color.red);
InValue = InValue - 16;
}
else
b4.setBackground(Color.green);
if ( (InValue - 8) >= 0 )
{
b3.setBackground(Color.red);
InValue = InValue - 8;
}
else
b3.setBackground(Color.green);
if ( (InValue - 4) >= 0 )
{
b2.setBackground(Color.red);
InValue = InValue - 4;
}
else
b2.setBackground(Color.green);
if ( (InValue - 2) >= 0 )
{
b1.setBackground(Color.red);
InValue = InValue - 2;
}
else
b1.setBackground(Color.green);
if ( (InValue - 1) >= 0 )
{
b0.setBackground(Color.red);
InValue = InValue - 1;
}
else
b0.setBackground(Color.green);
}
} // end of sxudp.java
 
什么控件也没显示是因为这一句:waitForPackets();
因为安全限制,浏览器不允许Applet作TCP/IP服务器,
如果去掉waitForPackets();控件就应该可以显示了。但是Applet
的功能就受到限制了。
此外,Applet只能作为提供它的服务器的TCP/IP客户。即这个Applet
的功能同样因此面受到了限制。
只有用签名才能解决这些问题。
现在的问题是你的这个Applet是用来做什么的。
是玩玩或想在Internet上给别人用?

 
接受答案了.
 
顶部