inb.java

来自「一个简单的visio程序。」· Java 代码 · 共 485 行

JAVA
485
字号
/*function InputBox (prompt[,title][,defval][,xpos,ypos])
  InputBox return variant of vartype(8-String) InputBox$ return String
  4/4/1997 WeiB   */

package HPCore.stdfunc;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.StringTokenizer;
import HPCore.stdstmt.hpfileio;
import HPCore.Exception.*;
import HECore.stddata.*;

public class inb
{
   static Frame frame=null;
	public static Variant InputBox(String prompt) throws HpException
	{
      if (frame == null)
         frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt);
		in.show();
		return new VString(in.retval);
	}

	public static String InputBox$(String prompt) throws HpException
	{
      if (frame == null)
         frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt);
		in.show();
		return  in.retval;
   }

   public static Variant InputBox(String prompt,String title) throws HpException
	{
      if (frame == null)
         frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title);
      in.show();
		return new VString(in.retval);
   }

	public static String InputBox$(String prompt,String title) throws HpException
	{
      if (frame == null)
        frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title);
		in.show();
		return in.retval;
   }

   public static Variant InputBox(String prompt,String title,String defval) throws HpException
   {
      if (frame == null)
        frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title,defval);
      in.show();
		return new VString(in.retval);
	}

	public static String InputBox$(String prompt,String title,String defval) throws HpException
	{
      if (frame == null)
         frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title,defval);
		in.show();
		return in.retval;
	}

	public static Variant InputBox(String prompt,String title,String defval,short xpos) throws HpException
	{
      if (frame == null)
        frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title,defval);
		in.show();
		return new VString(in.retval);
	}

	public static String InputBox$(String prompt,String title,String defval,short xpos) throws HpException
	{
      if (frame == null)
        frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title,defval);
		in.show();
		return in.retval;
   }

   public static Variant InputBox(String prompt,String title,String defval,short xpos,short ypos) throws HpException
   {
      if (frame == null)
        frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title,defval,xpos,ypos);
		in.show();
		return new VString(in.retval);
   }

   public static String InputBox$(String prompt,String title,String defval,short xpos,short ypos) throws HpException
   {
      if (frame == null)
        frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title,defval,xpos,ypos);
		in.show();
		return in.retval;
   }

   public static Variant InputBox(String prompt,String title,String defval,short xpos,short ypos,String help) throws HpException
   {
      throw new HpException(5,"Invalid procedure call");
	}

   public static String InputBox$(String prompt,String title,String defval,short xpos,short ypos,String help) throws HpException
   {
		throw new HpException(5,"Invalid procedure call");
   }

   public static Variant InputBox(String prompt,String title,String defval,short xpos,short ypos,String help,short cont) throws HpException
   {
      if (frame == null)
        frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title,defval,xpos,ypos,help,cont);
      in.show();
		return new VString(in.retval);
   }

   public static String InputBox$(String prompt,String title,String defval,short xpos,short ypos,String help,short cont) throws HpException
   {
      if (frame == null)
        frame = new Frame();
		INPUTDIALOG in = new INPUTDIALOG(frame,prompt,title,defval,xpos,ypos,help,cont);
		in.show();
		return in.retval;
	}
}

class INPUTDIALOG extends Dialog implements KeyListener,ActionListener,WindowListener
{
	int x =1200,y=1400;
	String retval = null;
	String helpfile = null;
	int cont;
	Label pmpt ;
	TextField Input ;
	Button ok_but ;
	Button cancel_but ;
	Button help_but;

	public INPUTDIALOG(Frame f,String prompt)
	{
		super(f,"",true);
      if (prompt == null)
         prompt = "";
		fillinput(prompt,"");
	}

	public INPUTDIALOG(Frame f,String prompt,String title)
	{
		super(f,title,true);
      if (prompt == null)
         prompt = "";
		fillinput(prompt,"");
	}

	public INPUTDIALOG(Frame f,String prompt,String title,String defval)
	{
		super(f,title,true);
      if (prompt == null)  prompt = "";
      if (defval == null)  defval = "";
		fillinput(prompt,defval);
	}

	public INPUTDIALOG(Frame f,String prompt,String title,String defval,int xpos,int ypos)
	{
		super(f,title,true);
		x = xpos;
		y = ypos;
      if (prompt == null)  prompt = "";
      if (defval == null)  defval = "";
		fillinput(prompt,defval);
	}

	public INPUTDIALOG(Frame f,String prompt,String title,String defval,int xpos,int ypos,String help,int con)
	{
		super(f,title,true);
		x = xpos;
		y = ypos;
      if (prompt == null)  prompt = "";
      if (defval == null)  defval = "";
      if (help == null)  help = "";
		helpfile = help;
		cont = con;
		fillinput(prompt,defval);
	}

