confirmdialog.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 59 行

JAVA
59
字号
import java.awt.*;
import java.awt.event.*;

public class ConfirmDialog extends Dialog implements ActionListener
{
	private Button okey		=	new Button("Okey");
	private Button cancel	=	new Button("Cancel");
	private Label label		=	new Label("Are you sure?", Label.CENTER);
	public boolean isOkay	=	false;
	private class WindowCloser extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			ConfirmDialog.this.isOkay	=	false;
			ConfirmDialog.this.hide();
		}
	}
	public ConfirmDialog(Frame parent)
	{
		this(parent, "Please confirm", "Are you sure?");
	}
	public ConfirmDialog(Frame parent, String title, String question)
	{
		super(parent, title, true);
		label.setText(question);
		setup();
		okey.addActionListener(this);
		cancel.addActionListener(this);
		addWindowListener(new WindowCloser());
		setResizable(false);
		pack();
		int x	=	parent.getLocation().x + (parent.getSize().width - getSize().width) / 2;
		int y	=	parent.getLocation().y + (parent.getSize().height - getSize().height) / 2;
		x = Math.min(getToolkit().getScreenSize().width - getSize().width, x);
		y = Math.min(getToolkit().getScreenSize().height - getSize().height, y);
		setLocation(Math.max(0, x), Math.max(0, y));
		show();
	}
	private void setup()
	{
		Panel buttons	=	new Panel();
		buttons.setLayout(new FlowLayout());
		buttons.add(okey);	buttons.add(cancel);
		setLayout(new BorderLayout());
		add("Center", label);	add("South", buttons);
		setLocationRelativeTo(null);
	    //setLocation(300, 300);
	}
	public void actionPerformed(ActionEvent ae)
	{
		isOkay = (ae.getSource() == okey);
		//---------------------- centerilzition ------------------------------------------------------
	    //--------------------------------------------------------------------------------------------
	    
	    //-------method II
		hide();
	}
	
}

⌨️ 快捷键说明

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