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

📄 test2.java

📁 用JAVA编网页编程示例
💻 JAVA
字号:
/*
	A basic extension of the java.applet.Applet class
 */

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.lang.*;

public class test2 extends Applet
{
    String email_address;
    String text;
    String msg;
	String txtmsg;
    String max_time;
    String Satz;   
    final int GETC = 0xf6;
    boolean rc;
    byte [] data = new byte[130];
    public tcpip tp; 
   
        
	public void init()
	{
		
		//Build the frames 
		Font font;
		font = new Font("Dialog",Font.BOLD,16);
		
		setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();   // C005
		c.insets =  new Insets(5,5,5,5);
		setBackground(java.awt.Color.lightGray);
		setSize(561,380);
		
		label2.setText("显示窗口:");
		c.gridx = 0; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1; 
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.WEST;
		c.fill = GridBagConstraints.NONE;
		add(label2,c);
		label2.setFont(new Font("Dialog", Font.BOLD, 16));
		label2.setBounds(5,80,150,23);
		
		label3.setText("通信协议:");
		c.gridx = 0; c.gridy = 3; c.gridwidth = 1; c.gridheight = 1; 
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.WEST;
		c.fill = GridBagConstraints.NONE;
		add(label3,c);
		label3.setFont(new Font("Dialog", Font.BOLD, 16));
		label3.setBounds(5,96,150,23);
		
		label4.setText("数采站址:");
		c.gridx = 0; c.gridy = 4; c.gridwidth = 1; c.gridheight = 1; 
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.WEST;
		c.fill = GridBagConstraints.NONE;
		add(label4,c);
		label4.setFont(new Font("Dialog", Font.BOLD, 16));
		label4.setBounds(5,112,150,23);
		
		label5.setText("数采器型号:");
		c.gridx = 0; c.gridy = 5; c.gridwidth = 1; c.gridheight = 1; 
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.WEST;
		c.fill = GridBagConstraints.NONE;
		add(label5,c);
		label5.setFont(new Font("Dialog", Font.BOLD, 16));
		label5.setBounds(5,128,150,23);
		
		//text_box
		c.gridx = 1; c.gridy = 2; c.gridwidth = 3; c.gridheight = 1; 
		c.weightx = 0.5; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(text_box,c);
		text_box.setFont(font);
		//text_box.setBounds(115,40,390,23);
		
		//通信协议
		c.gridx = 1; c.gridy = 3; c.gridwidth = 3; c.gridheight = 1; 
		c.weightx = 0.5; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(address_box,c);
		address_box.setFont(font);
		address_box.setEditable(false);
		address_box.setBounds(115,96,390,23);
		
		//数采站址
		c.gridx = 1; c.gridy = 4; c.gridwidth = 3; c.gridheight = 1; 
		c.weightx = 0.5; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(version_box,c);
		version_box.setFont(font);
		version_box.setEditable(false);
		version_box.setBounds(115,112,390,23);
		
		//数采器型号
		c.gridx = 1; c.gridy = 5; c.gridwidth = 3; c.gridheight = 1; 
		c.weightx = 0.5; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(device_box,c);
		device_box.setFont(font);
		device_box.setEditable(false);
		device_box.setBounds(115,128,390,23);
		
		BT_Clear.setLabel("清空窗口");
		c.gridx = 0; c.gridy = 7; c.gridwidth = 1; c.gridheight = 1; 
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(BT_Clear,c);
		BT_Clear.setBackground(java.awt.Color.lightGray);
		BT_Clear.setFont(new Font("Dialog", Font.BOLD, 20));
		BT_Clear.setBounds(5,262,194,33);
		
		BT_send.setLabel("发送命令");
		c.gridx = 3; c.gridy = 7; c.gridwidth = 1; c.gridheight = 1; 
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(BT_send,c);
		BT_send.setBackground(java.awt.Color.lightGray);
		BT_send.setFont(new Font("Dialog", Font.BOLD, 20));
		BT_send.setBounds(361,262,194,33);
			
		BT_Cancel.setLabel("返回");
		c.gridx = 0; c.gridy = 8; c.gridwidth = 1; c.gridheight = 2; 
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(BT_Cancel,c);
		BT_Cancel.setBackground(java.awt.Color.lightGray);
		BT_Cancel.setFont(new Font("Dialog", Font.BOLD, 20));
		BT_Cancel.setBounds(5,300,100,33);
		
		BT_Conf.setLabel("读取设置信息");
		c.gridx = 3; c.gridy = 8; c.gridwidth = 1; c.gridheight = 1; 
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(BT_Conf,c);
		BT_Conf.setBackground(java.awt.Color.lightGray);
		BT_Conf.setFont(new Font("Dialog", Font.BOLD, 20));
		BT_Conf.setBounds(361,300,194,33);
		
		BT_serial.setLabel("读取数据");
		c.gridx = 3; c.gridy = 9; c.gridwidth = 1; c.gridheight = 1; 
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(BT_serial,c);
		BT_serial.setBackground(java.awt.Color.lightGray);
		BT_serial.setFont(new Font("Dialog", Font.BOLD, 20));
		BT_serial.setBounds(361,330,194,33);
	
		//send_text();
		SymAction lSymAction = new SymAction();
		BT_Cancel.addActionListener(lSymAction);
		BT_send.addActionListener(lSymAction);
		BT_Conf.addActionListener(lSymAction);
		BT_serial.addActionListener(lSymAction);
		BT_Clear.addActionListener(lSymAction);
		address_box.setText("Modbus/RTU");
		version_box.setText ("002");
		device_box.setText ("DAU20BX");
		//text_box.
	
	
	
	
	
	
	}
	
	
	java.awt.Label label2 = new java.awt.Label();
	java.awt.Label label3 = new java.awt.Label();
	java.awt.Label label4 = new java.awt.Label();
	java.awt.Label label5 = new java.awt.Label();
	java.awt.TextArea text_box = new java.awt.TextArea();
	java.awt.TextField address_box = new java.awt.TextField();
	java.awt.TextField version_box = new java.awt.TextField();
	java.awt.TextField device_box = new java.awt.TextField();
	
