java 图形化小工具Abstract Window Toolit 常用组件:对话框Dialog FileDialog
对话框
- (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);
}
}
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);
}
}
java 图形化小工具Abstract Window Toolit 常用组件:对话框Dialog FileDialog的更多相关文章
- java 图形化小工具Abstract Window Toolit 常用组件
基本组件 Button: 按钮,可接受单击操作 Canvas: 用于绘图的画布 Checkbox: 复选框组(也可变成单选框组件) CheckboxGroup: 用于将多个checkbox组件组合成一 ...
- java 图形化小工具Abstract Window Toolit
老掉牙的历史 Java1.0在发布的时候,就为我们提供了GUI操作的库,这个库系统在所有的平台下都可以运行,这套基本的类库被称作抽象窗口工具集(Abstract Window Toolit),简称 ...
- java 图形化小工具Abstract Window Toolit 事件处理
事件处理设计到了三个对象: EventSource(事件源):事件发生的场所,通常就是各个组件,例如按钮.窗口,菜单等. Event (事件封装了GUI组件上发生的特定事情(通常就是一次用户操作).如 ...
- java 图形化小工具Abstract Window Toolit :画笔Graphics,画布Canvas(),弹球小游戏
画笔Graphics Java中提供了Graphics类,他是一个抽象的画笔,可以在Canvas组件(画布)上绘制丰富多彩的几何图和位图. Graphics常用的画图方法如下: drawLine(): ...
- java 图形化小工具Abstract Window Toolit ImageIO缩放图片,添加水印
实现步骤: 读取图像Image src = ImageIO.read 创建目标图像BufferedImage distImage = new BufferedImage(dstWidth, dstHe ...
- java 图形化小工具Abstract Window Toolit 画笔 处理位图
具体编程来处理位图 知识点: 实现逻辑: 画板上的图片 new BufferedImage(canvasWidth,canvasHeight,BufferedImage.TYPE_INT_BGR); ...
- java 图形化小工具Abstract Window Toolit 菜单项
AWT 中的菜单由如下几个类组合而成 MenuBar: 菜单条,菜单的容器. Menu: 菜单组件,菜单项的容器,它也是Menultem的子类,所以可作为菜单项使用. PopupMenu: 上下文菜单 ...
- java 图形化小工具Abstract Window Toolit ;布局管理器FlowLayout流式布局;BorderLayout边界布局;GridLayout网格布局;CardLayou重叠卡片布局;BoxLayout方框布局;绝对定位
1.FlowLayout流式布局管理器: FlowLayout布局管理器中,组件像水流一样向某方向流动(排列),遇到障碍(边界)就折回,重头开始排列 .在默认情况下,FlowLayout局管理器从左向 ...
- 转:二十七、Java图形化界面设计——容器(JFrame)
转:http://blog.csdn.net/liujun13579/article/details/7756729 二十七.Java图形化界面设计——容器(JFrame) 程序是为了方便用户使用的, ...
随机推荐
- 『与善仁』Appium基础 — 12、Appium的安装详解
目录 (一)Appium server安装 方式一:(桌面方式:推荐) 1.Appium Desktop下载 2.Appium Desktop安装 3.Appium Desktop使用 方式二:(No ...
- Linux远程软件
Xhell6:Linux的终端模拟软件 1>安装并破解:解压.破解(运行两个.bat文件).启动(点击Xshell.exe文件) 2>连接远端Linux系统: 创建会话:点击连接,在常规框 ...
- Mysql-多个left join 计算逻辑
单个left join: (1)一对一:结果表的行数=左表行数 (2)一对多:结果表的行数>左表行数 多个left join: (0)多个left join由上到下,依次生成查询表,原理同单个l ...
- Python压缩&解压缩
Python中常用的压缩模块有zipfile.tarfile.gzip 1.zipfile模块的简单使用 import zipfile # 压缩 z1 = zipfile.ZipFile('zip_t ...
- 02 Windows安装C语言开发工具CodeBlocks
CodeBlocks安装 使用微信扫码关注微信公众号,并回复:"C语言环境",免费获取下载链接! 1.卸载CodeBlocks(电脑未装此软件,跳过) 进入目录:C:\Pro ...
- C++ 中的多重继承的问题
如何正确使用C++多重继承 BY R12F · PUBLISHED 2011年06月17日 · UPDATED 2012年03月11日 原创文章,转载请注明:转载自Soul Apogee本文链接地 ...
- 12-gauge/bore shotgun
12-gauge/bore shotgun不是弹夹(magazine)容量为12发的霰(xian)弹枪.[LDOCE]gauge - a measurement of the width or thi ...
- A Child's History of England.33
To strengthen his power, the King with great ceremony betrothed his eldest daughter Matilda, then a ...
- Spark(四)【RDD编程算子】
目录 测试准备 一.Value类型转换算子 map(func) mapPartitions(func) mapPartitions和map的区别 mapPartitionsWithIndex(func ...
- d3动态坐标轴
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...