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

📄 customdialog.java

📁 打印管理程序,测试完全通过.windows开发环境.
💻 JAVA
字号:
/* 
    $Author: $
    $Date: $
    $Revision: $
    $NoKeywords: $
*/
package jp.co.ntl.swing;

import java.awt.AWTEventMulticaster;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.BorderLayout;
import java.awt.event.*;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

public class CustomDialog extends JDialog implements ActionListener {
    /**
	 * 
	 */
	private static final long serialVersionUID = 976276973533296354L;
	public static int UNINITIALIZED_VALUE   = 100;
    public static int WINDOW_CLOSE_OPTION   = 200;
    
    private ValidatePanel 				vpanel = null;
    private JButton[] 					buttons;
    
    transient ActionListener			actionListener;
    private String						actionCommand = "close";
   
    public CustomDialog(Frame owner, String title, boolean modal, ValidatePanel vpanel) {
        super(owner, title, modal);
        this.vpanel = vpanel;
        this.buttons = vpanel.getButtons();
        
        if (title.equals("")) {
        	setUndecorated(true);
        }
        
        Container	cont = getContentPane();
        cont.setLayout(new BorderLayout());
        
        JPanel	panel = new JPanel(new BorderLayout());
        panel.add(vpanel, BorderLayout.CENTER);
        cont.add(panel, BorderLayout.CENTER);
        
        JPanel	btnPanel = new JPanel(new BorderLayout());
        btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
        btnPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        btnPanel.add(Box.createHorizontalGlue());
        
        for (int i = 0; i < buttons.length; i++) {
        	btnPanel.add(buttons[i]);
        	buttons[i].addActionListener(this);
        	if (i != buttons.length - 1) {
        		btnPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        	}
        }
        cont.add(btnPanel, BorderLayout.PAGE_END);
        pack();
        
        getRootPane().setDefaultButton(buttons[vpanel.getDefaultButton()]);
        
		Dimension dialogDim = getSize();
        Dimension ownerDim = owner.getSize();
        Dimension screenSize = getToolkit().getScreenSize();
        Point location = owner.getLocation();
        location.translate(
        	(ownerDim.width - dialogDim.width) / 2,
        	(ownerDim.height - dialogDim.height) / 2);
        	
        location.x = Math.max(0, Math.min(location.x, screenSize.width - dialogDim.width));
        location.y = Math.max(0, Math.min(location.y, screenSize.height - dialogDim.height));
        
        setLocation(location.x, location.y);
        
        setResizable(false);

        addWindowListener(new MyWindowListener());
    }
    
    public void paint(Graphics g) {
    	super.paint(g);
    	
    	if (getTitle().equals("")) {
    		g.draw3DRect(0, 0, getWidth() - 1, getHeight() - 1, true);
    	}
    }
    
    private int codeAtEnd = UNINITIALIZED_VALUE;
    public int getSelection() { 
        return codeAtEnd;
    }
    
	public synchronized void addActionListener(ActionListener al) {
		actionListener = AWTEventMulticaster.add(actionListener, al);
	}
	
	public synchronized void removeActionListener(ActionListener al) {
		actionListener = AWTEventMulticaster.remove(actionListener, al);
	}
    
    public String getActionCommand() {
    	return actionCommand;
    }
    
    public void setActionCommand(String command) {
    	this.actionCommand = command;
    }
    
    public void actionPerformed(ActionEvent ae) {
        Object source = ae.getSource();
        for (int i = 0; i < buttons.length; i++) {
            if (source == buttons[i]) {
                if (vpanel.isValid(i)) {
                    codeAtEnd = i;
                    if (!isModal()) {
        		    	if (actionListener != null) {
        		    		actionCommand = "button" + String.valueOf(codeAtEnd);
        					actionListener.actionPerformed(
        							new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
        		    	}              	
                    }
                    setVisible(false);
                    dispose();
                } else {
                    // 壗傕偟側偄
                }
                break;
            }
        }
    }
    
    class MyWindowListener extends WindowAdapter {
        public void windowClosing(WindowEvent we) {
            codeAtEnd = WINDOW_CLOSE_OPTION;
            if (!isModal()) {
		    	if (actionListener != null) {
		    		actionCommand = "close";
					actionListener.actionPerformed(
							new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
			 	}
            }
            setVisible(false);
            dispose();
        }
    }
    
    public static Frame getFrameForComponent(Component comp) {
        Component parent = comp;
        while (parent != null) {
            if (parent instanceof Frame) {
                break;
            }
            parent = parent.getParent();
        }
        return (Frame)parent;
    }
    
/*    public static int showCustomDialog(Component parent, String title, ValidatePanel vpanel) {
    	CustomDialog	dialog = new CustomDialog(getFrameForComponent(parent), title, true, vpanel);
        dialog.setVisible(true);
        
        return dialog.getSelection();
    }*/
}

⌨️ 快捷键说明

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