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

📄 msgbox.java

📁 一个简单的visio程序。
💻 JAVA
字号:
/**
 * MsgBox class, MsgBox.java
 * Author : Phong Nguyen Le
 * Created : 03/25/98
 * Modified : 03/26/98
 */

package hc.util;

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.awt.image.*;

public class MsgBox extends Dialog implements ActionListener, WindowListener, KeyListener
{
    private static String[] BUTTON_LABELS = {"Order", "OK", "Yes", "Yes to all", "Back", "Next", "Retry", "Continue",
						 "No", "No to all", "Move", "Delete", "Cancel", "Exit", "Help"};

	private int maxButtons = BUTTON_LABELS.length;
	private int validButtonSet = ((int)Math.pow(2, maxButtons)) - 1;
	private Button[] buttons = new Button[0];

    public static final int NIL    	   = 0;
    public static final int ORDER	   = 1;
    public static final int OK     	   = 2;
    public static final int YES    	   = 4;
    public static final int YES_TO_ALL	   = 8;
    public static final int BACK	   = 16;
    public static final int NEXT	   = 32;
    public static final int RETRY	   = 64;
    public static final int CONTINUE	   = 128;
    public static final int NO     	   = 256;
    public static final int NO_TO_ALL	   = 512;
	//Xuejun added two button as below;
    public static final int MOVE		= 1024;
	public static final int DELETE		=2048;
	public static final int CANCEL 	   = 4096;
    public static final int EXIT	   = 8192;
    public static final int HELP	   = 16384;

    public static final int CROSS 	   = -1;

    public static final int INFORMATION = 0;
    public static final int EXCLAMATION = 1;
    public static final int STOP = 2;
    public static final int HAND = 3;

    private static String[] icons = { "question.gif", "exclaim.gif", "stop.gif", "hand.gif" };

    private int     buttonSet              = NIL;
    private int     status 		   = NIL;
	private int 	buttonWidth 		   = 74;
	private int 	buttonHeight		   = 23;
    private ArrayIndexOutOfBoundsException err   = new ArrayIndexOutOfBoundsException("\nMsgBox: [ERROR] " + "No such button set available");

    ImageCanvas imgCanvas = null;
    FormatTextCanvas textCanvas = null;
    Panel buttonPanel = null;

    boolean initialized = false;

    public MsgBox(Frame parent)
    {
  	    super(parent,"", true);
	init("", null, NIL, getImage(-1), false);
    }

    public MsgBox(Frame parent, String title, String message, int buttonSet, int icon) {
	super(parent, title, true);
	init(message, null, buttonSet, getImage(icon), true);
    }

    public MsgBox(Frame parent, String title, String message, int buttonSet, int icon, boolean show) {
	super(parent, title, true);
	init(message, null, buttonSet, getImage(icon), show);
    }

    public MsgBox(Frame parent, String title, String message, int buttonSet, String imgPath) {
	super(parent, title, true);
	init(message, null, buttonSet, getImage(imgPath), true);
    }

    public MsgBox(Frame parent, String title, String msg, Font font, int buttonSet, String imgPath) {
	super(parent, title, true);
	init(msg, font, buttonSet, getImage(imgPath), true);
    }

    public MsgBox(Frame parent, String title, String message, Font font, int buttonSet, Image img, boolean show)
    {
        super(parent, title, true);
	init(message, font, buttonSet, img, show);
    }

    void init(String message, Font font, int buttonSet, Image img, boolean show) {
	addNotify();
	setLayout(null);
	setBackground(Color.lightGray);
        addWindowListener(this);
	setResizable(false);
	setImage(img);
	setMessage(message, font, null);
	setButtonSet(buttonSet);
	validate();
	setVisible(show);
	initialized = true;
    }

