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) 程序是为了方便用户使用的, ...
随机推荐
- 查询某个信息下只有一条数据的sql
- x86汇编反编译到c语言之——(1)表达式求值及赋值语句
一. 反编译一种可能的实现方式 我们的目的是将多种平台的汇编如x86,ARM,6502反编译为c语言,所以实现时先将多种汇编转化为 特定虚拟机汇编语言,然后只需要将虚拟机汇编语言反编译为c语言.其中多 ...
- 【程序员翻身计划】Java高性能编程第一章-Java多线程概述
目标 重点: 线程安全的概念 线程通信的方式与应用 reactor线程模型 线程数量的优化 jdk常用命令 Netty框架的作用 难点 java运行的原理 同步关键字的原理 AQS的抽象 JUC的源码 ...
- 什么是H5
H5其实就是HTML5 常说的H5测试,就是移动端web页面,他跟安卓app与IOS app的区别: (1)以往的app是使用的原生系统内核的,相当于直接在系统上操作,是传统意义上的软件,更加稳定 ( ...
- Codeforces 1236F - Alice and the Cactus(期望+分类讨论)
Codeforces 题面传送门 & 洛谷题面传送门 期望好题. 首先拆方差: \[\begin{aligned} &E((x-E(x))^2)\\ =&E(x^2)-2E(x ...
- Atcoder Grand Contest 015 F - Kenus the Ancient Greek(找性质+乱搞)
洛谷题面传送门 & Atcoder 题面传送门 一道难度 Au 的 AGC F,虽然看过题解之后感觉并不复杂,但放在现场确实挺有挑战性的. 首先第一问很简单,只要每次尽量让"辗转相除 ...
- Codeforces 521D - Shop(贪心)
Codeforces 题目传送门 & 洛谷题目传送门 一道不算太难的贪心,可惜又没自己想出来,显然省选之后我的能力呈 \(y=-1145141919810192608179998244353x ...
- Shell 变量嵌套
实现:eval 1 a="indv1" 2 indv1="Sus1" 3 4 eval tmp='$'$a 5 echo $tmp //这里 echo 返回值为 ...
- Linux升级命令yum upgrade和yum update的区别
Linux升级命令有两个分别是yum upgrade和yum update, 这个两个命令是有区别的: yum -y update 升级所有包同时也升级软件和系统内核 yum -y upgrade 只 ...
- Python中的随机采样和概率分布(二)
在上一篇博文<Python中的随机采样和概率分布(一)>(链接:https://www.cnblogs.com/orion-orion/p/15647408.html)中,我们介绍了Pyt ...