simpledialogtest.java

来自「java2图形设计卷1:awt 源码」· Java 代码 · 共 64 行

JAVA
64
字号
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class SimpleDialogTest extends Applet {
	Button launchButton = new Button("Show Dialog ...");
	Dialog dialog;
	Frame myFrame;

	public void init() {
		add(launchButton);

		launchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				Point scrnLoc = myFrame.getLocationOnScreen();
				Dimension dialogSize = dialog.getSize();

				dialog.setLocation(
					scrnLoc.x - dialogSize.width - 2,
				    scrnLoc.y);

				showStatus(null);
				dialog.show();
				showStatus("Dialog shown");
			}
		});
	}
	public void start() {
	 	Button doneButton = new Button("Done");

		myFrame = getFrame(SimpleDialogTest.this);
		dialog  = new Dialog(myFrame, "Simple Dialog", true);

		dialog.add(doneButton);
		dialog.pack();
		dialog.setResizable(false);

		doneButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				dialog.dispose();
				showStatus(null);
			}
		});
		dialog.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				System.out.println("Window Closing");
				dialog.dispose();
			}
			public void windowClosed(WindowEvent event) {
				System.out.println("Window Closed");
			}
		});
	}
    static Frame getFrame(Component c) {
		Frame frame = null;

        while((c = c.getParent()) != null) {
            if(c instanceof Frame)
                frame = (Frame)c;
        }
        return frame;
    }
}

⌨️ 快捷键说明

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