📄 app17_8.java
字号:
// app17_8, 对话框的实际应用
import java.awt.*;
import java.awt.event.*;
public class app17_8 extends Frame implements ActionListener
{
static app17_8 frm=new app17_8();
static Dialog dlg=new Dialog(frm); // 建立Dialog对象dlg
static Button Close_btn=new Button("Close"); // Close 按钮
static Button Cancel_btn=new Button("Cancel"); // Cancel按钮
static WinLis wlis=new WinLis(); // 建立聆听者对象wlis
public static void main(String args[])
{
frm.setTitle("Dialog Demo");
frm.setSize(200,150);
dlg.setTitle("Are you sure?"); // 设置对话框的标题
dlg.setSize(140,100); //设置对话框的大小
dlg.setLayout(new FlowLayout(FlowLayout.CENTER,5,30));
dlg.add(Close_btn); // 将Close_btn加入对话框中
dlg.add(Cancel_btn); // 将Cancel_btn加入对话框中
Cancel_btn.addActionListener(frm); // 设置Cancel_btn的聆听者
Close_btn.addActionListener(frm); // 设置Close_btn的聆听者
frm.addWindowListener(wlis); // 设置frm的聆听者
frm.setVisible(true);
}
static class WinLis extends WindowAdapter
{
public void windowClosing(WindowEvent e) // 按下窗口关闭钮时
{
dlg.setLocation(80,30); // 设置对话框的位置
dlg.show(); // 显示对话框
}
}
public void actionPerformed(ActionEvent e) // 按下对话框上的按钮时
{
Button btn=(Button) e.getSource(); // 取得被按下的按钮
if(btn==Close_btn) // 如果是Close按钮被按下
{
dlg.dispose(); // 关闭对话框
frm.dispose(); // 关闭窗口
}
else if (btn==Cancel_btn) // 如果是Cancel按钮被按下时
dlg.hide(); // 隐藏对话框
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -