java 图形化小工具Abstract Window Toolit 常用组件:对话框Dialog FileDialog

对话框

Dialog是Window类的子类,是1个容器类,属于特殊组件,对话框是可以独立存在的顶级窗口,因此用法与普通窗口的用法几乎完全一样。但对话框有如下两点需要注意。
  • (1),对话框通常依赖于其他窗口,就是通常有parent窗口。
  • (2),对话框有非模式(non-model)和模式(modal)两种,模式(modal)方式当某个模式对话框被打开之后,该模式对话框总是位于它依赖的窗口之上:在模式对话框被关闭之前,它依赖的窗口无法获得焦点。

import java.awt.*;
import java.awt.event.ActionListener;

/**
 * @ClassName DialogTest
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/5/5.
 */
public class DialogTest {
    public static void main(String[] args) {
        Frame frame = new Frame("测试新对话框");

        Dialog dia1 = new Dialog(frame,"非模式对话框",false);
        dia1.setBounds(100,100,200,300);
        Dialog dia2 = new Dialog(frame,"模式对话框",true);
        dia2.setBounds(100,100,200,300);
        ActionListener btnClickListener = e -> {
            switch (e.getActionCommand()) {
                case "打开非模式对话框":
                    dia1.setVisible(true);
                    break;
                case "打开模式对话框":
                    dia2.setVisible(true);
                    break;
            }
        };
        Button btn1 = new Button("打开非模式对话框");
        btn1.addActionListener(btnClickListener);
        Button btn2 = new Button("打开模式对话框");
        btn2.addActionListener(btnClickListener);
        frame.setLocation(400,300);
        frame.add(btn1);frame.add(btn2,BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
}
Dialog有一个子类叫做FileDialog,可以用来选择打开或者保存文件。
示例代码:
新建会话框选择文件,以字符流方式读取文件内容。再将读取的数据另存为文件
import java.awt.*;
import java.io.*;

/**
 * @ClassName FileDialogTest
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/5/6.
 */
public class FileDialogTest {
    public static void main(String[] args) {
        Frame frame = new Frame("打开/保存文件窗口");
        Button openFileButton = new Button("打开文件");
        Button saveFileButton = new Button("保存到文件");
        //选择文件对话框
        FileDialog openFileDialog = new FileDialog(frame,"选择文件",FileDialog.LOAD);
        //保存文件对话框
        FileDialog saveFileDialog = new FileDialog(frame,"保存文件",FileDialog.SAVE);
        //打开文件对话框事件
        StringBuffer sb = new StringBuffer(); //读取文件存放字符数据的变量
        openFileButton.addActionListener(e ->{
            openFileDialog.setVisible(true);
            String openFilePath = openFileDialog.getDirectory() + openFileDialog.getFile();
            //用字符流、缓冲流方式打开读取文件,存放为String字符串变量中
            try(BufferedReader br = new BufferedReader(new FileReader(openFilePath))) {
                String line = null;
                while ((line = br.readLine()) != null){
                    sb.append(line + "
");
                }
                System.out.println(sb);
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        //保存文件对话框事件
        saveFileButton.addActionListener(e ->{
            saveFileDialog.setVisible(true);
            String saveFilePath = saveFileDialog.getDirectory() + saveFileDialog.getFile();
            System.out.println(saveFilePath);
            if (sb.length() != 0){
                try (BufferedWriter bw = new BufferedWriter(new FileWriter(saveFilePath))){
                    bw.write(sb.toString());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });

        frame.add(openFileButton);frame.add(saveFileButton,BorderLayout.SOUTH);
        frame.setLocation(400,300);
        frame.pack();
        frame.setVisible(true);
    }
}

teacher版本:原子化操作变量

AtomicReference<String> fileContent = new AtomicReference<>();
import java.awt.*;
import java.io.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName FileDialogTest
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/5/6.
 */
public class FileDialogTestTeacher {
    public static void main(String[] args) {
        Frame frame = new Frame("打开/保存文件窗口");
        Button openFileButton = new Button("打开文件");
        Button saveFileButton = new Button("保存到文件");
        //选择文件对话框
        FileDialog openFileDialog = new FileDialog(frame,"选择文件",FileDialog.LOAD);
        //保存文件对话框
        FileDialog saveFileDialog = new FileDialog(frame,"保存文件",FileDialog.SAVE);
        //打开文件对话框事件
        AtomicReference<String> fileContent = new AtomicReference<>();
        openFileButton.addActionListener(e ->{
            openFileDialog.setVisible(true);
            String openFilePath = openFileDialog.getDirectory() + openFileDialog.getFile();
            //用字符流、缓冲流方式打开读取文件,存放为String字符串变量中
            StringBuffer sb = new StringBuffer(); //读取文件存放字符数据的变量
            try(BufferedReader br = new BufferedReader(new FileReader(openFilePath))) {
                String line = null;
                while ((line = br.readLine()) != null){
                    sb.append(line + "
");
                }
                System.out.println(sb);
                fileContent.set(sb.toString());
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        //保存文件对话框事件
        saveFileButton.addActionListener(e ->{
            saveFileDialog.setVisible(true);
            String saveFilePath = saveFileDialog.getDirectory() + saveFileDialog.getFile();
            System.out.println(saveFilePath);
            if (fileContent.get() != null){
                try (BufferedWriter bw = new BufferedWriter(new FileWriter(saveFilePath))){
                    bw.write(fileContent.get());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            else {
                System.out.println("您还没有打开/读取文件内容");
            }
        });

        frame.add(openFileButton);frame.add(saveFileButton,BorderLayout.SOUTH);
        frame.setLocation(400,300);
        frame.pack();
        frame.setVisible(true);
    }
}
 
原文地址:https://www.cnblogs.com/zhangmingda/p/14735072.html