主页 M

java 自定义确认取消的Dialog对话框,10秒之后自动关闭

2017-09-19 网页编程网 网页编程网

问题如下:

点击一个按钮,弹出一个对话框,对话框里面包含确定和取消按钮,若点击确定,返回值为0,点击取消,返回值为1,若超过10秒不点击,提示窗口自动关闭,返回值为-5,后续可以根据返回值来做各种不同的判断。

1.TimerTest 主程序,先运行main方法,然后点击按钮。TimerTest.java

package com.chat.client;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

import com.chat.ui.TimeDialog;
public class TimerTest extends JFrame {
private static final long serialVersionUID = 1L;
    private static JButton button;
    private static TimerTest TimerTest;

	public static void main(String[] args) {
		TimerTest = new TimerTest();
		button = new JButton("按我");
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			   TimeDialog d = new TimeDialog();
			   int result = d.showDialog(TimerTest, "对方想要和你语音是否接受?", 10);// TimerTest是程序主窗口类,弹出的对话框10秒后消失
				System.out.println("===result: "+result);
			}
		});
		
		button.setBounds(2, 5, 80,20);
		TimerTest.getContentPane().setLayout(null);
		TimerTest.getContentPane().add(button);
		TimerTest.setSize(new Dimension(400,200));
		TimerTest.setLocation(500,200);
		TimerTest.setVisible(true);
		TimerTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}

2.自定义的Dialog。TimeDialog.java

package com.chat.ui;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class TimeDialog {
	private String message = null;
    private int secends = 0;
    private JLabel label = new JLabel();
    private JButton confirm,cancel; 
    private JDialog dialog = null;
    int result = -5;
    public int  showDialog(JFrame father, String message, int sec) {
    	
        this.message = message;
        secends = sec;
        label.setText(message);
        label.setBounds(80,6,200,20);
        ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
        confirm = new JButton("接受");
        confirm.setBounds(100,40,60,20);
        confirm.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				result = 0;
				TimeDialog.this.dialog.dispose();
			}
		});
        cancel = new JButton("拒绝");
        cancel.setBounds(190,40,60,20);
        cancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				result = 1;
				TimeDialog.this.dialog.dispose();
			}
		});
        dialog = new JDialog(father, true);
        dialog.setTitle("提示: 本窗口将在"+secends+"秒后自动关闭");
        dialog.setLayout(null);
        dialog.add(label);
        dialog.add(confirm);
        dialog.add(cancel);
        s.scheduleAtFixedRate(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                
                TimeDialog.this.secends--;
                if(TimeDialog.this.secends == 0) {
                    TimeDialog.this.dialog.dispose();
                }else {
                	dialog.setTitle("提示: 本窗口将在"+secends+"秒后自动关闭");
                }
            }
        }, 1, 1, TimeUnit.SECONDS);
        dialog.pack();
        dialog.setSize(new Dimension(350,100));
        dialog.setLocationRelativeTo(father);
        dialog.setVisible(true);
        return result;
    }
}
阅读原文
阅读 5359
123 显示电脑版