对话框

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 + "\n");
}
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 + "\n");
}
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);
}
}
 

最新文章

  1. Java--FutureTask原理与使用(FutureTask可以被Thread执行,可以被线程池submit方法执行,并且可以监控线程与获取返回值)
  2. ANDROID学习之路 转
  3. SlimerJS – Web开发人员可编写 JS 控制的浏览器
  4. Myeclipse for Mac快捷键
  5. WINDOWS下用脚本运行redis和mongodb
  6. 无锁编程(六) - seqlock(顺序锁)
  7. Oracle RAC OCR 的备份与恢复
  8. php安装ecshop
  9. JAVA小项目之五子棋
  10. java transient关键字和transaction的区别
  11. python 嵌套字典比较值,取值
  12. AdobeFlashBuilder还不如AdobeFlashProfessional写actionscript体验好
  13. Redis in Python:HyperLogLog(pfadd、pfcount、pfmerge)
  14. 并发系列(6)之 ThreadPoolExecutor 详解
  15. 键盘keyCode值
  16. 利用matplotlib库和numpy库画数学图形
  17. 【刷题】BZOJ 2069 [POI2004]ZAW
  18. 如何用html把文本框外观格式设为只显示底部的横线
  19. Django之模版层-语法:标签 {% %}
  20. Jmeter—实现识别验证码登录

热门文章

  1. CreateProcess error=206, 文件名或扩展名太长。
  2. 待办事项-redis
  3. Nocalhost 为 KubeSphere 提供更强大的云原生开发环境
  4. NOI2021 去不了记
  5. Atcoder Regular Contest 096 C - Everything on It(组合数学)
  6. Tarjan 的一些板子
  7. mGWAS研究思路
  8. CPU大小端模式及转换
  9. UE4 C++工程以Game模式启动
  10. 一个简单的BypassUAC编写