练习 窗体和对话框Dialog
2017-04-01 01:03
169 查看
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindowDemo
{
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
MyWindowDemo()
{
init();
}
public void init() //不写在构造函数里,因为可以这样被覆盖
{
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());//不指定默认全填充
tf = new TextField(50);
but = new Button("转到");
//TextArea 对象是显示文本的多行区域。可以将它设置为允许编辑或只读。
ta = new TextArea(25,65);//行,列
d = new Dialog(f,"提示信息-self",true);
//又设置提示框
d.setBounds(400,200,200,150);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
//显示窗体前加载一下窗体上的事件
myEvent();
f.setVisible(true);
}
private void myEvent()
{
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
//添加键盘监听
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
});
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
//ta.setText(text);
//System.out.println(text);
//tf.setText("");
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
//封装成一个功能函数,方便调用
private void showDir()
{
//操作谁就去谁那找方法
String dirPath = tf.getText();
//通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory())
{
//清空文本区域
ta.setText("");
String[] names = dir.list();
for(String name : names)
{
//ta.setText(name+"\r\n");
ta.append(name+"\r\n");
}
}
else
{
String info = "您输入的信息:"+dirPath+"错的,重输";
lab.setText(info);
d.setVisible(true);
}
}
public static void main(String[] args)
{
new MyWindowDemo();
}
}
import java.awt.event.*;
import java.io.*;
class MyWindowDemo
{
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
MyWindowDemo()
{
init();
}
public void init() //不写在构造函数里,因为可以这样被覆盖
{
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());//不指定默认全填充
tf = new TextField(50);
but = new Button("转到");
//TextArea 对象是显示文本的多行区域。可以将它设置为允许编辑或只读。
ta = new TextArea(25,65);//行,列
d = new Dialog(f,"提示信息-self",true);
//又设置提示框
d.setBounds(400,200,200,150);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
//显示窗体前加载一下窗体上的事件
myEvent();
f.setVisible(true);
}
private void myEvent()
{
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
//添加键盘监听
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
});
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
//ta.setText(text);
//System.out.println(text);
//tf.setText("");
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
//封装成一个功能函数,方便调用
private void showDir()
{
//操作谁就去谁那找方法
String dirPath = tf.getText();
//通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory())
{
//清空文本区域
ta.setText("");
String[] names = dir.list();
for(String name : names)
{
//ta.setText(name+"\r\n");
ta.append(name+"\r\n");
}
}
else
{
String info = "您输入的信息:"+dirPath+"错的,重输";
lab.setText(info);
d.setVisible(true);
}
}
public static void main(String[] args)
{
new MyWindowDemo();
}
}
相关文章推荐
- GUI__【GUI概述】【布局】【Frame】【事件监听机制】【窗体事件】【按钮】【鼠标事件】【键盘事件】【Dialog对话框】【菜单】
- 窗体 对话框类(Dialog) 文件对话框(FileDialog) 面板(JPanel)
- GUI(练习-列出指定目录内容 )的升级版 有对话框Dialog
- Creating Dialogbased Windows Application (3) / 创建基于对话框的Windows应用程序(三)Checkbox的应用、窗体置顶、设置图标 / VC++, Windows
- Java-GUI(练习-列出指定目录内容)-(对话框Dialog)
- Dialog对话框练习1
- vb.net 教程 3-5 窗体编程 对话框3 ColorDialog & FontDialog
- vb.net 教程 3-5 窗体编程 对话框1 OpenFileDialog & SaveFileDialog
- Java基础--Java---IO流------GUI(布局)、Frame、事件监听机制、窗体事件、Action事件、鼠标事件、对话框Dialog、键盘事件、菜单
- Creating Dialogbased Windows Application (1) / 创建基于对话框的Windows应用程序(一)新建窗体 / VC++, Windows
- 在窗体上放置一个标签、一个按钮、一个fontDialog控件。标签内容改为“烟台大学”。实现功能:1)程序运行时,单击打开字体对话框按钮,可选择字体,并以所选字体作为标签字体。
- js showModalDialog 弹出对话框的简单实例(子窗体)
- Dialog对话框练习2
- AlertDialog提示对话框练习
- 《窗体与对话框的练习——Java第十三周》
- Creating Dialogbased Windows Application (2) / 创建基于对话框的Windows应用程序(二)Button的应用、新建子窗体 / VC++, Windows
- Java基础--Java---IO流------GUI(布局)、Frame、事件监听机制、窗体事件、Action事件、鼠标事件、对话框Dialog、键盘事件、菜单
- Java基础--Java---IO流------GUI(布局)、Frame、事件监听机制、窗体事件、Action事件、鼠标事件、对话框Dialog、键盘事件、菜单
- vb.net 教程 3-5 窗体编程 对话框2 OpenFileDialog & FolderBrowserDialog
- Android UI基础之Dialog对话框的使用