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

📄 responsedialog.java

📁 ALGAE是一个快速创建算法演示的框架。目前支持的算法实现语言包括java和c
💻 JAVA
字号:
package edu.odu.cs.zeil.AlgAE;import java.awt.BorderLayout;import java.awt.Button;import java.awt.Dialog;import java.awt.Event;import java.awt.Frame;import java.awt.Panel;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import edu.odu.cs.zeil.AlgAE.MessageListener;/** *  Dialog box displaying for displaying a prompt and awaiting a response. *  @author Steven J. Zeil */public class ResponseDialog extends Dialog{    private TextField response;  private MessageListener notify;  private boolean sent;      /**   *  Constructor   *  @param theParent   Window from which this dialog is issued.   *  @param text        Text to display in dialog   */  public ResponseDialog  (Frame theParent, String text,			  MessageListener notifyWhenClosed)  {    super (theParent, "AlgAE", false);    notify = notifyWhenClosed;    sent = false;    Panel central = new Panel();    central.setLayout (new BorderLayout());    int maxWidth = 0;    int width = 0;    int nLines = 0;    int pos = 0;    int endLinePos = 0;        while (endLinePos >= 0)      {	++nLines;	endLinePos = text.indexOf(10, pos);	if (endLinePos >= 0)	  width = endLinePos - pos;	else	  width = text.length() - pos;		maxWidth =  (width > maxWidth) ? width : maxWidth;	pos = endLinePos + 1;      }        TextArea msg = new TextArea (text, nLines, maxWidth,				 TextArea.SCROLLBARS_NONE);    msg.setEditable(false);    central.add ("Center", msg);    response = new TextField(20);    response.setEditable(true);    central.add ("South", response);    add ("Center", central);        Button ok = new Button ("OK");    ok.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {	  sendResponse();	}});    add ("South", ok);    addWindowListener(new WindowAdapter() {      public void windowActivated(WindowEvent e) {response.requestFocus();}      public void windowClosing(WindowEvent e) {	  sendResponse();      }    });    response.addKeyListener(new KeyAdapter() {      public void keyPressed(KeyEvent e) {	if (e.getKeyCode() == KeyEvent.VK_ENTER)	  {	    sendResponse();	  }	}});     pack();    response.requestFocus();    show();  }    public void sendResponse()    {      if (!sent)	{	  sent = true;	  setVisible(false);	  notify.messageReceived(getText(), null);	  dispose();	}    }      /**   *  Get the text currently in the editable response field.   */  public String getText()  {    String reply = response.getText();    reply.trim();    return reply;  }  }

⌨️ 快捷键说明

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