对话框

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. 调试:'Object reference note set to an instance of an object.'

    今天调试代码遇到一个奇怪的问题,每次调试到 var files = new List<string>()这一行代码,总是报错:System.NullReferenceException: ...

  2. SpringBoot引入第三方jar的Bean的三种方式

    在SpringBoot的大环境下,基本上很少使用之前的xml配置Bean,主要是因为这种方式不好维护而且也不够方便. 因此本篇博文也不再介绍Spring中通过xml来声明bean的使用方式. 一.注解 ...

  3. 洛谷 P3688 - [ZJOI2017]树状数组(二维线段树+标记永久化)

    题面传送门 首先学过树状数组的应该都知道,将树状数组方向写反等价于前缀和 \(\to\) 后缀和,因此题目中伪代码的区间求和实质上是 \(sum[l-1...n]-sum[r...n]=sum[l-1 ...

  4. R语言与医学统计图形-【28】ggplot2扩展包ggrepel、ggsci、gganimate、ggpubr

    ggplot2绘图系统--扩展包ggrepel.ggsci.gganimate.ggpubr等 部分扩展包可在CRAN直接下载,有些需借助devtools包从Github下载. 1. ggrepel包 ...

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

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

  6. cat的生产应用

    web日志文件的合并 cat one.log two.log >all.log sort -k 4 all.log   按照第四列进行时间排序

  7. 24-Longest Palindromic Substring-Leetcode

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  8. mysql 中@ 和 @@的区别

    @x 是 用户自定义的变量 (User variables are written as @var_name)@@x 是 global或session变量 (@@global @@session )@ ...

  9. java中接口可以继承接口

    今天阅读别人的代码才发现,接口是可以继承接口的 一个类只能extends一个父类,但可以implements多个接口. 一个接口则可以同时extends多个接口,却不能implements任何接口. ...

  10. Prometheus概述

    Prometheus是什么 首先, Prometheus 是一款时序(time series) 数据库, 但他的功能却并非支部与 TSDB , 而是一款设计用于进行目标 (Target) 监控的关键组 ...