	java.awt.Button BT_send = new java.awt.Button();
	java.awt.Button BT_Cancel = new java.awt.Button();
	java.awt.Button BT_Conf = new java.awt.Button();
	java.awt.Button BT_serial = new java.awt.Button();
	java.awt.Button BT_Clear = new java.awt.Button();
	
	
	// Configuration sends a Command to the DN the request of this cmd is the firmware information 
	// the open Port is 77fe  
	public void Configuration()
	{
     	    BT_disable();
	    byte [] data = new byte[130];
            int i;
            InetAddress reader_ip = null;
	    tcpip tp = null;
	    format f = null;
	    
		try{
		 reader_ip = InetAddress.getByName(getCodeBase().getHost());
		}
		catch (UnknownHostException e){}
		
		try{
		    if (reader_ip != null)
    	        	tp = new tcpip(new Socket(reader_ip, 0x77fe));
    	    	   else
    	    	   	System.out.println("Socket always open");
    	   	}
    		catch (IOException e){}
	
		data[0] = 0x00;
		data[1] = 0x00;
		data[2] = 0x00;
		data[3] = (byte) GETC;		// GETC Command to get the Configuration from the DN
    		tp.send(data, false);
	    
	    	try{Thread.sleep(1000);}catch(Exception e){}
	 	data = tp.receive(true);	// receive the answer from the DN
		tp = null;
		msg = "";
		for(i = 24; i < 30; i++){ 			// MAC address
			msg += f.charToHex((char)data[i])+" ";
			address_box.setText(msg);
		} 
		msg = "";					// SW Version
		msg = f.charToHex((char)data[7])+".";
		msg += f.charToHex((char)data[6]);
	 	version_box.setText(msg);
	
		msg = "";
		int devicenumber = data[8]-0x30;		// Device Type
		msg = ""+devicenumber+""+(char)data[9];
		device_box.setText(msg);
	}	


// serial_com waits for a char and send the char to the Text Window. The abort after 200ms
	// the open Port is 502 default Modbus/TCP port  
	public void serial_com()
	{
    	    BT_disable();
	    byte [] data = new byte[130];
            int i;
            InetAddress reader_ip = null;
	    tcpip tp = null;
	    
		format f = null;
		String msg2;
	    
		try{
		 reader_ip = InetAddress.getByName(getCodeBase().getHost());
		}
		catch (UnknownHostException e){}
		
		try{
			if (reader_ip != null) {
    	        	tp = new tcpip(new Socket(reader_ip, 502)); //0x2711));
                     			
			}
    	    	   else
   	    	   	System.out.println("Socket always open");
   	   	}
    		catch (IOException e){}
		
		data[0] = 0x00;
		data[1] = 0x00;
		data[2] = 0x00;
		data[3] = 0x00;	
    	data[4] = 0x00;
		data[5] = 0x06;
		data[6] = 0x01;
		data[7] = 0x04;
		data[8] = 0x00;
		data[9] = 0x00;
		data[10] = 0x00;
		data[11] =0x28;
		tp.send(data, false);
	    
	    	try{Thread.sleep(500);}catch(Exception e){}
			
	 	data = tp.receive(true);	// receive the answer from the DN
		tp = null;
				msg = "";
		for(i = 0; i<data.length ; i++){ 			
			msg += f.charToHex((char)data[i])+" ";

		}
	 msg2="\n";
	 
		for(i = 0; i<data.length ; i++){ 			
			msg2 +=(char)data[i];
           
		}	
	text_box.setText(msg+msg2);
		msg = "";					
	}	
	