    public void doLayout() {
    	Dimension imgSize = new Dimension(0, 0),
		    textSize = new Dimension(0, 0),
		    buttonsSize = new Dimension(0, 0);
	if(imgCanvas != null)
	    imgSize = imgCanvas.getSize();
	if(textCanvas != null)
	    textSize = textCanvas.getSize();
	if(buttonPanel != null)
	    buttonsSize = buttonPanel.getSize();
	int w = Math.max(buttonsSize.width, imgSize.width + textSize.width + 10) +
		20 + getInsets().left + getInsets().right;
	int h = Math.max(imgSize.height, textSize.height) + buttonsSize.height + 30 +
		getInsets().top + getInsets().bottom;
	Dimension size = getToolkit().getScreenSize();
	setBounds(Math.max(0, (size.width - w) / 2), Math.max(0, (size.height - h) / 2), w, h);
	int upperH = h - buttonsSize.height - getInsets().bottom - 10;
	if(buttonPanel != null)
	    buttonPanel.setLocation((w - buttonsSize.width) / 2, upperH);
	upperH -= getInsets().top;
	int space = (w - getInsets().left - imgSize.width - getInsets().right - textSize.width) / 3;
	if(imgCanvas != null)
	    imgCanvas.setLocation(getInsets().left + space, getInsets().top + (upperH - imgSize.height)/ 2);
	if(textCanvas != null)
	    textCanvas.setLocation(getInsets().left + space * 2 + imgSize.width,
				getInsets().top + (upperH -textSize.height) / 2);
    }

    public void setImage(String imgPath) {
	setImage(getImage(imgPath));
    }

    public void setImage(Image img) {
	if(imgCanvas != null) {
	    remove(imgCanvas);
	    imgCanvas = null;
	}
	if(img != null) {
	    imgCanvas = new ImageCanvas(img);
	    add(imgCanvas);
	    if(initialized)
	        validate();
	}
    }


    public void setMessage(String msg) {
	setMessage(msg, null, null);
    }

    /**
     * display message and wrap long line with width greater than width
     */
    public void setMessage(String msg, int width) {
	if(textCanvas != null) {
	    remove(textCanvas);
	    textCanvas = null;
	}
	if(msg == null || msg.length() == 0)
	    return;
	textCanvas = new FormatTextCanvas(msg, width);
	textCanvas.setBackground(getBackground());
	add(textCanvas);
	if(initialized)
	    validate();
    }

    public void setMessage(String msg, Font font, Color color) {
	if(textCanvas != null) {
	    remove(textCanvas);
	    textCanvas = null;
	}
	if(msg == null || msg.length() == 0)
	    return;
	textCanvas = new FormatTextCanvas(msg, font);
	textCanvas.setBackground(getBackground());
	if(color != null) {
	    textCanvas.setForeground(color);
	}
	add(textCanvas);
	if(initialized)
	    validate();
    }

    public void setButtonSet(int buttonSet)
    {
        if(buttonSet < 0 || buttonSet > validButtonSet)
            throw err;
	if(buttonPanel != null) {
	    remove(buttonPanel);
	    buttonPanel = null;
	}
	buttonPanel = new Panel();
	buttonPanel.setLayout(null);
	buttonPanel.setBackground(getBackground());
        this.buttonSet = buttonSet;

		buttons = null;
		buttons = new Button[0];
		int totalButtonsRequested = 0;
		for(int i = 0; i < maxButtons; i++)
		{
			if((buttonSet & ((int)Math.pow(2, i))) != 0)
			{
				Button[] tempButtons = new Button[buttons.length + 1];
				System.arraycopy(buttons, 0, tempButtons, 0, buttons.length);
				tempButtons[buttons.length] = new Button(BUTTON_LABELS[i]);
				buttonPanel.add(tempButtons[buttons.length]);
				tempButtons[buttons.length].addActionListener(this);
				tempButtons[buttons.length].addKeyListener(this);//<---Xuejun added key Listener.
				buttons = tempButtons;
				totalButtonsRequested = totalButtonsRequested + 1;
			}
		}
		buttonPanel.setSize((buttonWidth * totalButtonsRequested)+((totalButtonsRequested-1)*10), buttonHeight);
		int startXLocation = 0;
		for(int j = 0; j < totalButtonsRequested; j++)
		{
			buttons[j].setBounds(startXLocation, 0, buttonWidth, buttonHeight);
			startXLocation = startXLocation + buttonWidth + 10;
		}
	add(buttonPanel);
	if(initialized)
	    validate();
    }

    public int getButtonSet()
    {
        return buttonSet;
    }

