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

📄 testdialog.java

📁 java 的小东西 大家看看啊
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

public class TestDialog		//定义Java Application主类
{
	public static void main(String args[])
	{
		MyDialogFrame df = new MyDialogFrame();
	}
}

class MyDialogFrame extends Frame 
		implements ActionListener,ComponentListener,FocusListener
{
	Dialog MegDlg,InOutDlg;			//对话框隶属于Frame
	Button btn1,btn2,btnY,btnN,btnR;
	TextField tf = new TextField("没有信息",45);
	TextField getMeg = new TextField("输入信息",10);

	MyDialogFrame()					//构造函数
	{
		super("使用对话框");
		btn1 = new Button("隐藏");
		btn2 = new Button("询问");
		btnY = new Button("是");
		btnN = new Button("否");
		btnR = new Button("返回");
		setLayout(new FlowLayout());
		add(tf);	
		add(btn1);	
		add(btn2);
		btn1.addComponentListener(this);	//Frame响应容器事件
		this.addWindowListener(new WinAdpt());	//Frame响应窗口关闭事件
		btn1.addActionListener(this);	
		btn2.addActionListener(this);
		btnY.addActionListener(this);	
		btnN.addActionListener(this);
		btnR.addActionListener(this);
		setSize(350,150);		//改变Frame尺寸
		show();				//显示原来不可见的Frame
	}
	public void actionPerformed(ActionEvent e) //响应按钮引发的动作事件
	{
		if(e.getActionCommand()=="隐藏")  //按"隐藏"按钮将隐藏此按钮本身
		{	//创建"有模式"的消息对话框向用户确认删除操作
			MegDlg = new Dialog(this,"真要隐藏吗?",true);
			Panel p1 = new Panel();
			p1.add(new Label("此操作将隐藏该按钮,要继续吗?"));
			MegDlg.add("Center",p1);
			Panel p2 = new Panel();
			p2.add(btnY);
			p2.add(btnN);
			MegDlg.add("South",p2);
			MegDlg.setSize(200,100);
			MegDlg.show();		//显示对话框
		}
		else if(e.getActionCommand()=="询问")
		{	//创建"无模式"的对话框接受用户输入的信息
			InOutDlg = new Dialog(this,"请输入信息");
			InOutDlg.add("Center",getMeg);
			InOutDlg.add("South",btnR);
			InOutDlg.setSize(200,100);
			InOutDlg.addFocusListener(this);
			InOutDlg.show();
		}
		else if(e.getActionCommand()=="是")//用户对消息对话框中的按钮的响应
		{
			MegDlg.dispose();		//关闭消息对话框
			btn1.setVisible(false);	//确认隐藏,引发组件事件
		}
		else if(e.getActionCommand()=="否")//用户对消息对话框中的按钮的响应
			MegDlg.dispose();	//取消隐藏操作,关闭对话框
		else if(e.getActionCommand()=="返回")//用户对输入输出对话框中按钮的响应
		{	//获取用户在对话框的输入并显示在Frame中
			tf.setText(getMeg.getText()+"是对话框的输入");	
			InOutDlg.dispose();		//关闭输入输出对话框
		}
	}
	public void componentShown(ComponentEvent e){}//实现ComponentListener中方法
	public void componentResized(ComponentEvent e){}
	public void componentMoved(ComponentEvent e){}
	public void componentHidden(ComponentEvent e) //当按钮被隐藏时,显示相关信息
	{
		tf.setText("按钮\""+((Button)e.getComponent()).getLabel()+"\"被隐藏!");
	}
	public void focusGained(FocusEvent e)
	{
		getMeg.setText("对话框\""+((Dialog)e.getComponent()).getTitle()
			+"\"获得了注意的焦点!");
	}
	public void focusLost(FocusEvent e)	{}
}
class WinAdpt extends WindowAdapter	//创建窗口剪裁类子类,处理窗口关闭事件
{
	public void windowClosing(WindowEvent e)
	{
		((Frame)e.getWindow()).dispose();//获得引发事件的窗口并关闭之
		System.exit(0);
	}
}

⌨️ 快捷键说明

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