timerdialog.java

来自「java聊天室客户端的源代码」· Java 代码 · 共 118 行

JAVA
118
字号
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.event.*;

public class TimerDialog extends JDialog implements Runnable {

	private JPanel jContentPane = null;
	private JButton okButton = null;
	private JLabel msgLabel = null;
	private Thread t;
	private int time;
	private JLabel iconLabel = null;
	private JFrame fFrame = null;	

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setBounds(new java.awt.Rectangle(85,53,115,28));
			okButton.setText("确   定");
			okButton.setForeground(new java.awt.Color(255,102,102));
			okButton.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 14));
			okButton.setBackground(new java.awt.Color(197,234,252));
			okButton.setSelected(true);
			okButton.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							dispose();
							fFrame.dispose();
						}
					}
					
			);
		}
		return okButton;
	}	
	public TimerDialog(JFrame fatherFrame,boolean model,String inMsg) {
		super(fatherFrame,model);
		initialize(inMsg,fatherFrame);
		try
		{
			t=new Thread(this);
			t.start();
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	
	private void initialize(String inMsg,JFrame inFrame) {
		this.fFrame = inFrame;
		this.setContentPane(getJContentPane());
		msgLabel.setText(inMsg);
		this.setSize(301, 120);
		this.setModal(false);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setResizable(false);
		this.setTitle("Fetion消息窗口");
		this.setVisible(true);		
		this.setLocationRelativeTo(null);
		
	}

	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			iconLabel = new JLabel();
			iconLabel.setIcon(new ImageIcon(getClass().getResource("/pictures/msg.JPG")));
			iconLabel.setLocation(new java.awt.Point(18,13));
			iconLabel.setSize(new java.awt.Dimension(27,27));
			iconLabel.setText("");
			msgLabel = new JLabel();
			msgLabel.setBounds(new java.awt.Rectangle(57,15,218,25));
			msgLabel.setFont(new java.awt.Font("楷体_GB2312", java.awt.Font.BOLD, 14));
			msgLabel.setForeground(java.awt.Color.blue);
			msgLabel.setToolTipText("");
			msgLabel.setText("");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(new java.awt.Color(245,255,255));
			jContentPane.add(getOkButton(), null);
			jContentPane.add(msgLabel, null);
			jContentPane.add(iconLabel, null);
		}
		return jContentPane;
	}
	public void run()
	{
		for(time=10;time>0;time--)
		{			
			okButton.setText("确  定(" + Integer.toString(time) + ")");
			try 
			{
				t.sleep(1000);
			} 
			catch (Exception e) 
			{
				System.out.println("倒计时异常:" + e);
				e.printStackTrace();
			}
		}		
		dispose();
		fFrame.dispose();
	}
	public static void main(String[] args)
	{
		new TimerDialog(new JFrame(),false,"jjjjjjjjjjjjjjjjjjjjjjjjjj");
	}
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?