	public void actionPerformed(ActionEvent e)
	{
		Object obj = e.getSource();
		if (obj == ok_but)
		{
			retval = Input.getText();
			this.dispose(); 
		}
		else if (obj == cancel_but)
		{
         retval = "";
			this.dispose(); 
      }
		else if (obj == help_but)
		{
			if (helpfile != null)
			{
				String command[] = new String[2];
    			command[0] = "winhlp32";
    			command[1] = helpfile;
    			Runtime runtime = Runtime.getRuntime();
    			try{
    				runtime.exec(command);
				}catch(java.io.IOException ioe){
					System.out.println("ShowHelp Error. " + ioe);
    			}
    			runtime.gc();
			}
		}
   }

	public void keyPressed(KeyEvent evt)
	{
		Object obj = evt.getSource();
		int keycode = evt.getKeyCode();
		if (keycode == KeyEvent.VK_F1)
		{
			if (helpfile != null)
			{
				String command[] = new String[2];
    			command[0] = "winhlp32";
    			command[1] = helpfile;
    			Runtime runtime = Runtime.getRuntime();
    			try{
    				runtime.exec(command);
				}catch(java.io.IOException ioe){
					System.out.println("ShowHelp Error. " + ioe);
    			}
    			runtime.gc();
			}
		}
		else if (obj == Input && keycode == KeyEvent.VK_ENTER)
		{
			retval = Input.getText();
			this.dispose(); 
		}else if (keycode == KeyEvent.VK_TAB && obj == Input){
         ok_but.requestFocus();
		}else if (keycode == KeyEvent.VK_TAB && obj == ok_but){
			cancel_but.requestFocus();
		}else if (keycode == KeyEvent.VK_TAB && obj == cancel_but){
			if (helpfile != null)
				help_but.requestFocus();
			else Input.requestFocus();
		}else if (keycode == KeyEvent.VK_TAB && obj == help_but){
         Input.requestFocus();
      }else if(obj == ok_but && keycode == KeyEvent.VK_ENTER)
      {
			retval = Input.getText();
			this.dispose(); 
		}else if(obj == cancel_but && keycode == KeyEvent.VK_ENTER)
		{
         retval = "";
			this.dispose(); 
		}else if(obj == help_but && keycode == KeyEvent.VK_ENTER)
		{
			if (helpfile != null)
			{
				String command[] = new String[2];
    			command[0] = "winhlp32";
    			command[1] = helpfile;
    			Runtime runtime = Runtime.getRuntime();
    			try{
    				runtime.exec(command);
				}catch(java.io.IOException ioe){
					System.out.println("ShowHelp Error. " + ioe);
    			}
    			runtime.gc();
			}
		}
   }

	public void keyReleased(KeyEvent evt)
	{
		Object obj = evt.getSource();
		int keycode = evt.getKeyCode();

		if(obj == ok_but && keycode == KeyEvent.VK_SPACE)
		{
			retval = Input.getText();
			this.dispose(); 
		}else if(obj == cancel_but && keycode == KeyEvent.VK_SPACE)
		{
			retval = "";
			this.dispose(); 
		}else if(obj == help_but && keycode == KeyEvent.VK_SPACE)
		{
			if (helpfile != null)
			{
				String command[] = new String[2];
    			command[0] = "winhlp32";
    			command[1] = helpfile;
    			Runtime runtime = Runtime.getRuntime();
    			try{
    				runtime.exec(command);
				}catch(java.io.IOException ioe){
					System.out.println("ShowHelp Error. " + ioe);
    			}
    			runtime.gc();
			}
		}
   }

	public void keyTyped(KeyEvent e){}

	public void windowClosed(WindowEvent event) {}
	public void windowDeiconified(WindowEvent event) {}
	public void windowIconified(WindowEvent event) {}
	public void windowActivated(WindowEvent event) {}
	public void windowDeactivated(WindowEvent event) {}
	public void windowOpened(WindowEvent event) {}
	public void windowClosing(WindowEvent event) 
	{
		retval = "";
		this.dispose(); 
   }

