import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JShutdownTimer extends JPanel {
private static final String INTRO = "intro";
private static final String USED_BEFORE = "Shutting Down";
private CardLayout cardLayout = new CardLayout();
private JLabel countDownLabel = new JLabel("", SwingConstants.CENTER);
private JLabel lblHours = new JLabel("Hours to shutdown:", SwingConstants.CENTER);
private JLabel lblMinutes = new JLabel("Minutes to shutdown:", SwingConstants.CENTER);
private JLabel lblStatus = new JLabel("Status:");
private JLabel lblWelcome = new JLabel("Welcome", SwingConstants.CENTER);
private JTextField txtHours = new JTextField();
private JTextField txtMinutes = new JTextField();
private JButton btnRun = new JButton("Run");
public JShutdownTimer() {
JPanel introPanel = new JPanel();
introPanel.setPreferredSize(new Dimension(300, 200));
introPanel.setLayout(new GridLayout(8, 1));
lblStatus.setFont(new Font("Dialog", 1, 15));
introPanel.add(lblWelcome);
// hours label, textbox and validation
lblHours.setFont(new Font("Dialog", 1, 15));
introPanel.add(lblHours);
txtHours.setFont(new Font("Dialog", 1, 15));
introPanel.add(txtHours);
txtHours.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!((c >= '0') && (c <= '9') ||
(c == KeyEvent.VK_BACK_SPACE) ||
(c == KeyEvent.VK_DELETE))) {
getToolkit().beep();
e.consume();
}
}
});
// minutes label, textbos and validation
lblMinutes.setFont(new Font("Dialog", 1, 15));
introPanel.add(lblMinutes);
txtMinutes.setFont(new Font("Dialog", 1, 15));
introPanel.add(txtMinutes);
txtMinutes.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!((c >= '0') && (c <= '9') ||
(c == KeyEvent.VK_BACK_SPACE) ||
(c == KeyEvent.VK_DELETE))) {
getToolkit().beep();
e.consume();
}
}
});
// button
btnRun.setFont(new Font("Dialog", 1, 15));
introPanel.add(btnRun);
RunClass runclass = new RunClass();
btnRun.addActionListener(runclass);
JPanel introSouthPanel = new JPanel();
lblStatus.setFont(new Font("Dialog", 1, 15));
introSouthPanel.add(lblStatus);
countDownLabel.setFont(new Font("Dialog", 1, 15));
introSouthPanel.add(countDownLabel);
introPanel.add(introSouthPanel, BorderLayout.SOUTH);
JPanel usedBeforePanel = new JPanel(new BorderLayout());
usedBeforePanel.setBackground(Color.pink);
usedBeforePanel.add(new JLabel("Used Before", SwingConstants.CENTER));
setLayout(cardLayout);
add(introPanel, INTRO);
add(usedBeforePanel, USED_BEFORE);
new HurdlerTimer(this).start();
}
private class RunClass implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnRun) {
int intHours = Integer.valueOf(txtHours.getText()) * 60;
int intMinutes = Integer.valueOf(txtMinutes.getText());
int intTotalMinutes = intHours + intMinutes;
Shutdown.shutdown(intTotalMinutes);
}
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("JShutdownTimer");
frame.getContentPane().add(new JShutdownTimer());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
public void setCountDownLabelText(String text) {
countDownLabel.setText(text);
}
public void showNextPanel() {
cardLayout.next(this);
}
}
class HurdlerTimer {
private static final int TIMER_PERIOD = 1000;
protected static final int MAX_COUNT = 10;
private JShutdownTimer welcome; // holds a reference to the Welcome class
private int count;
public HurdlerTimer(JShutdownTimer welcome) {
this.welcome = welcome; // initializes the reference to the Welcome class.
String text = "(" + (MAX_COUNT - count) + ") seconds left";
welcome.setCountDownLabelText(text);
}
public void start() {
new Timer(TIMER_PERIOD, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (count < MAX_COUNT) {
count++;
String text = "(" + (MAX_COUNT - count) + ") seconds left";
welcome.setCountDownLabelText(text); // uses the reference to Welcome
} else {
((Timer) e.getSource()).stop();
welcome.showNextPanel();
}
}
}).start();
}
}