对话框

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);
}
}
 

java 图形化小工具Abstract Window Toolit 常用组件:对话框Dialog FileDialog的更多相关文章

  1. java 图形化小工具Abstract Window Toolit 常用组件

    基本组件 Button: 按钮,可接受单击操作 Canvas: 用于绘图的画布 Checkbox: 复选框组(也可变成单选框组件) CheckboxGroup: 用于将多个checkbox组件组合成一 ...

  2. java 图形化小工具Abstract Window Toolit

      老掉牙的历史 Java1.0在发布的时候,就为我们提供了GUI操作的库,这个库系统在所有的平台下都可以运行,这套基本的类库被称作抽象窗口工具集(Abstract Window Toolit),简称 ...

  3. java 图形化小工具Abstract Window Toolit 事件处理

    事件处理设计到了三个对象: EventSource(事件源):事件发生的场所,通常就是各个组件,例如按钮.窗口,菜单等. Event (事件封装了GUI组件上发生的特定事情(通常就是一次用户操作).如 ...

  4. java 图形化小工具Abstract Window Toolit :画笔Graphics,画布Canvas(),弹球小游戏

    画笔Graphics Java中提供了Graphics类,他是一个抽象的画笔,可以在Canvas组件(画布)上绘制丰富多彩的几何图和位图. Graphics常用的画图方法如下: drawLine(): ...

  5. java 图形化小工具Abstract Window Toolit ImageIO缩放图片,添加水印

    实现步骤: 读取图像Image src = ImageIO.read 创建目标图像BufferedImage distImage = new BufferedImage(dstWidth, dstHe ...

  6. java 图形化小工具Abstract Window Toolit 画笔 处理位图

    具体编程来处理位图 知识点: 实现逻辑: 画板上的图片 new BufferedImage(canvasWidth,canvasHeight,BufferedImage.TYPE_INT_BGR); ...

  7. java 图形化小工具Abstract Window Toolit 菜单项

    AWT 中的菜单由如下几个类组合而成 MenuBar: 菜单条,菜单的容器. Menu: 菜单组件,菜单项的容器,它也是Menultem的子类,所以可作为菜单项使用. PopupMenu: 上下文菜单 ...

  8. java 图形化小工具Abstract Window Toolit ;布局管理器FlowLayout流式布局;BorderLayout边界布局;GridLayout网格布局;CardLayou重叠卡片布局;BoxLayout方框布局;绝对定位

    1.FlowLayout流式布局管理器: FlowLayout布局管理器中,组件像水流一样向某方向流动(排列),遇到障碍(边界)就折回,重头开始排列 .在默认情况下,FlowLayout局管理器从左向 ...

  9. 转:二十七、Java图形化界面设计——容器(JFrame)

    转:http://blog.csdn.net/liujun13579/article/details/7756729 二十七.Java图形化界面设计——容器(JFrame) 程序是为了方便用户使用的, ...

随机推荐

  1. 力扣 - 剑指 Offer 10- I. 斐波那契数列

    题目 剑指 Offer 10- I. 斐波那契数列 思路1(递归 / 自顶向下) 这题是很常见的一道入门递归题,可以采用自顶向下的递归方法,比如我们要求第n个位置的值,根据斐波那契数列的定义fib(n ...

  2. 【.NET 与树莓派】MPD 的 Mini-API 封装

    在前面的水文中,一方面,老周向各位同学介绍了通过 TCP 连接来访问 MPD 服务:另一方面,也简单演示了 ASP.NET Core 的"极简 API"(Mini API).本篇老 ...

  3. Apache发布支持Java EE微服务的Meecrowave服务器

    Apache OpenWebBeans团队希望通过使服务器适应用户来消除复杂性.所以,该团队发布了Apache Meecrowave项目1.0版. Apache Meecrowave是一款小型服务器, ...

  4. AT4168 [ARC100C] Or Plus Max

    从\(whk\)回来了. 考虑我们需要维护一个子集的信息. 对于二进制的子集信息维护有一个很经典的操作: 高维前缀和. AT4168 [ARC100C] Or Plus Max // Problem: ...

  5. 洛谷 P7620 - CF1431J Zero-XOR Array(状压 dp)

    洛谷题面传送门 首先显然题目等价于求有多少个长度 \(n-1\) 的序列 \(b\) 满足 \(a_i\le b_i\le a_{i+1}\),满足 \(b_1\oplus b_2\oplus\cdo ...

  6. CF1466G Song of the Sirens

    题目传送门 题意简述:给出 \(n,s_0,t\ (n=|t|)\),定义 \(s_i=s_{i-1}+t_i+s_{i-1}\).多次询问给出 \(k,m\),求 \(m\) 在 \(s_k\) 中 ...

  7. Linux下脚本文件第一行的作用

    Linux下脚本文件第一行的作用 在Linux/Unix系统中,你可以在脚本hello.py顶部添加以下命令让Python脚本可以像SHELL脚本一样可直接执行: #! /usr/bin/env py ...

  8. BaiduPCS-Go----百度云下载工具

    1.网页登录百度网盘:https://pan.baidu.com/2.百度输入法生成:http://pcs.baidu.com/rest/2.0/pcs/file?app_id=265486& ...

  9. 毕业设计之LNP+DISCUZ +分离的数据库操作

    环境介绍: CentOS6.9最小化安装 https://nginx.org/download/nginx-1.16.1.tar.gz https://www.php.net/distribution ...

  10. CSS3实现字体描边

    CSS3实现字体描边的两种方法 -webkit-text-stroke: 1px #fff;:不建议,向内描边,字体颜色变细,效果不佳: 用box-shadow模拟描边,向外描边,保留字体粗细,赞! ...