代码如下:
import java.awt.*;
public class FileDailogTest
{
Frame f = new Frame("File");
// 创建两个文件对话框
FileDialog d1 = new FileDialog(f
, "Pleaae select the file" , FileDialog.LOAD);
FileDialog d2 = new FileDialog(f
, "Where save the file?" , FileDialog.SAVE);
//创建一个ScrollPane容器
ScrollPane sp=new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
//建立一个文本域编辑文件
Panel p=new Panel();
Panel p1=new Panel();
TextArea ts=new TextArea(450,800);
Button b1 = new Button("OpenFile");
Button b2 = new Button("SaveFile");
public void init()
{
b1.addActionListener(e ->
{
d1.setVisible(true);
// 打印出用户选择的文件路径和文件名
System.out.println(d1.getDirectory()
+ d1.getFile());
});
b2.addActionListener(e ->
{
d2.setVisible(true);
// 打印出用户选择的文件路径和文件名
System.out.println(d2.getDirectory()
+ d2.getFile());
});
//吧文本域加入ScrollPane容器中
sp.add(ts);
f.add(sp,BorderLayout.CENTER);
//然后再在panel中使用流布局
p.setLayout(new FlowLayout());
p.add(b1);
p.add(b2);
f.add(p,BorderLayout.SOUTH);//加入panel
//指定窗口的大小
f.setBounds(450,450,450,450);
f.setVisible(true);
}
public static void main(String[] args)
{
new FileDailogTest().init();
}
}