java 自定義確認取消的Dialog對話方塊,10秒之後自動關閉
阿新 • • 發佈:2019-01-11
問題如下:
點選一個按鈕,彈出一個對話方塊,對話方塊裡面包含 確定 和 取消 按鈕
若點選確定,返回值為0,點選取消,返回值為1
若超過10秒不點選,提示視窗自動關閉,返回值為-5
後續可以根據返回值來做各種不同的判斷。
1.TimerTest 主程式,先執行main方法,然後點選按鈕
TimerTest.java
2.自定義的Dialogpackage 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); } }
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; } }