closeframe.java

来自「它用来实现一个简单的窗口控件功能」· Java 代码 · 共 79 行

JAVA
79
字号
package close;
import java.awt.*;
import java.awt.event.*;
public class CloseFrame extends Frame implements WindowListener,ActionListener
{
	Button button_ok,button_yes,button_no;
	Dialog dialog;
	private Label label_dialog;

	public CloseFrame()
	{
		super("CloseFrame");
		this.setSize(320,280);
		this.setBackground(Color.white);
		this.setLocation(300,240);
		this.setLayout(new FlowLayout());
		button_ok=new Button("OK");
		this.add(button_ok);
		button_ok.addActionListener(this);
		this.addWindowListener(this);
		
		this.setVisible(true);
		
		dialog = new Dialog(this,"Dialog",true);
		dialog.setSize(54,100);
		dialog.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));
		label_dialog = new Label("真的关闭吗",Label.CENTER);
		dialog.add(label_dialog);
		button_yes=new Button("YES");
		dialog.add(button_yes);
		button_yes.addActionListener(new DialogClose(this));
		button_no=new Button("NO");
		dialog.add(button_no);
		button_no.addActionListener(new DialogClose(this));
		dialog.addWindowListener(this);
		
	}
	public void actionPerformed(ActionEvent e)
	{
		dialog.setLocation(380,280);
		dialog.setVisible(true);
	}
	
	public void windowClosing(WindowEvent e)
	{
		dialog.setLocation(380,280);
		dialog.setVisible(true);
		if (e.getSource()==dialog)
			dialog.setVisible(false);
	}
	public void windowOpened(WindowEvent e){}
	public void windowActivated(WindowEvent e){}
	public void windowDeactivated(WindowEvent e){}
	public void windowClosed(WindowEvent e){}
	public void windowIconified(WindowEvent e){}
	public void windowDeiconified(WindowEvent e){}
	
	public static void main(String args[])
	{
		new CloseFrame();
	}
	
}

class DialogClose implements ActionListener
{
	private CloseFrame frame;
	public DialogClose(CloseFrame frame)
	{
		this.frame=frame;
	}
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource()==frame.button_yes)
			System.exit(0);
		else
			frame.dialog.setVisible(false);
	}
}

⌨️ 快捷键说明

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