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

📄 message_box.java

📁 身份认证和数字签名在实际应用中是通过以数字证书为核心的公开密钥基础结构(PKI)来实现的
💻 JAVA
字号:
/****************************************************************
* 软件:Java签名协议扩展 (Java Signature Protocol Extension, JSPE)
* 版本:V1.0
* 软件功能:实现SSL签名协议
* 模块:公共类Message_Box
*-----------------------------------------------------------------
*                                 版权所有:中山大学软件研究所 2002
*                                 Programmed by 佛山张峰岭 fszfl@21cn.com
*                                              2002.4 - 2002.5
*-----------------------------------------------------------------
*    这个类用于产生一个接收信息输入的窗口
*    根据提示的信息提示用户在窗口中输入字符串
*****************************************************************/
package com.zsusoft.zfl;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
*   类Message_Box是一个通用的用窗口方式提示用户输入信息的工具
*   用于实现弹出提示窗口界面提示用户输入信息,用于用户输入密码和确认执行签名。
**/
public class Message_Box
  extends Frame
    implements ActionListener, WindowListener, KeyListener
{
   /**
   *  定义界面成员
   **/	
   private TextField text;   //输入域
   //private Label label;      //提示行
   private TextArea label;     //提示信息显示区域
   private Button ok;        //确认按钮
   private Button cancel;    //取消按钮
   private String Desc_Message = "此处显示提示信息!";  //提示信息
   private boolean finished = false;
   /**
   * 构造函数用于生成界面
   **/
   Message_Box()
   {
   	super("SSL签名协议输入窗口");
   	
   	Panel pFrame = new Panel();
	pFrame.setLayout(new BorderLayout(0, 0));

        //label = new Label(Desc_Message);
        label = new TextArea(Desc_Message);
        label.setEditable(false);
        label.setRows(5);
        pFrame.add("Center",label);
	
	Panel pFrame_in = new Panel();
	pFrame_in.setLayout(new GridLayout(2,1,5,5));
	
	text = new TextField("");
	//text.setForeground(Color.yellow);
	text.setEditable(true);
	text.addKeyListener( this);
	pFrame_in.add(text);
        
        Panel pKey = new Panel();
        pKey.setLayout(new GridLayout(1,2));
        ok=new Button("Ok");
        ok.addActionListener( this);
        cancel=new Button("Cancel");
        cancel.addActionListener( this);
        pKey.add(ok);
        pKey.add(cancel);
        pFrame_in.add(pKey);
   	
        pFrame.add("South",pFrame_in);
   
   	setLayout(new BorderLayout(0, 0));	
	add("North", pFrame);
   	
   	addWindowListener( this);
	addKeyListener( this);

	//setBounds(0,0,250,200);
	//setResizable(false);
	pack();
	
   }
   /**
   *   显示输入窗口,等候用户输入字符串
   *   Desc_Msg为提示的信息
   *   boolean = true 显示键入的内容
   *           = false 不显示键入的内容 用*代替
   **/
   public String inputString(String Desc_Msg,boolean echo)
   {
   	Desc_Message = Desc_Msg;
   	label.setText(Desc_Message);
   	
   	finished = false;
   	//以下逻辑用于计算Desc_Msg的行数,用于调整提示信息显示区域大小
   	int line_count = 2;
   	int fromIndex = 0;
   	int nextIndex = 0;
   	int max_line_col= 0;
   	while( nextIndex >= 0)
   	{
   		line_count += 1;
   		nextIndex = Desc_Msg.indexOf('\n',fromIndex);
   		if((nextIndex - fromIndex) > max_line_col) max_line_col = nextIndex - fromIndex;  
   		if(nextIndex != -1) fromIndex =nextIndex + 1;  //找下一个回车
   	}
   	if (max_line_col == 0) max_line_col = Desc_Msg.length();
   	max_line_col += 10;  
   	if (max_line_col > 90) max_line_col =90 ;
   	if(line_count > 30 ) line_count = 30 ;  //不能太多
   	
   	//调整窗口大小
   	label.setColumns(max_line_col);
   	label.setRows(line_count);
   	  //System.err.println("line_count =" + Integer.toString(line_count));
   	  //System.err.println("max_line_col =" + Integer.toString(max_line_col));
   	setResizable(false);
   	setBounds(50,30,0,0);
   	setSize(getPreferredSize());
   	pack();
   	show();
   	
   	if(echo)
   	   text.setEchoChar((char)0);
   	   else
   	   text.setEchoChar('*');
   	text.setText("");
   	text.requestFocus();
   	while(finished == false)
   	{
   		toFront();
   		//
   	}
   	hide();
   	return text.getText();
   }
   
   public void actionPerformed ( ActionEvent e ) {
	String arg = e.getActionCommand();
	if(arg.equals("Ok")){
		finished = true;
	}
	if(arg.equals("Cancel")){
		text.setText("");
		finished = true;
	}
   }
   
   public void keyPressed ( KeyEvent e ) {
   }

   public void keyReleased ( KeyEvent e ) {
   }

   public void keyTyped ( KeyEvent e ) {
   	if(e.getKeyChar() == '\n') //回车
   	  {
   	  	finished = true;
   	  }
   }
   
   public void windowActivated ( WindowEvent evt ) {
   	text.requestFocus();
   }

   public void windowClosed ( WindowEvent evt ){
     //hide();
     text.setText("");
     finished = true;
   }
   
   public void windowClosing ( WindowEvent evt ){
	hide();
	//dispose();
   }

   public void windowDeactivated ( WindowEvent evt ){
   }

   public void windowDeiconified ( WindowEvent evt ){
   }

   public void windowIconified ( WindowEvent evt ){
   }

   public void windowOpened ( WindowEvent evt ){
   }
   /**
   *   main用于对Message_Box功能的调试和测试
   **/
   public static void main(String args[])
   {
	Message_Box msg = new Message_Box();
	if(args.length == 0)
	 System.err.println(msg.inputString("请输入密码",false));
	else
	 System.err.println(msg.inputString(args[0],true));
	System.exit(0);
   }
}

⌨️ 快捷键说明

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