	// send_text send the Text in the Text Window.
	
	public void send_text()
	{
  	    InetAddress reader_ip = null;
	    tcpip tp = null;
		int i;
	    
		try{
		 reader_ip = InetAddress.getByName(getCodeBase().getHost());
		}
		catch (UnknownHostException e){}
		
		try{
		    if (reader_ip != null)
    	        	tp = new tcpip(new Socket(reader_ip, 502));
    	    	   else
    	    	   	System.out.println("Socket always open");
    	    	}
    		catch (IOException e){}
	
		text = setbox();
		int len = text.length();
		if( len > 1){
			tp.send(text, false);
		   	BT_disable();
	   	}
		else
			System.out.println("no text");
	
		try{Thread.sleep(200);}catch(Exception e){}
		data = tp.receive(true);	// receive the answer from the DN
		tp = null;
		msg = "";			
		for(i = 0; i<data.length ; i++){ 			
			msg +=(char)data[i];
           
		}
		text_box.setText( msg );
			
		tp = null;
	}	

	// send_text send the Text in the Text Window.
	
	public void send_text1()
	{
  	    InetAddress reader_ip = null;
	    tcpip tp = null;
	    
		try{
		 reader_ip = InetAddress.getByName(getCodeBase().getHost());
		}
		catch (UnknownHostException e){}
		
		try{
		    if (reader_ip != null)
    	        	tp = new tcpip(new Socket(reader_ip,502));
    	    	   else
    	    	   	System.out.println("Socket always open");
    	    	}
    		catch (IOException e){}
	
		text = setbox();
		int len = text.length();
		if( len > 1){
			tp.send(text, true);
		   	BT_disable();
	   	}
		else
			System.out.println("no text");
		tp = null;
	}	
	
	
	// setbox reads the text in the Text Window and send the result back.  
	public String setbox()
	{
		String message;	
		message = text_box.getText()+(char)(13);
		int len = message.length();
		return(message+'\0');
	}


	// SymAction decide which Button is activ
	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == BT_Cancel)
				BTCancel_ActionPerformed(event);
			else if (object == BT_send)
				BTOK_ActionPerformed(event);
			else if (object == BT_Conf)
				BTConf_ActionPerformed(event);
			else if (object == BT_serial)	
				BTserial_ActionPerformed(event);
			else if (object == BT_Clear)	
				BTClear_ActionPerformed(event);	
		}
	}


	void BTCancel_ActionPerformed(java.awt.event.ActionEvent event)
	{
	    URL page;
	    try {
            	page = new URL(getCodeBase(), "example.html");
            	getAppletContext().showDocument(page , "_self");
            } 
            catch (MalformedURLException e) {
            	System.out.println("Page load failed");
            }
	}
	
	

	void BTOK_ActionPerformed(java.awt.event.ActionEvent event)
	{
           send_text();
 	   BT_enable();           
	}

	void BTConf_ActionPerformed(java.awt.event.ActionEvent event)
	{
           Configuration();
           BT_enable();
	}

	void BTserial_ActionPerformed(java.awt.event.ActionEvent event)
	{
           serial_com();
           BT_enable();
	}

	void BTClear_ActionPerformed(java.awt.event.ActionEvent event)
	{
           text_box.setText("");
       	}

	void BT_enable()
	{
	   BT_send.enable();
	   BT_Cancel.enable();
	   BT_Clear.enable();
	   BT_serial.enable();
	   BT_Conf.enable();	           
	}	
	
	void BT_disable()
	{
	   BT_send.disable();
	   BT_Cancel.disable();
	   BT_Clear.disable();
	   BT_serial.disable();
	   BT_Conf.disable();	      	           
	}
}

⌨️ 快捷键说明

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