• java swing JDialog 和 java.util.concurrent的使用


    参考链接:

    Java-Swing的JFrame的一些插件使用详解

    java swing JDialog 使用

    ScheduledExecutorService定时周期执行指定的任务

    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,
                    long initialDelay,
                    long period,
                    TimeUnit unit);

    command:执行线程
    initialDelay:初始化延时
    period:两次开始执行最小间隔时间
    unit:计时单位

    示例:

    /**
     * 以固定周期频率执行任务
     */
    public static void executeFixedRate() {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
        executor.scheduleAtFixedRate(
                new EchoServer(),
                0,
                100,
                TimeUnit.MILLISECONDS);
    }

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

    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                    long initialDelay,
                    long delay,
                    TimeUnit unit);

    command:执行线程
    initialDelay:初始化延时
    period:前一次执行结束到下一次执行开始的间隔时间(间隔执行延迟时间)
    unit:计时单位

    示例:

    /**
     * 以固定延迟时间进行执行
     * 本次任务执行完成后,需要延迟设定的延迟时间,才会执行新的任务
     */
    public static void executeFixedDelay() {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
        executor.scheduleWithFixedDelay(
                new EchoServer(),
                0,
                100,
                TimeUnit.MILLISECONDS);
    }

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

    主函数类:

     1 import java.awt.Dimension;
     2 import java.awt.event.ActionEvent;
     3 import java.awt.event.ActionListener;
     4 
     5 import javax.swing.JButton;
     6 import javax.swing.JFrame;
     7 
     8 
     9 
    10 public class TimerTest extends JFrame {
    11 
    12     /**
    13      * 
    14      */
    15     private static final long serialVersionUID = 1L;
    16     private static JButton button;
    17     private static TimerTest TimerTest;
    18 
    19     public static void main(String[] args) {
    20         TimerTest = new TimerTest();
    21         button = new JButton("按我");
    22         button.addActionListener(new ActionListener() {
    23             
    24             @Override
    25             public void actionPerformed(ActionEvent e) {
    26                TimeDialog d = new TimeDialog();
    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                   * 这边设置布局为 null
    38          */
    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
     1 import java.awt.Dimension;
     2 import java.awt.event.ActionEvent;
     3 import java.awt.event.ActionListener;
     4 import java.util.concurrent.Executors;
     5 import java.util.concurrent.ScheduledExecutorService;
     6 import java.util.concurrent.TimeUnit;
     7 
     8 import javax.swing.JButton;
     9 import javax.swing.JDialog;
    10 import javax.swing.JFrame;
    11 import javax.swing.JLabel;
    12 
    13 public class TimeDialog {
    14     private String message = null;  
    15     private int secends = 0;  
    16     private JLabel label = new JLabel();  
    17     private JButton confirm,cancel;   
    18     private JDialog dialog = null;  
    19     int result = -5;  
    20     public int  showDialog(JFrame father, String message, int sec) 
    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(new ActionListener() {              
    30             @Override  //这个是事件触发函数
    31             public void actionPerformed(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(new ActionListener() {  
    40               
    41             @Override  
    42             public void actionPerformed(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(new Runnable() {  
    58             @Override  
    59             public void run() {  
    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         return result;  
    76         
    77     }
    78 
    79 }
    View Code
  • 相关阅读:
    scss-变量分隔符
    scss-声明变量与引用
    scss-入门
    关于动画框架类注意的点点滴滴03
    关于动画框架类注意的点点滴滴02
    关于动画框架类注意的点点滴滴01
    简单的form表单
    简易自定义下拉菜单 与简易默认下拉html片段
    有边框的简易表格
    css模仿表格 居中
  • 原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/14051623.html
Copyright © 2020-2023  润新知