dialog与java_java swing JDialog 和 java.util.concurrent的使用

本文介绍了如何在Java Swing中使用JDialog创建对话框,包括不同构造函数的应用和模态设置。同时,文章讲解了java.util.concurrent包中的scheduleAtFixedRate方法,展示了如何结合JDialog实现定时关闭对话框的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考链接:

swing JDialog

创建对话框窗口的主要类。可以使用此类创建自定义的对话框,或者调用 JOptionPane 中的多个类方法来创建各种标准对话框。有关创建对话框的信息,请参阅 The Java Tutorial 中的 How to Make Dialogs 一节。

JFrame 有一个 Content Pane,窗口能显示的所有组件都是添加在这个 Content Pane 中

JDialog 组件包含一个 JRootPane 作为其唯一子组件。contentPane 应该是所有 JDialog 子组件的父级。为了方便使用 add 及其变体,已经重写了remove 和 setLayout,以在必要时将其转发到 contentPane。这意味着可以编写:

dialog.add(child);

setLayout(布局管理器)

各容器都有默认的布局管理,见下表:

容器默认布局方式

顶层容器

JFrame

BorderLayout(边界布局)

顶层容器

JDialog

BorderLayout(边界布局)

顶层容器

JApplet

JApplet

中间容器

JPanel

FlowLayout(流式布局)

更多内容可见:https://blog.csdn.net/qq_38341596/article/details/78800646

JDialog的构造函数

JDialog():建立一个non-modal的对话框,没有title也不属于任何事件窗口组件。

JDialog(Dialog owner):建立一个属于Dialog组件的对话框,为non-modal形式,也没有title.

JDialog(Dialog owner,Boolean modal):建立一个属于Dialog组件的对话框,可决定modal形式,但没有title.

JDialog(Dialog owner,String title):建立一个属于Dialog组件的对话框,为non-modal形式,对话框上有title.

JDialog(Dialog owner,String title,Boolean modal):建立一个属于Dialog组件的对话框,可决定modal形式,且对话框上有 title.

JDialog(Frame owner):建立一个属于Frame组件的对话框,为non-modal形式,也没有title.

JDialog(Frame owner,Boolean modal):建立一个属于Frame组件的对话框,可决定modal形式,但没有title.

JDialog(Frame owner,String title):建立一个属于Frame组件的对话框,为non-modal形式,对话框上有title.

JDialog(Frame owner,String title,Boolean modal):建立一个属于Frame组件的对话框,可决定modal形式,且对话框上有title. 经常用这个构造函数。

上面所说的modal是一种对话框操作模式,当modal为true时,代表用户必须结束对话框才能回到原来所属的窗口。当modal为 false时,代表对话框与所属窗口可以互相切换,彼此之间在操作上没有顺序性。

一般而言对话框都会依附在某个窗口上,例如JFrame或JDialog,原因在于对话框通常是一个程序运行的过程中与用户互动的中 间过程,在使用JDialog上跟JFrame非常相似,由上面的JDialog层次结构图中你可以发现,JDialog是继承AWT的Dialog类而来,因 此JDialog为一个Heavyweight组件。要加入组件到JDialog上与JFrame是一样的,你必须先取得JDialog的ContentPane,然后再把组 件加到此ContentPane中,JDialog默认的版面管理器是BorderLayout.

除此之外,你还可以使用JOptionPane。当你在使用 JOptionPane时,系统会自动产生JDialog组件,并将JOptionPane的内容放入JDialog的ContentPane中,而这些均由系统在背后自动 运行,并不需要由我们介入。

我们下面没有用到它,故不过多叙述,相关使用示例见:JOptionPane

scheduleAtFixedRate和scheduleWithFixedDelay方法定义

接口scheduleAtFixedRate原型定义及参数说明

当执行任务的时间大于我们指定的间隔时间时,它并不会在指定间隔时开辟一个新的线程并发执行这个任务。而是等待该线程执行完毕。

public ScheduledFuture>scheduleAtFixedRate(Runnable command,longinitialDelay,longperiod,

TimeUnit unit);

command:执行线程

initialDelay:初始化延时

period:两次开始执行最小间隔时间

unit:计时单位

示例:

/*** 以固定周期频率执行任务*/

public static voidexecuteFixedRate() {

ScheduledExecutorService executor= Executors.newScheduledThreadPool(1);

executor.scheduleAtFixedRate(newEchoServer(),0,100,

TimeUnit.MILLISECONDS);

}

接口scheduleWithFixedDelay原型定义及参数说明

public ScheduledFuture>scheduleWithFixedDelay(Runnable command,longinitialDelay,longdelay,

TimeUnit unit);

command:执行线程

initialDelay:初始化延时

period:前一次执行结束到下一次执行开始的间隔时间(间隔执行延迟时间)

unit:计时单位

示例:

/*** 以固定延迟时间进行执行

* 本次任务执行完成后,需要延迟设定的延迟时间,才会执行新的任务*/

public static voidexecuteFixedDelay() {

ScheduledExecutorService executor= Executors.newScheduledThreadPool(1);

executor.scheduleWithFixedDelay(newEchoServer(),0,100,

TimeUnit.MILLISECONDS);

}

给出一个由JDialog和scheduleAtFixedRate写的示例程序

主函数类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 importjava.awt.Dimension;2 importjava.awt.event.ActionEvent;3 importjava.awt.event.ActionListener;4

5 importjavax.swing.JButton;6 importjavax.swing.JFrame;7

8

9

10 public class TimerTest extendsJFrame {11

12 /**

13 *14 */

15 private static final long serialVersionUID = 1L;16 private staticJButton button;17 private staticTimerTest TimerTest;18

19 public static voidmain(String[] args) {20 TimerTest = newTimerTest();21 button = new JButton("按我");22 button.addActionListener(newActionListener() {23

24 @Override25 public voidactionPerformed(ActionEvent e) {26 TimeDialog d = newTimeDialog();27 int result = d.showDialog(TimerTest, "对方想要和你语音是否接受?", 10);//TimerTest是程序主窗口类,弹出的对话框10秒后消失

28 System.out.println("===result: "+result);29 }30 });31 /*这个方法定义了组件的位置。32 * setBounds(x, y, width, height)33 * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。34 */

35 button.setBounds(2, 5, 80,20);36 /*布局部分我们这边不多做介绍37 * 这边设置布局为 null38 */

39 TimerTest.getContentPane().setLayout(null);40 TimerTest.getContentPane().add(button);41 TimerTest.setSize(new Dimension(400,200));42 TimerTest.setLocation(500,200);43 TimerTest.setVisible(true);44 TimerTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);45

46 }47

48 }

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 importjava.awt.Dimension;2 importjava.awt.event.ActionEvent;3 importjava.awt.event.ActionListener;4 importjava.util.concurrent.Executors;5 importjava.util.concurrent.ScheduledExecutorService;6 importjava.util.concurrent.TimeUnit;7

8 importjavax.swing.JButton;9 importjavax.swing.JDialog;10 importjavax.swing.JFrame;11 importjavax.swing.JLabel;12

13 public classTimeDialog {14 private String message = null;15 private int secends = 0;16 private JLabel label = newJLabel();17 privateJButton confirm,cancel;18 private JDialog dialog = null;19 int result = -5;20 public int showDialog(JFrame father, String message, intsec)21 {22 this.message =message;23 secends =sec;24 label.setText(message);25 label.setBounds(80,6,200,20);26 ScheduledExecutorService s =Executors.newSingleThreadScheduledExecutor();27 confirm = new JButton("接受");28 confirm.setBounds(100,40,60,20);29 confirm.addActionListener(newActionListener() {30 @Override //这个是事件触发函数

31 public voidactionPerformed(ActionEvent e) {32 result = 0;33 //dispose只是关闭你的图形资源,而没有关闭进程

34 TimeDialog.this.dialog.dispose();35 }36 });37 cancel = new JButton("拒绝");38 cancel.setBounds(190,40,60,20);39 cancel.addActionListener(newActionListener() {40

41 @Override42 public voidactionPerformed(ActionEvent e) {43 result = 1;44 TimeDialog.this.dialog.dispose();45 }46 });47 //JDialog(Frame owner,Boolean modal):建立一个属于Frame组件的对话框,可决定modal形式,但没有title.48 //当modal为true时,代表用户必须结束对话框才能回到原来所属的窗口。当modal为 false时,49 //代表对话框与所属窗口可以互相切换,彼此之间在操作上没有顺序性。

50 dialog = new JDialog(father, true);51 dialog.setTitle("提示: 本窗口将在"+secends+"秒后自动关闭");52 dialog.setLayout(null);53 dialog.add(label);54 dialog.add(confirm);55 dialog.add(cancel);56 //让程序定时执行

57 s.scheduleAtFixedRate(newRunnable() {58 @Override59 public voidrun() {60 //TODO Auto-generated method stub

61 TimeDialog.this.secends--;62 if(TimeDialog.this.secends == 0) {63 TimeDialog.this.dialog.dispose();64 }else{65 dialog.setTitle("提示: 本窗口将在"+secends+"秒后自动关闭");66 //dialog.setTitle("加上我就覆盖掉上面啦!");

67 }68 }69 }, 1, 1, TimeUnit.SECONDS); //SECONDS这是计时单位70 //最后调用新建的JDialog的pack和setVisual method去显示对话框。

71 dialog.pack();72 dialog.setSize(new Dimension(350,100));73 dialog.setLocationRelativeTo(father);74 dialog.setVisible(true);75 returnresult;76

77 }78

79 }

View Code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值