import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Event;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.KeyStroke;
import java.awt.Point;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.Toolkit;
import java.awt.Insets;
import java.awt.Color;
import java.awt.Font;
public class CountDown {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="261,21"
private JPanel mainPanel = null;
private JPanel northPanel = null;
private JPanel centerPanel = null;
private JLabel hoursLeftLabel = null;
private JTextField hoursLeftTextField = null;
private JLabel minutesTimeLabel = null;
private JTextField minutesLeftTextField = null;
private JLabel secondsLeftLabel = null;
private JTextField secondsLeftTextField = null;
private JPanel southPanel = null;
private JButton goButton = null;
private JLabel timeLeftjLabel = null;
private Counter counter = new Counter();
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(370, 130);
jFrame.setResizable(false);
jFrame.setContentPane(getMainPanel());
jFrame.setTitle("THE FINAL COUNTDOWN by Djordje Popovic");
jFrame.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2,Toolkit.getDefaultToolkit().getScreenSize().height/2);
}
return jFrame;
}
/**
* This method initializes mainPanel
*
* @return javax.swing.JPanel
*/
private JPanel getMainPanel() {
if (mainPanel == null) {
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(getNorthPanel(), BorderLayout.NORTH);
mainPanel.add(getCenterPanel(), BorderLayout.CENTER);
mainPanel.add(getSouthPanel(), BorderLayout.SOUTH);
}
return mainPanel;
}
/**
* This method initializes northPanel
*
* @return javax.swing.JPanel
*/
private JPanel getNorthPanel() {
if (northPanel == null) {
secondsLeftLabel = new JLabel();
secondsLeftLabel.setText("Seconds:");
minutesTimeLabel = new JLabel();
minutesTimeLabel.setText("Minutes: ");
hoursLeftLabel = new JLabel();
hoursLeftLabel.setText("Hours:");
northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(hoursLeftLabel, null);
northPanel.add(getHoursLeftTextField(), null);
northPanel.add(minutesTimeLabel, null);
northPanel.add(getMinutesLeftTextField(), null);
northPanel.add(secondsLeftLabel, null);
northPanel.add(getSecondsLeftTextField(), null);
}
return northPanel;
}
/**
* This method initializes centerPanel
*
* @return javax.swing.JPanel
*/
private JPanel getCenterPanel() {
if (centerPanel == null) {
timeLeftjLabel = new JLabel();
timeLeftjLabel.setText("TIME LEFT");
timeLeftjLabel.setForeground(new Color(102, 255, 102));
timeLeftjLabel.setBackground(Color.black);
timeLeftjLabel.setFont(new Font("DialogInput", Font.BOLD, 18));
timeLeftjLabel.setHorizontalAlignment(SwingConstants.CENTER);
centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
centerPanel.setBackground(Color.black);
centerPanel.add(timeLeftjLabel, BorderLayout.NORTH);
}
return centerPanel;
}
/**
* This method initializes hoursLeftTextField
*
* @return javax.swing.JTextField
*/
private JTextField getHoursLeftTextField() {
if (hoursLeftTextField == null) {
hoursLeftTextField = new JTextField();
hoursLeftTextField.setPreferredSize(new Dimension(20, 20));
hoursLeftTextField.setText("0");
}
return hoursLeftTextField;
}
/**
* This method initializes minutesLeftTextField
*
* @return javax.swing.JTextField
*/
private JTextField getMinutesLeftTextField() {
if (minutesLeftTextField == null) {
minutesLeftTextField = new JTextField();
minutesLeftTextField.setPreferredSize(new Dimension(20, 20));
minutesLeftTextField.setText("0");
}
return minutesLeftTextField;
}
/**
* This method initializes secondsLeftTextField
*
* @return javax.swing.JTextField
*/
private JTextField getSecondsLeftTextField() {
if (secondsLeftTextField == null) {
secondsLeftTextField = new JTextField();
secondsLeftTextField.setPreferredSize(new Dimension(20, 20));
secondsLeftTextField.setText("0");
}
return secondsLeftTextField;
}
/**
* This method initializes southPanel
*
* @return javax.swing.JPanel
*/
private JPanel getSouthPanel() {
if (southPanel == null) {
southPanel = new JPanel();
southPanel.setLayout(new FlowLayout());
southPanel.add(getGoButton(), null);
}
return southPanel;
}
/**
* This method initializes goButton
*
* @return javax.swing.JButton
*/
private JButton getGoButton() {
if (goButton == null) {
goButton = new JButton();
goButton.setText("GO");
goButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
counter.setJl(timeLeftjLabel,Integer.parseInt(hoursLeftTextField.getText()),Integer.parseInt(minutesLeftTextField.getText()),Integer.parseInt(secondsLeftTextField.getText()));
counter.start();
goButton.setVisible(false);
}
});
}
return goButton;
}
/**
* Launches this application
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CountDown application = new CountDown();
application.getJFrame().setVisible(true);
}
});
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
import javax.swing.JLabel;
public class Counter extends Thread {
private JLabel jl = new JLabel();
private String theTime;
private int hours;
private int minutes;
private int seconds;
public Counter() {
}
public JLabel getJl() {
return jl;
}
public void setJl(JLabel jl, int h, int m, int s) {
this.jl = jl;
hours = h;
minutes = m;
seconds = s;
}
public void countBack() {
try {
while(seconds >= 0 && minutes >= 0 && hours >= 0) {
if(seconds > 0){
sleep(1000);
seconds = seconds -1;
jl.setText("" + hours + ":" + minutes + ":" + seconds);
}
else {
if(minutes > 0){
minutes = minutes - 1;
seconds = 59;
jl.setText("" + hours + ":" + minutes + ":" + seconds);
}
else{
if(hours > 0){
hours = hours - 1;
minutes = 60;
jl.setText("" + hours + ":" + minutes + ":" + seconds);
}
}
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
while(hours >= 0 && minutes >= 0 && seconds >= 0) {
countBack();
}
}
}