    Image getImage(int icon) {
	Image img = null;
	if(icon >= 0 && icon < 4) {
	    MediaTracker tracker = new MediaTracker(this);
	    img = loadImage(icons[icon]);
	    tracker.addImage(img, 0);
	    try {
		tracker.waitForID(0);
	    }
	    catch(Exception e) {
		//
	    }
	}
	return img;
    }


    Image getImage(String imgPath) {
	Image img = getToolkit().getImage(imgPath);
	MediaTracker tracker = new MediaTracker(this);
	tracker.addImage(img, 0);
	try {
	    tracker.waitForID(0);
	}
	catch(Exception e) {
	    //
	}
	return img;
    }

    public void actionPerformed(ActionEvent event)
    {
        String buttonClicked = ((Button) event.getSource()).getLabel();
		clickButton(buttonClicked);//<---Xuejun modified.

    }

	//--->
	//Xuejun added the followings for key evnets:
	private void clickButton(String buttonClicked)
	{

		for(int i = 0; i < BUTTON_LABELS.length; i++)
			if(BUTTON_LABELS[i].compareTo(buttonClicked) == 0)
				status = (int)Math.pow(2, i);

        setVisible(false);
	}

	public void keyPressed(KeyEvent e){
        int key = e.getKeyCode();
        Object obj = e.getSource();
        if (key == KeyEvent.VK_ENTER)
		{
			String buttonClicked = ((Button) e.getSource()).getLabel();
			clickButton(buttonClicked);
		}
	}

    public void keyReleased(KeyEvent e){}

    public void keyTyped(KeyEvent e){}

	//<---End

    public void windowActivated(WindowEvent e){}
    public void windowClosed(WindowEvent e){}
    public void windowDeactivated(WindowEvent e){}
    public void windowDeiconified(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowOpened(WindowEvent e){}
    public void windowClosing(WindowEvent event)
    {
        if(event.getSource() == this && buttonSet == NIL)
	{
	    status = CROSS;
	    setVisible(false);
	}
        else
            return;
    }

	public int getStatus()
	{
		return status;
	}

   public java.awt.Image loadImage(String imgName) {

        //System.out.println("imgName = " + imgName);
        webide.MainConsole applet = webide.MainConsole.getMainConsole();
		if(applet==null) return null;
        java.net.URL url = applet.getCodeBase();
		//System.out.println("url::::::::::" + url);
        String imgRes = applet.getParameter("imagesource") + java.io.File.separator  + imgName;
        try {
        url = new URL(url,imgRes);
        }catch(Exception e) {
            e.printStackTrace();
        }
        //System.out.println("url = " + url + ", imgName = " + imgName);
        if (url != null) {
            return applet.getImage(url);
        }else{
            return null;
        }

   }

    public static int MessageBox(Frame parent, String title, String msg, int buttonSet, int icon) {
	MsgBox msgBx = new MsgBox(parent, title, msg, buttonSet, icon);
    	int re = msgBx.getStatus();
	msgBx.dispose();
	return re;
    }

    public static int MessageBox(Frame parent, String title, String msg, int buttonSet, String imgPath) {
	MsgBox msgBx = new MsgBox(parent, title, msg, buttonSet, imgPath);
    	int re = msgBx.getStatus();
	msgBx.dispose();
	msgBx = null;
	return re;
    }

    public static int MessageBox(Frame parent, String title, String msg, int msgWidth, int buttonSet, int icon) {
	MsgBox msgBx = new MsgBox(parent, title, null, buttonSet, icon, false);
	msgBx.setMessage(msg, msgWidth);
	msgBx.setVisible(true);
    	int re = msgBx.getStatus();
	msgBx.dispose();
	msgBx = null;
	return re;
    }

    /**
     * use this function only if you don't have any Frame object to pass to function.
     */
    public static int MessageBox(String title, String msg, int buttonSet, int icon) {
	Frame parent = new Frame();
	parent.addNotify();
	MsgBox msgBx = new MsgBox(parent, title, msg, buttonSet, icon);
    	int re = msgBx.getStatus();
	msgBx.dispose();
	parent.dispose();
	msgBx = null;
	parent = null;
	return re;
    }




} // end of MsgBox.java

⌨️ 快捷键说明

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