[javaSE] GUI(对话框Dialog)
发布时间:2021-05-21 06:44:43 所属栏目:大数据 来源: https://www.jb51.cc
导读:对话框不能单独存在,依赖于窗体,有显示标题,有模式 ? 获取 Dialog 对象, new 出来,构造参数: Frame 对象, String 的标题,模式 窗体内部的内容, Label 对象, Button 对象,调用 Dialog 对象的 add() 方法,把这两个添加进去 Dialog 也是一个普通的
对话框不能单独存在,依赖于窗体,有显示标题,有模式 ? 获取Dialog对象,new出来,构造参数:Frame对象,String的标题,模式 窗体内部的内容,Label对象,Button对象,调用Dialog对象的add()方法,把这两个添加进去 Dialog也是一个普通的窗体,需要设置尺寸和位置 这个Dialog窗体不用加入到Frame窗体中,只需要设置是否显示就可以了 ? 对话框的叉号,给对话框添加窗体事件 调用Dialog对象的addWindowListener()方法,设置,重写windowClosing()方法,方法里面调用Dialog对象setVisible(flase),隐藏掉 ? 显示信息 调用Label对象的setText()方法,设置文本 import java.awt.Button; java.awt.Dialog; java.awt.FlowLayout; java.awt.Frame; java.awt.Label; java.awt.TextArea; java.awt.TextField; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.awt.event.WindowAdapter; java.awt.event.WindowEvent; java.io.File; public class FrameDemo { private Frame frame; TextField tf; TextArea ta; Button button; Dialog dialog; Label label; public FrameDemo() { init(); } /** * 初始化 */ void init() { frame = new Frame("测试窗体"); frame.setBounds(300,200,300,400); frame.setLayout(new FlowLayout()); tf = new TextField(20); button = new Button("转到"); ta = new TextArea(30,30); frame.add(button); frame.add(tf); frame.add(ta); dialog=new Dialog(frame,"警告",false); dialog.setBounds(250,100,100); dialog.setLayout( FlowLayout()); label= Label(); dialog.add(label); frame.setVisible(true); addEventAction(); } * 添加事件 addEventAction() { // 退出 frame.addWindowListener( WindowAdapter() { @Override windowClosing(WindowEvent e) { System.exit(0); } }); 对话框的window事件 dialog.addWindowListener( windowClosing(WindowEvent e) { dialog.setVisible( action事件 button.addActionListener( ActionListener() { @Override actionPerformed(ActionEvent e) { String dirName = tf.getText(); File file = File(dirName); if (!file.isDirectory() || !file.exists()) { dialog.setVisible(); label.setText(dirName+"目录不存在"); ta.setText("目录不存在"); return; } String[] files = file.list(); StringBuilder sb = StringBuilder(); for (String name : files) { sb.append(name); sb.append("rn"); } ta.setText(sb.toString()); } }); } * @param args static main(String[] args) { FrameDemo(); } } ? (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |