效果图
这个控件还可以有很多种窗口 , 大家可以自己去看文档或查询百度.
重要步骤
创建:
FileDialog saveFile = new FileDialog(父窗口 , "标题" , FileDialog.SAVE(类型) );
显示:
saveFile.show(); //直接调用show()方法即可
代码笔记
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* FileDialog的运用
* @author Administrator
*
*/
public class FileDialogTest extends JFrame {
//创建两个FileDialog对象
FileDialog saveFile = new FileDialog(this , "请选择您要保存的文件:" , FileDialog.SAVE );
FileDialog loadFile = new FileDialog(this , "请选择您要打开的文件:" , FileDialog.LOAD );
//创建两个按钮等下用来打开窗口
JButton openSave = new JButton("打开保存窗口!" );
JButton openLoad = new JButton("打开加载窗口!" );
JPanel panel_box = new JPanel();
public FileDialogTest() {
//设置界面布局和大小
panel_box.setLayout(new BorderLayout() );
this.setContentPane(panel_box );
this.setTitle("FileDialog的应用!" );
this.setBounds(200 , 200 , 300 , 100 );
//将两个按钮添加到界面上
panel_box.add(openSave , BorderLayout.NORTH );
panel_box.add(openLoad , BorderLayout.SOUTH );
//绑定点击事件 , 当点击时就显示FileDialog
openSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
saveFile.show();
}
});
openLoad.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadFile.show();
}
});
//设置窗口可见和可以被关闭
this.setVisible(true );
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
}
public static void main(String[] args) {
new FileDialogTest();
}
}