   private void fillinput(String prompt,String defval)
   {
		setLayout(null);
		setFont(new Font("Ms Sans Serif", Font.PLAIN,11));
		String tmp=prompt;
		int chn=(int)'\n';
		int chr=(int)'\r';
		int i=0,j=0,k=0;
		Vector strVector = new Vector();
       
		while (tmp.indexOf(chn)!=-1 || tmp.indexOf(chr)!=-1)
      {
			i=tmp.indexOf(chn);
			j=tmp.indexOf(chr);
			if (i!=-1 && j==-1)
			{
				strVector.addElement(tmp.substring(0,i));
				tmp = tmp.substring(i+1);
			}else if (i==-1 && j!=-1){
				strVector.addElement(tmp.substring(0,j));
				tmp = tmp.substring(j+1);
			}else if (i==j+1){
				strVector.addElement(tmp.substring(0,j));
				tmp = tmp.substring(i+1);
			}else if (j==i+1){
				strVector.addElement(tmp.substring(0,i));
				tmp = tmp.substring(j+1);
			}else if (j>i+1){
				strVector.addElement(tmp.substring(0,i));
				tmp = tmp.substring(i+1);
			}else if (1+j<i){
				strVector.addElement(tmp.substring(0,j));
				tmp = tmp.substring(j+1);
			}
		}
		if (tmp != null && !tmp.equals(""))
			strVector.addElement(tmp);
		Vector mv = new Vector();
		for(int z=0;z<strVector.size();z++)
		{
			FontMetrics fm = getToolkit().getFontMetrics(new Font("Dialog", Font.PLAIN, 11));
			String str = (String)strVector.elementAt(z);
			int strWidth = fm.stringWidth(str);
			if (strWidth > 270)
			{
				StringTokenizer size = new StringTokenizer(str," ");
				StringTokenizer st = new StringTokenizer(str," ");
				StringBuffer s = new StringBuffer();
				StringBuffer stb = new StringBuffer();
				while (size.hasMoreTokens()) 
				{
					s.append(size.nextToken()+" ");
					strWidth = fm.stringWidth(s.toString());
					if (strWidth > 270)
					{
						mv.addElement(stb.toString());
						stb = new StringBuffer(st.nextToken()+" ");
						s = new StringBuffer();
					}else stb.append(st.nextToken()+" ");
				}
				mv.addElement(stb.toString());
			}else 
				mv.addElement(str);
		}
		Label bord[] = new Label[mv.size()];
		Input = new TextField(defval);
		add(Input);
		ok_but = new Button("Ok");
		cancel_but = new Button("Cancel");
		help_but = new Button("Help");
		if (helpfile != null)
		{
			ok_but.setBounds(getInsets().left+282,getInsets().top+25,65,25);
			cancel_but.setBounds(getInsets().left+282,getInsets().top+55,65,25);
			help_but.setBounds(getInsets().left+282,getInsets().top+85,65,25);
			add(ok_but);
			add(cancel_but);
			add(help_but);
	   }else{
			ok_but.setBounds(getInsets().left+282,getInsets().top+25,65,25);
			cancel_but.setBounds(getInsets().left+282,getInsets().top+55,65,25);
			add(ok_but);
			add(cancel_but);
	   }
	   for(k=0;k<mv.size();k++){
			bord[k] = new Label((String)mv.elementAt(k),0);
			bord[k].setBounds(getInsets().left+8,getInsets().top+25+k*16,272,15);
			add(bord[k]);
	   }
		ok_but.addKeyListener(this);
		cancel_but.addKeyListener(this);
		ok_but.addActionListener(this);
		cancel_but.addActionListener(this);
		help_but.addKeyListener(this);
		help_but.addActionListener(this);
		if(System.getProperty("os.name").equalsIgnoreCase("Windows 95")
			||System.getProperty("os.name").equalsIgnoreCase("Windows NT"))
	   {
		   if (k+1<=5)
		   {
			  Input.setBounds(getInsets().left+8,getInsets().top+115,344,23);
			  setSize(getInsets().left + getInsets().right + 360, getInsets().top + getInsets().bottom + 148);
		   }else {
			  Input.setBounds(getInsets().left+8,getInsets().top+(k+1)*16+25,344,23);
			  setSize(getInsets().left + getInsets().right + 360, getInsets().top + getInsets().bottom + (k+1)*16+53);
		   }
	   }else
	   {
		   if (k+1<=5)
		   {
			  Input.setBounds(getInsets().left+8,getInsets().top+115,344,29);
			  setSize(getInsets().left + getInsets().right + 360, getInsets().top + getInsets().bottom + 154);
		   }else {
			  Input.setBounds(getInsets().left+8,getInsets().top+(k+1)*16+25,344,29);
			  setSize(getInsets().left + getInsets().right + 360, getInsets().top + getInsets().bottom + (k+1)*16+59);
		   }
	   }
		Input.setBackground(Color.white);
		Input.addKeyListener(this);
		Input.requestFocus();
		addWindowListener(this);
		setResizable(false);
		setLocation(x/15,y/15);
   }

	/*public void Show()
	{
		HcBean.form.dialogisShow = true;
		super.show();		        
	}	

	public void setVisible(boolean visible)
	{		
		if(visible)
		{
			HcBean.form.dialogisShow = true;
			super.setVisible(visible);
		}
		else
		{
			HcBean.form.dialogisShow= false;
			super.setVisible(visible);			
		}
	}

   public final void dispose() //add by xiang -rong 98-05-12
   {
		HcBean.form.dialogisShow = false;		
	  HcBean.form.setSepState();
	  super.dispose();
	  HcBean.form.DialogDispose();
	}*/
}

⌨️ 快捷键说明

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